unit MyWindow;
// 请完善一下如何给这个类添加ONPaint
// 或者是任何的事件,比如是ONCLICK 等
// 万分感谢
interface
uses Windows, Messages;
type
TONPaint = procedure(h:Hwnd);
TMyWindow = class(TObject)
private
hWindow: HWnd;
fONPaint: TONPaint;
public
constructor Create;
procedure MyRun;
destructor Destroy; override;
property ONPaint: TONPaint read fONPaint write fONPaint;
end;
implementation
function WindowProc(Window: HWnd; AMessage: UINT; WParam: WPARAM;
LParam: LPARAM): LRESULT; stdcall; export;
var
dc: hdc;
ps: TPaintStruct;
r: TRect;
begin
Result := 0;
case AMessage of
WM_PAINT:
begin
if Assigned(ONPaint) then ONPaint(Window);
// 请完善一下如何给这个类添加ONPaint
{
dc := BeginPaint(Window, ps);
GetClientRect(Window, r);
DrawText(dc, '使用TMyWindow类封装的Windows程序', -1, r,
DT_SINGLELINE or DT_CENTER or DT_VCENTER);
EndPaint(Window, ps);
}
end;
else
Result := DefWindowProc(Window, AMessage, WParam, LParam); // 标准处理
end;
end;
constructor TMyWindow.Create;
const
AppName = 'MyClassDemo';
var
WindowClass: WndClass;
begin
WindowClass.Style := cs_hRedraw or cs_vRedraw;
WindowClass.cbClsExtra := 0;
WindowClass.cbWndExtra := 0;
WindowClass.hInstance := 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;
WindowClass.lpfnWndProc := TFNWndProc(@WindowProc);
RegisterClass(WindowClass);
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, SW_SHOW);
UpdateWindow(hWindow);
end;
end;
destructor TMyWindow.Destroy;
begin
inherited;
end;
procedure TMyWindow.MyRun;
var
AMessage: TMsg;
begin
while GetMessage(AMessage, 0, 0, 0) do
begin
TranslateMessage(AMessage);
DispatchMessage(AMessage);
end;
end;
end.