<<>>

701701 2008-01-25 07:57:36
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.

...全文
78 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
UltraBejing 2008-05-01
  • 打赏
  • 举报
回复
不会,帮顶
701701 2008-01-27
  • 打赏
  • 举报
回复
ONPaint 事件 是 TMyWindow的,你不明白我的意思

我的意思是
function WindowProc(Window: HWnd; AMessage: UINT; WParam: WPARAM;
LParam: LPARAM): LRESULT; stdcall;

替换成

function TMyWindow. WindowProc(Window: HWnd; AMessage: UINT; WParam: WPARAM;
LParam: LPARAM): LRESULT; stdcall;
Bear_hx 2008-01-27
  • 打赏
  • 举报
回复
这个在李唯的那本Inside VCL中有讲怎么做的办法。
Bear_hx 2008-01-26
  • 打赏
  • 举报
回复
if Assigned(ONPaint) then ONPaint(Window);

不是就给这个类添加了OnPaint事件,具体你写代码赋值就可以了

1,183

社区成员

发帖
与我相关
我的任务
社区描述
Delphi Windows SDK/API
社区管理员
  • Windows SDK/API社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧