Delphi原生Windows程序

2017-04-24 09:40:26来源:oschina作者:千里之行12人点击

原生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.

最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台