很简单的问题,就是不会,还望各位朋友进来帮帮忙!(50分)

  • 主题发起人 manmanchong
  • 开始时间
M

manmanchong

Unregistered / Unconfirmed
GUEST, unregistred user!
一个project下,有form1(unit1)和unit2(单独的单元文件没有窗体文件)。
我在unit2中自定义一个消息处理函数:
const WM_MYMSG= WM_User + 101;//消息常数
...
procedure SetMyMsg(var msg:TMessage);message WM_MYMSG;
//消息处理函数声明;
...
procedure TMyClass.SetMyMsg(var msg: TMessage);
begin //消息处理函数实现;
messagebox(Application.Handle,'message!','caption',mb_ok);
end;
在form1的button的onclick中发送消息:
sendmessage(handle,WM_MYMSG,1,1);
但是却不能响应该消息处理,还望高人指点。
 
handle用消息处理函数声明所在form的。
 
to :Xelloss
那我应该如何改正呢?
 
1 你的 Tmyclass 要从 TWincontroll 继承,必须有 handle,
你可以先用一个窗口来测试(Form2)。

2.
sendmessage(form2.handle,WM_MYMSG,1,1);
///////////

其实你可以用一个空体来测试:
在 TForm1 中定义消息处理:
procedure SetMyMsg(var msg:TMessage);message WM_MYMSG;
 
楼上说法没错
 
to jsxjd:
在本窗体中可以实现,但是在单独的单元文件就不可以了。tmyclass是直接从tobject中
继承下来的。如果不改变父类就没有办法实现吗?
 
>>tmyclass是直接从tobject中继承下来的
我记得好像必须从 TWinControl 继承才能获得消息的...

Delphi Help:

TWinControl

Windowed controls are objects that appear at runtime and that the user can
interact with. Each windowed control has a window handle, accessed through its
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~!
Handle property, that lets Windows identify and operate on the control. The
handle allows the control to receive input focus and can be passed to Windows
API functions.
All windowed controls descend from the TWinControl class. These include most
standard Windows controls, such as pushbuttons, list boxes, and edit boxes.
While you could derive an original control (one that抯 not related to any
existing control) directly from TWinControl, Delphi provides the TCustomControl
component for this purpose. TCustomControl is a specialized windowed control
that makes it easier to draw complex visual images.
 
自定义的消息好像要注册吧....
 
自定义的消息只要在两边申明的一样就不需要注册。RegisterWindowMessage只是为了
防止出现两个程序定义了同样的消息吧。
 
多人接受答案了。
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
603
import
I
I
回复
0
查看
508
import
I
I
回复
0
查看
514
import
I
顶部