关于dephi消息的问题(100分)

  • 主题发起人 主题发起人 awu306
  • 开始时间 开始时间
A

awu306

Unregistered / Unconfirmed
GUEST, unregistred user!
请问在自定义控件时怎样定义不是标准事件消息(标准事件指鼠标移动等)
 
在控件的类里面添加方法:
procedure ON_MY_MESSAGE(var m:TMessage);message MY_MESSAGE;//MY_MESSAGE换成你自己定义的消息常数
 
学了一招[:D][:D]
 
//注意uses中必须加入extctrls一个非常详细的例子
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Buttons, ExtCtrls, StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
TmyImage=class( tImage)
private
procedure CMMouseEnter(var Message: TMessage); message CM_MOUSEENTER;
procedure CMMouseLeave(var Message: TMessage); message CM_MOUSELEAVE;

end;
var
Form1: TForm1;

implementation

{$R *.DFM}
var
img:tmyimage;

procedure TmyImage.CMMouseEnter(var Message: TMessage);
begin
//进入事件
self.Canvas.TextOut(1,1,'enter');
end;

procedure TmyImage.CMMouseLeave(var Message: TMessage);
begin
//离开事件
self.Canvas.TextOut(1,1,'leave');
end;


procedure TForm1.Button1Click(Sender: TObject);
begin
img:=tmyimage.Create(self);
img.left:=10;
img.Top:=10;
img.Parent:=self;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
if img<>nil then img.free;
end;

end
 
谢谢各位的指点!
不过hbezwwl你说的还是标准事件,鼠标移动和获得焦点时触发。
但是自定义事件的话又怎样触发呢? 是否用API函数:sendmessage()?
这样的话就是显式地调用了,就不是捕获消息了。是这样吗?
 
留下你的EMAIL吧,我给你发一段代码,希望合你胃口
 
看看下面代码,不明白发mail
liuyj@cbn.com.cn

unit UnitDownload;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls, ExtCtrls;

Const
Down_Mpg = wm_User + 2;

type
TDownLoadThread = class(TThread)
private
{ Private declarations }
protected
procedure Execute; override;
end;

type
TFormDownload = class(TForm)
Panel1: TPanel;
Bevel2: TBevel;
GroupBox1: TGroupBox;
SingleLeaveTimeLabel: TLabel;
Image1: TImage;
SingleProgressBar: TProgressBar;
Button_Cancel: TButton;
Image9: TImage;
Label1: TLabel;
procedure FormShow(Sender: TObject);
procedure Button_CancelClick(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
procedure DownMpg(Var Msg:Tmessage); message Down_MPG;
{ Private declarations }
public
{ Public declarations }
end;

var
FormDownload: TFormDownload;
DownLoadThread : TDownLoadThread;

implementation

uses UnitConst, UnitContent;

{$R *.dfm}

procedure TDownLoadThread.Execute;
var
DownLoadHandle : HWND ;
begin
DownLoadHandle := FormDownLoad.Handle ;
StartSend := GetTickCount() ;
DllDown(@LoginNCUBE, 'VOD', PChar(Format('DOWN %s %s %s',[FormContent.ListViewMEDIA_FILENAME.Selected.SubItems[0], ModifyDirectory(Directory), SelectLPARTITION + FormContent.ListViewMEDIA_FILENAME.Selected.Caption])), DownLoadHandle);
FormDownLoad.Close;
end;

procedure TFormDownLoad.DownMpg(var Msg: TMessage);
var
LeaveTime : Int64;
Progress : Int64;
begin
if SingleProgressBar.Position = 99 then
begin
Close ;
end
else
begin
SingleProgressBar.StepIt ;
Progress := SingleProgressBar.Position ;
LeaveTime := SingleLeaveTime(StartSend, Progress) div 1000;
SingleLeaveTimeLabel.Caption := '剩余时间:' + IntToTime(LeaveTime);
end;
end;

procedure TFormDownload.FormShow(Sender: TObject);
begin
SingleLeaveTimeLabel.Caption := '' ;
SingleProgressBar.Position := 1 ;
DownLoadThread := TDownLoadThread.Create(False);

{if FontSize(Application.handle) = 120 then
begin
OldHeight := (Sender as TForm).Height;
OldWith := (Sender as TForm).Width;
(Sender as TForm).Height := Round((Sender as TForm).Height * 1.25 + 0.5);
(Sender as TForm).Width := Round((Sender as TForm).Width * 1.25 + 0.5);
end;}
end;

procedure TFormDownload.Button_CancelClick(Sender: TObject);
begin
Screen.Cursor := crHourGlass;
DownLoadThread.Suspend;
DownLoadThread.Terminate;
Close;
Screen.Cursor := crDefault;
end;

procedure TFormDownload.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
{if FontSize(Application.handle) = 120 then
begin
(Sender as TForm).Height := OldHeight;
(Sender as TForm).Width := OldWith;
end;}
end;

end.

谢谢
 
sendmessage()
postmessage() 都可以触发自定义的消息
 
Hb207:
你发的邮件中的代码很详细,谢谢!
不过我还有一些地方不太明白,我已经发了邮件给你了,那里有具体的问题。
也谢谢各位大使的帮助!
 
多人接受答案了。
 
后退
顶部