閱讀799 返回首頁    go 阿裏雲 go 技術社區[雲棲]


Delphi原生Windows程序

使用Windows api編寫原生Windows程序:

program Project3;

uses
  Winapi.Windows,
  Winapi.Messages,
  System.SysUtils;

const
  AppName = 'ObjectPascalHello';

function WindowProc(Window : HWND; AMessage : UINT; WParam : WPARAM; LParam : LPARAM) : LRESULT; stdcall; export;
var
  dc : HDC;
  ps : TPaintStruct;
  r : TRect;
begin
  WindowProc := 0;

  case AMessage of
    WM_PAINT:
      begin
        dc := BeginPaint(Window, ps);
        GetClientRect(Window, r);
        DrawText(dc, '原始Windows程序!', -1, r, DT_SINGLELINE or DT_CENTER or DT_VCENTER);
        EndPaint(Window, ps);
        Exit;
      end;
    WM_DESTROY:
      begin
        PostQuitMessage(0);
        Exit;
      end;
  end;

  WindowProc := DefWindowProc(Window, AMessage, WParam, LParam);
end;

function WinRegister:Boolean;
var
  WindowClass : WNDCLASS;
begin
  //定義窗口類的內容
  WindowClass.style := CS_HREDRAW or CS_VREDRAW;
  WindowClass.lpfnWndProc := TFNWndProc(@WindowProc);
  WindowClass.cbClsExtra := 0;
  WindowClass.cbWndExtra := 0;
  WindowClass.hInstance := system.MainInstance;
  WindowClass.hIcon := LoadIcon(0, idi_Application);
  WindowClass.hCursor := LoadCursor(0, idc_Arrow);
  WindowClass.hbrBackground := GetStockObject(WHITE_BRUSH);
  WindowClass.lpszMenuName := nil;
  WindowClass.lpszClassName := AppName;

  //注冊窗口類
  Result := RegisterClass(WindowClass) <> 0;
end;

function WinCreate : HWND;
var
  hWindow : HWND;
begin
  hWindow := CreateWindow(AppName, 'Hello World', WS_OVERLAPPEDWINDOW, cw_usedefault, cw_usedefault, cw_usedefault, cw_usedefault, 0, 0, system.MainInstance, nil);

  if hWindow <> 0 then
  begin
    ShowWindow(hWindow, CmdShow);
    ShowWindow(hWindow, SW_SHOW);
    UpdateWindow(hWindow);
  end;

  Result := hWindow;
end;


var
  AMessage : TMsg;
  hWindow : HWND;
begin
  //定義窗口類的內容
  //注冊窗口類
  //創建窗口
  //進入窗口消息處理循環,知道程序結束。
  if not WinRegister then
  begin
    MessageBox(0, 'Register failed', nil, MB_OK);
    Exit;
  end;
  hWindow := WinCreate;
  if LongInt(hWindow) = 0 then
  begin
    MessageBox(0, 'WinCreate failed', nil, MB_OK);
    Exit;
  end;

  while GetMessage(AMessage, 0, 0, 0) do
  begin
    TranslateMessage(AMessage);
    DispatchMessage(AMessage);
  end;
  Halt(AMessage.wParam);
end.

最後更新:2017-05-07 07:57:21

  上一篇:go Greenplum 跨庫數據JOIN需求 - dblink的使用和弊端以及解決方案
  下一篇:go FastReport中如何加入自定義函數