用delphi写的原生窗口(〖暴风侧影〗原创)
很早就想用api来写delphi原生窗口,一直都很懒,今天看了下李维的书,冲动之下就写了个。
program MyWindow;
uses
Windows,Messages,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,'用object pascal写的Widnows窗口哦!', -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_INFORMATION);
WindowClass.hCursor := LoadCursor(0,IDC_CROSS);
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,'〖暴风侧影〗',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,'注册失败',nil,MB_OK);
Exit;
end;
hWindow := WinCreate;
if longint(hWindow) = 0 then
begin
MessageBox(0,'创建失败',nil,MB_OK);
Exit;
end;
while GetMessage(AMessage, 0, 0, 0) do
begin
TranslateMessage(AMessage);
DispatchMessage(AMessage);
end;
Halt(0);
end.
uses
Windows,Messages,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,'用object pascal写的Widnows窗口哦!', -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_INFORMATION);
WindowClass.hCursor := LoadCursor(0,IDC_CROSS);
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,'〖暴风侧影〗',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,'注册失败',nil,MB_OK);
Exit;
end;
hWindow := WinCreate;
if longint(hWindow) = 0 then
begin
MessageBox(0,'创建失败',nil,MB_OK);
Exit;
end;
while GetMessage(AMessage, 0, 0, 0) do
begin
TranslateMessage(AMessage);
DispatchMessage(AMessage);
end;
Halt(0);
end.