program WinApp;<br><br>uses<br> Windows, Messages;<br><br>var<br> GAppClass: PChar = 'TWinApp';<br> GAppCaption: PChar = 'A Standard Windows Application';<br> AMessage: MSG;<br> hWindow: HWND;<br> btnAbout: HWND;<br> btnClose: HWND;<br> <br>{$R *.RES}<br><br>// 窗口的消息处理函数<br>function WindowProc(AhWnd: HWND; AMessage: UINT; wParam: WPARAM;<br> lParam: LPARAM): LRESULT; stdcall; export;<br>begin<br> WindowProc := 0;<br> case AMessage of<br> WM_COMMAND:<br> begin<br> if HWND(lParam) = btnAbout then<br> MessageBox(hWindow, 'SDK Application demo by Rainux',<br> 'About', MB_OK);<br> if HWND(lParam) = btnClose then<br> PostMessage(hWindow, WM_DESTROY, 0, 0);<br> end;<br> WM_DESTROY:<br> begin<br> PostQuitMessage(0);<br> Exit;<br> end;<br> end;<br> WindowProc := DefWindowProc(AhWnd, AMessage, wParam, lParam);<br>end;<br><br>// 窗口注册函数<br>function WinRegister: Boolean;<br>var<br> wc: WNDCLASS;<br>begin<br> { Define the Window class 's style }<br> wc.style := CS_HREDRAW or CS_VREDRAW or CS_DBLCLKS;<br> { Define the WindowProc function }<br> wc.lpfnWndProc := @WindowProc;<br> wc.cbClsExtra := 0;<br> wc.cbWndExtra := 0;<br> wc.hInstance := HInstance;<br> { Load common MAINICON from resource }<br> wc.hIcon := LoadIcon(HInstance, 'MAINICON');<br> wc.hCursor := LoadCursor(0, IDC_ARROW);<br> { Should be COLOR_BTNFACE + 1, why? }<br> wc.hbrBackground := COLOR_BTNFACE + 1;<br> { If use menu, must be the resource name }<br> wc.lpszMenuName := nil;<br> wc.lpszClassName := GAppClass;<br> Result := Windows.RegisterClass(wc) <> 0;<br>end;<br><br>// 窗口建立函数<br>function WinCreate: HWND;<br>var<br> hWindow: HWND;<br>begin<br> { The window with WS_PUPOP style will like TForm.BorderStyle := bsNone }<br> hWindow := CreateWindowEx(WS_EX_OVERLAPPEDWINDOW, GAppClass, GAppCaption,<br> WS_OVERLAPPEDWINDOW,<br> 200, 200, 400, 200,<br> 0, 0,<br> HInstance, nil);<br> btnAbout := CreateWindow('Button','About',WS_VISIBLE or WS_CHILD or <br> BS_PUSHLIKE or BS_TEXT or WS_TABSTOP,<br> 210, 130, 75, 25, hWindow, 0, HInstance, nil);<br> btnClose := CreateWindow('Button','Close',WS_VISIBLE or WS_CHILD or <br> BS_PUSHLIKE or BS_TEXT or WS_TABSTOP,<br> 300, 130, 75, 25, hWindow, 0, HInstance, nil);<br> if hWindow <> 0 then ShowWindow(hWindow, SW_SHOW);<br> if btnAbout <> 0 then ShowWindow(btnAbout, SW_SHOW);<br> if btnClose <> 0 then ShowWindow(btnClose, SW_SHOW);<br> Result := hWindow;<br>end;<br><br>// WinMain<br>begin<br> if not WinRegister then<br> begin<br> MessageBox(0, '注册窗口失败!', nil, MB_OK);<br> Exit;<br> end;<br><br> hWindow := WinCreate;<br> if hWindow = 0 then<br> begin<br> MessageBox(0, '建立窗口失败!', nil, MB_OK);<br> Exit;<br> end;<br><br> { The standard message loop }<br> { If the hWnd is 0, will retrieves messages for any window<br> that belongs to the calling thread and thread messages posted<br> to the calling thread }<br> while GetMessage(AMessage, 0, 0, 0) do<br> begin<br> if not IsDialogMessage(hWindow, AMessage) then<br> begin<br> TranslateMessage(AMessage);<br> DispatchMessage(AMessage);<br> end;<br> end;<br><br>end.<br>