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. 原文链接

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值