《windows程序设计》获取窗口尺寸(05)
代码如下:
program Project2; {$APPTYPE CONSOLE} {$R *.res} uses System.SysUtils, windows, Winapi.Messages, Vcl.Dialogs; var swndClass: tagWNDCLASS; message: MSG; mHwnd: hwnd; cxClient, cyClient: Integer; function WindowProc(hwnd: hwnd; uMsg: UINT; wParam: wParam; lParam: lParam): LRESULT; stdcall; var uHdc: HDC; ps: tagPAINTSTRUCT; str: string; begin case uMsg of WM_CREATE: begin {获取窗口尺寸} cxclient := GetSystemMetrics(SM_CXSCREEN); cyclient := GetSystemMetrics(SM_CYSCREEN); end; WM_PAINT: begin uHdc := BeginPaint(hwnd, ps); str := format('当前分辨率:%d X %d', [cxclient, cyclient]); TextOut(uHdc, 0, 0, PWideChar(str), Length(str)); EndPaint(hwnd, ps); end; end; result := DefWindowProc(hwnd, uMsg, wParam, lParam); end; begin swndClass.cbClsExtra := 0; //窗口类扩展,无 swndClass.cbWndExtra := 0; //窗口实例扩展 swndClass.hbrBackground := COLOR_BACKGROUND; //窗口背景颜色黑色 //LoadCursor() swndClass.hCursor := LoadCursor(0, IDC_ARROW); //窗口采用箭头光标 swndClass.hIcon := LoadIcon(0, IDI_APPLICATION); //窗口最小化图标:采用缺省图标 swndClass.hInstance := hInstance; //窗口实例句柄 swndClass.lpfnWndProc := @WindowProc; //窗口处理函数 swndClass.lpszClassName := 'myWnd'; //窗口类名 swndClass.lpszMenuName := nil; //窗口菜单 swndClass.style := CS_DBLCLKS; //窗口样式 if RegisterClass(swndClass) = 0 then begin Writeln('windows class register error!'); Exit; end; mHwnd := CreateWindowEx(0, 'myWnd', 'Delphi Windows', WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 544, 375, HWND_DESKTOP, 0, hInstance, 0); ShowWindow(mHwnd, SW_SHOW); UpdateWindow(mHwnd); while GetMessage(message, 0, 0, 0) do begin TranslateMessage(message); DispatchMessage(message); end; Readln; end.