学用 Delphi Message 方法全集
- 格式:doc
- 大小:561.50 KB
- 文档页数:58
delphi memo用法-回复Delphi Memo常用于在Delphi编程中处理文本信息和显示大量数据。
它提供了许多功能和方法,可以使开发者更方便地操作和显示文本。
下面我将详细介绍Memo的用法,并一步一步回答与它相关的问题。
Memo是Delphi中的一个常用控件,它类似于一个多行编辑框或文本框,但功能更为强大。
使用Memo可以方便地通过代码来控制文本的输入、输出以及各种文本处理操作。
1. Memo控件的相关属性和方法Memo控件具有许多有用的属性和方法,可以让我们更灵活地操作文本。
以下是一些常用的属性和方法:- Text属性:获取或设置Memo的文本内容。
- Lines属性:获取Memo的行集合。
- SelStart、SelLength、SelText属性:用于选择部分文本或获取选择文本的起始位置、长度和内容。
- Append方法:向Memo的末尾追加文本内容。
- Clear方法:清空Memo的内容。
- SelText属性:获取或设置所选文本。
2. Memo的基本用法要使用Memo控件,首先需要在窗体上放置一个Memo控件。
可以通过拖动Memo控件到窗体上的方式来添加Memo控件,也可以通过代码创建Memo控件并添加到窗体上。
2.1 文本输入和输出使用Memo控件进行文本输入和输出是Memo的基本功能之一。
通过修改Text属性,我们可以设置Memo的文本内容;通过读取Text属性,我们可以获取Memo的文本内容。
例如,使用以下代码将一段文本输出到Memo控件中:DelphiMemo1.Text := 'Hello, World!';此时,Memo1控件将显示"Hello, World!"。
2.2 文本操作Memo控件还提供了一些方法和属性,用于对文本进行操作。
例如,我们可以通过Append方法向Memo控件中追加文本。
使用以下代码将"Welcome!"追加到Memo控件的末尾:DelphiMemo1.Append('Welcome!');Memo1控件将显示"Hello, World!"和"Welcome!"。
MessageDlg用法对话框类型:mtwarning——含有感叹号的警告对话框mterror——含有红色叉符号的错误对话框mtinformation——含有蓝色i符号的信息对话框mtconfirmation——含有绿色问号的确认对话框mtcustom——不含图标的一般对话框,对话框的标题是程序的名称按钮组中的按钮:mbYes——mrYes或6mbNo——mrNo或7mbOk——mrOk或1mbCancel——mrCancel或2mbHelp——help按钮mbAbort——mrAbort或3mbRetry——mrRetry或4mbIgnore——mrIgnore或5mbAll——mrAll或8mbNoToAll——9mbYesToAll——10procedure TForm1.Button1Click(Sender: TObject);varS:string;beginif MessageDlg('提示框',mtInformation,[mbok,mbyes],0)=mryes thenmessagebeep(MB_ICONEXCLAMATION);end;TMsgDlgType = (mtWarning, mtError, mtInformation, mtConfirmation, mtCustom);TMsgDlgBtn = (mbYes, mbNo, mbOK, mbCancel, mbAbort, mbRetry, mbIgnore, mbAll, mbNoToAll, mbYesToAll, mbHelp);constmrNone = 0;mrOk = idOk;mrCancel = idCancel;mrAbort = idAbort;mrRetry = idRetry;mrIgnore = idIgnore;mrYes = idYes;mrNo = idNo;mrAll = mrNo + 1;mrNoToAll = mrAll + 1;mrYesToAll = mrNoToAll + 1;MessageDlg()信息的汉化Delphi中的常量都放在consts.pas中,修改它可以达到汉化的目的.如: MessageDlg()显示的窗口标题及其中的按钮标题都是英文,虽然不影响使用,但在一个中文软件中总显得有些不协调.为此在consts.pas中查找以下内容:"SMsgDlgWarning"把SMsgDlgWarning = 'Warning';SMsgDlgError = 'Error';SMsgDlgInformation = 'Information';SMsgDlgConfirm = 'Confirm';SMsgDlgYes = '&Yes';SMsgDlgNo = '&No';SMsgDlgOK = 'OK';SMsgDlgCancel = 'Cancel';SMsgDlgHelp = '&Help';SMsgDlgHelpNone = 'No help available';SMsgDlgHelpHelp = 'Help';SMsgDlgAbort = '&Abort';SMsgDlgRetry = '&Retry';SMsgDlgIgnore = '&Ignore';SMsgDlgAll = '&All';SMsgDlgNoToAll = 'N&o to All';SMsgDlgYesToAll = 'Yes to &All';改成SMsgDlgWarning = '警告'; SMsgDlgError = '错误'; SMsgDlgInformation = '提示'; SMsgDlgConfirm = '确认'; SMsgDlgYes = '是(&Y)';SMsgDlgNo = '不(&N)';SMsgDlgOK = '确定'; SMsgDlgCancel = '取消'; SMsgDlgHelp = '帮助(&H)'; SMsgDlgHelpNone = '没有该帮助信息'; SMsgDlgHelpHelp = '帮助'; SMsgDlgAbort = '放弃(&A)'; SMsgDlgRetry = '重试(&R)'; SMsgDlgIgnore = '忽略(&I)'; SMsgDlgAll = '全部(&A)'; SMsgDlgNoToAll = '全都不(&O)'; SMsgDlgYesToAll = '全都是(&A)';然后重新编译Consts.pas,把Consts.duc拷到delphi的lib和slib子目录下,就一劳永逸啦!。
用delphi 实现发送短信
昨天实现了用 java 发送短信,今天就实现用 delphi 发,其实原理都一样,但是看起来好像用 delphi 更简单一些。
一、准备工作
请参看 java 发短信的准备工作(点击进入)
二、开发环境准备
下载 delphi7 或 delphi2007 均可,我用的是后者,不过 delphi7 一样可以使用 APRO,不用担心版本问题。
三、控件准备
下载 TurboPower Async Professional,也就是 APRO 控件,注意版本,必须是4.0.2 以上的,不然没有发短信控件。
将这个控件安装到 delphi 中,会多出APRO 面板,看到面板时,表明安装成功。
四、软件开发
从控件面板中找到 TApdCommPort,TApdTApiDevice,TApdGSMPhone控件,把它们拖到窗体中。
然后放入 TLabel,TEdit,TButton,界面如图所示:
然后将TApdCommPort,TApdTApiDevice,TApdGSMPhone进行属性关联。
关联完毕后,即可编写代码。
双击“选择设备”按钮,写一行代码:
双击“发送”按钮,写以下的代码:
好了,程序已经完成了,下面就是编译并且运行,发短信时记得填入短信中心的号码,这个号码如果不知道,可以去你的手机上找,肯定有的。
Delphi中SendMessage使用技巧Windows系统是由消息机制驱动的每个线程如果建立了一个窗口则由系统分配一个消息队列用于窗口消息的处理另外消息也可以不经过消息队列而利用SendMessage函数直接发送给窗口窗口过程将处理这个消息但只有当消息被处理之后SendMessage才能返回到调用程序下面结合两个Delphi程序讨论如何利用SendMessage向控件发送消息和控件对这种消息的响应用SendMessage向控件发送消息在编程中有时需要控件以特殊的风格显示而这种要求又无法通过设置控件属性实现例如读取客户列表并显示在下拉框供用户选择如果下拉框宽度太窄则不能全部显示如果将宽度定得太宽界面又有不紧凑之感因此希望能在运行期动态地确定下拉框显示区域的宽度这种要求如果不用SendMessage函数就很难实现解决办法是在读数据库时计算字符串的显示宽度用显示宽度的最大值确定下拉框显示区域的宽度再用SendMessage函数向下拉框发送CB_SETDROPPEDWIDTH消息和宽度值下拉框根据消息中传来的信息就可以进行正确显示部分源程序代码如下i:= ; //计数MaxWidth:= ; Query SQL Clear; Query SQL Add( select Company from Customer ); Query Open;//读客户列表到下拉框while not Query Eof do begin ComboBox Items add(Query FieldByName( Company ) AsString);Width:=ComboBox Font Size *Length(ComboBox Items[i]);if Width>MaxWidth then MaxWidth:=Width; //找出最大值Query Next; i:=i+; end; Query Close; ComboBox Text:=ComboBox Items[ ]; //发送消息以确定显示区域的宽度SendMessage(ComboBox Handle CB_SETDROPPEDWIDTH MaxWidth ); 利用SendMessage函数还可以实现一些有趣的效果例如在按钮的Click事件中加入如下语句SendMessage(Button Handle BM_SETSTYLEBS_RADIOBUTTON )运行后点击按钮就可以把按钮变成一个收音机按钮控件接收SendMessage消息上面讨论了用SendMessage向控件发送消息的过程但凡事有利就有弊用SendMessage发送的消息在处理上存在着一定困难因为该消息不经过消息队列所以无法用OnMessage方式来指定对消息的响应甚至用HookMainWindow也不行因为消息直接发送到控件绕过了主窗体要对这种类型的消息作出响应需要重载控件的WndProc *** 例如对于一个列表框滚动条的滚动消息就是用SendMessage方式发送的因此该消息不在TlistBox的事件列表中下面是处理控件响应该滚动消息的具体步骤首先从TlistBox继承一个TmyListBox类并重载WndProc *** 在程序中加入下列定义typeTMyListBox=class(TListBox)privateprocedure WndProc(var Msg: TMessage); override;//重载WndProc 处理发送到控件的消息publicend; 其中WndProc *** 指定控件对消息的响应输入参数是TMessage类型该数据类型是一个记录包含了消息代码和消息的参数消息参数可以用Longint或Word方式获得对滚动事件做出响应在WndProc *** 中加入如下处理代码if (Msg Msg=WM_VSCROLL) and(Msg WParamLo=SB_ENDSCROLL) then begin//获得鼠标位置对应的列ItemIndex:=ItemAtPos(Point true); Form Edit Text:=inttostr(ItemIndex);inherited;end else inherited; 当程序接收到WM_VSCROLL消息且WParamLo参数为SB_ENDSCROLL时表示竖直滚动条停止滚动就可以用ItemAtPos *** 确定与鼠标位置对应的ItemIndex ItemAtPos *** 的Point参数是一个TPoint类型的变量用来保存鼠标的位置lishixinzhi/Article/program/Delphi/201311/24698。
文章标题:深入探讨Delphi中MessageBox的用法及其实际应用在Delphi编程语言中,MessageBox是一个非常常见且实用的函数,用于在程序中显示各种提示、警告和错误信息。
在本文中,我们将从浅入深地探讨MessageBox的使用方法及其实际应用。
1. MessageBox的基本语法在Delphi中,使用MessageBox函数可以在程序中创建一个消息框来显示提示信息。
其基本语法如下:```ShowMessage('这是一个简单的提示信息');```上面的代码将在程序中弹出一个消息框,显示内容为“这是一个简单的提示信息”。
2. MessageBox的参数MessageBox函数还可以接受多个参数,以便实现更丰富的提示信息展示。
常用的参数包括:- 弹出消息框的标题- 指定显示的消息内容- 消息框的类型(比如信息、警告、错误等)- 消息框的按钮类型(比如确定、取消、是、否等)3. MessageBox的实际应用在实际开发中,MessageBox函数可以帮助程序员完成很多任务,比如:- 在用户操作错误时给出友好的提示- 在程序出现异常时提醒用户- 在特定条件下展示重要的信息4. MessageBox的深入使用除了基本的使用方法,MessageBox还可以通过结合其他功能来实现更复杂的效果。
比如可以在MessageBox弹出后执行一些额外的逻辑,或者定制化消息框的外观和行为。
5. 个人观点和总结对于MessageBox的用法,我的个人观点是,在实际开发中要灵活运用MessageBox函数,根据不同的场景和需求来选择合适的参数和样式,以展现出更好的用户体验。
也要注意不要过多地使用MessageBox,以免影响用户的操作流畅性。
通过本文的阐述,相信大家对Delphi中MessageBox的用法有了更深入的了解。
在实际开发中,熟练掌握MessageBox函数的使用方法将有助于提高程序的用户友好性和交互性。
delphi中Message消息的使用方法原文链接:/delphi1234/archive/2008/02/20/211008 3.aspxunit Unit1;interfaceusesWindows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;constWM_ME=WM_USER+100; //自定义消息;typeTForm1 = class(TForm)Button1: TButton;procedure Button1Click(Sender: TObject);//第一种消息处理方式; 只能处理常量消息;procedure wmme(var message:TMessage);message WM_ME; //自定义消息处理过程,专门处理WM_ME消息; private { Private declarations }public{ Public declarations }//第二种消息处理方式;可能处理常量或变量消息;procedure WndProc(var message:TMessage);override; //重载窗口消息过程//第三种消息处理方式procedure WMCommand(var Message: TWMCommand); message WM_COMMAND; //命令消息处理过程end;varForm1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);beginSendMessage(Handle,WM_ME,0,0); //发送消息WM_ME ; 消息先由WndProc处理,再交给wmme处理;end;procedure TForm1.WMCommand(var Message: TWMCommand);begin //第三种消息处理方式if Message.NotifyCode = BN_CLICKED thenif FindControl(Message.Ctl) = Button1 then showmessage(‘点击了Button1’);inherited;end;procedure TForm1.wmme(var message: TMessage);beginShowMessage(IntT oStr(Handle)+ 'wmme');//第一种消息处理方式end;procedure TForm1.WndProc(var message: TMessage);beginif message.Msg=WM_ME then //第二种消息处理方式ShowMessage(IntT oStr(Handle)+ 'WndProc');inherited WndProc(Message); //这里inherited才会触发一次wmme消息;end;end.实例2/////////////////////////////////////////////////////////////消息的广播方式//////////////////////////////////////////////////////////////////////// ////////////消息广播只能将消息传递到接收消息的主程序中,MDIChild窗体不能接收到广播消息;/////////unit Unit1;interfaceusesWindows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;typeTForm1 = class(TForm)Button1: TButton;procedure Button1Click(Sender: TObject);procedure FormCreate(Sender: TObject);private{ Private declarations }public{ Public declarations }procedure WndProc(var message:TMessage);override;end;varForm1: TForm1;MyMessage: DWORD;implementation{$R *.dfm}{ TForm1 }procedure TForm1.WndProc(var message: TMessage);beginif message.Msg=MyMessage then //第3步:重载Windows 消息处理过程,处理接收到的MyMessage消息;ShowMessage(FloatToStr(Handle)+ 'MyMessage');inherited WndProc(Message);end;procedure TForm1.Button1Click(Sender: TObject);beginSendMessage(HWND_BROADCAST,MyMessage,0,0); //第2步:广播MyMessage消息;end;/////////预先注册好Windows消息;第一种方法////////////////////procedure TForm1.FormCreate(Sender: TObject);begin//同一字符串,如'MyMessageMe'在不同的程序中调用RegisterWindowMessage注册消息时,返回的MyMessage结果是一样的。
Delphi中SendMessage使用方法找另外一个窗口的句柄:handle := FindWindow(nil,PChar('窗口的标题'));//查到窗体句柄查找子窗体:childHandle := FindWindowEx(handle,0,'子窗体类','子窗体标题');另外有个枚举子窗体的API,EnumChildWindows(主创体句柄,@回调函数,用户参数);用这个函数需要自己写一个回调的函数,比如:function EnumChildProc(ahWND:HWND; param:LPARAM):boolean; stdcall;sendmessage(handle,message,wl,rl)unit Unit1;interfaceuses Windows, Messages,Tlhelp32, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;typeTForm1 = class(TForm)procedure FormCreate(Sender: TObject);private{ Private declarations }public{ Public declarations }end;var Form1: TForm1;implementation{$R *.DFM}procedure TForm1.FormCreate(Sender: TObject);var a,b:PAnsiChar;h:HWND;beginh:= FindWindow(nil,'abc.txt - 记事本');h:= FindWindowEx(h,0,'edit',nil);SendMessage(h,WM_SETTEXT,255,Integer(PChar('我来测度了')));ShowMessage( IntToStr(h));end;SendMessage(TreeView.Handle,TVM_SETBKCOLOR,0,RGB(25 5,0,0)); 设置TV背景颜色SendMessage(Button.Handle,WM_LBUTTONDOWN,0,0);鼠标左键按下SendMessage(Button.Handle,WM_LBUTTONUP,0,0); 鼠标左键抬起SendMessage(Edit.Handle,WM_SETTEXT,255,Integer(PChar(' abc'))); 传递文本SendMessage(Edit.Handle,WM_Char,Wparam('Q'),2);传递字符SendMessage(Button.Handle,BM_SETSTYLE,BS_RADIOBUTT ON,1); 改变Button风格SendMessage(ComboBox.Handle,CB_SETDROPPEDWIDTH,3 00,0); 改变CBDownWidthWM_CUT、WM_COPY和WM_PASTE 剪切,复制,粘帖实现任意组合键keybd_event(VK_Control, MapVirtualKey(VK_Control, 0), 0, 0);keybd_event(ord('V'), MapVirtualKey(ord('V'), 0), 0, 0);keybd_event(ord('V'), MapVirtualKey(ord('V'), 0), KEYEVENTF_KEYUP, 0);keybd_event(VK_Control, MapVirtualKey(VK_Control, 0), KEYEVENTF_KEYUP, 0);SendMessageA说明: 调用一个窗口的窗口函数,将一条消息发给那个窗口。
delphi收发短信介绍一下这段代码,这段代码绝对不是网上那些不能编译,或是没有价值的东西,这些代码都是本人亲测,能够收发短信的代码,相信是经典的不可多得的代码unit Unit1;interfaceusesWindows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,Dialogs,Unit2, StdCtrls, ExtCtrls;//请务必下载PCommMonitor6来帮助理解或者是自己分析,PCommMonitor6是一款串口监视软件typeTForm1 = class(TForm)Button1: TButton;Memo1: TMemo;Edit1: TEdit;Edit2: TEdit;Label1: TLabel;procedure FormCreate(Sender: TObject);procedure FormDestroy(Sender: TObject);procedure Button1Click(Sender: TObject);private{ Private declarations }publicprocedure msgok(ok:boolean);procedure laixin(haoma:string;neirong:string);{ Public declarations }end;varForm1: TForm1;shouji:Tshouji;implementation{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);beginshouji:=tshouji.create;shouji.ok:=msgok;shouji.arr:=laixin;end;procedure TForm1.FormDestroy(Sender: TObject);beginshouji.destroy;end;procedure TForm1.msgok(ok:boolean); //发送完毕事件beginif ok thenbeginmemo1.Lines.Add('发送成功')end elsebeginmemo1.Lines.Add('发送失败');end;end;procedure ixin(haoma:string;neirong:string); //收到消息事件beginmemo1.Lines.Add('=========收到信息=========');memo1.Lines.Add('信息号码为:'+haoma);memo1.Lines.Add('收到信息为:'+neirong);memo1.Lines.Add('==========================' );end;procedure TForm1.Button1Click(Sender: TObject);//调用发送方法beginshouji.sendx(edit1.Text,edit2.Text);memo1.Lines.Add('=========发送信息=========');memo1.Lines.Add('发送号码为:'+edit1.Text);memo1.Lines.Add('发送信息为:'+edit2.Text);memo1.Lines.Add('==========================' );end;end./////////////////////////////////////////////////////////////////// /////////////////////////////////////////////unit Unit2;//中兴u230通行模块,中兴u230实际价格是320元,带pc套件,能当modem,功能是相当于六七百的高档手机的//但美中不足就是移动定制手机interfaceusesWindows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,Dialogs,AdPort,ExtCtrls;typeTarrive=procedure (haoma:string;neirong:string) of object;//接受信息的指针Tsendok=procedure (ok:boolean) of object;//判断信息发送情况指针Tshouji=class(Tcomponent)privateFarr:Tarrive;Fok:Tsendok;ComPort: TApdComPort;Time:TTimer;procedure timetime(Sender: TObject);//主要是用于开始的测试连接procedure TriggerAvail(CP: TObject; Count: Word);//串口接受信息publicconstructor create;destructor destroy;override;procedure sendx(haoma:string;neirong:string);//发送信息的方法publishedproperty arr:Tarrive read Farr write Farr;//接受信息的事件property ok:Tsendok read Fok write Fok;//信息发送情况end;xinxi=record //暂时存放信息haoma:string; //信息号码xinxi:string;//信息内容end;varmyxinxi:xinxi;implementationconstructor Tshouji.create;begincomport:=TApdComPort.Create(self);Number:=9;//我的U230手机串口号是9 ,有的可能是4或5comport.OnTriggerAvail:=TriggerAvail;comport.AutoOpen:=true;comport.Open:=true;time:=ttimer.Create(self);time.Interval:=1000;time.OnTimer:=timetime;time.Enabled:=true;end;destructor Tshouji.destroy;begincomport.Output:='AT+ZPCSTART=0'#13;//断开与u230连接comport.Destroy;time.Destroy;inherited;end;function_Ucx2T oText(buf:pbytearray;len:integer):string;//pdu解码vartemp,dest:array [0..260] of byte;i:integer;beginlen:=len div 2;for i:=0 to len-1 dobegintemp[i*2]:=buf[i*2+1];temp[i*2+1]:=buf[i*2];temp[i*2+2]:=0;temp[i*2+3]:=0;end;widechartomultibyte(cp_acp,0,pwidechar(@temp[0]),-1,lptstr(@dest[0]),260,nil,nil);result:=strpas(@dest[0]);end;function SEncodeMobNO(SmobNO: string): string;//手机号码进行PDU编码。
Delphi消息处理机制Delphi中的消息消息是Windows发出的一个通知,它告诉应用程序某个事件发生了。
在Delphi中,大多数情况下Windows的消息被封装在VCL的事件中,我们只需处理相应的VCL事件就可以了,但如果我们需要编写自己的控件、截获或过滤消息就必须深入研究Win32的消息处理机制。
在Delphi中消息以TMessage记录的方式定义。
打开Message.pas文件,我们可以看到Tmessage是这样定义的:typeTMessage = packed recordMsg: Cardinal;case Integer of0: ( WParam: Longint;LParam: Longint;Result: Longint);1: ( WParamLo: Word;WParamHi: Word;LParamLo: Word;LParamHi: Word;ResultLo: Word;ResultHi: Word);end;其中,Msg是区别于其他消息的常量值,这些常量值可以是Windows单元中预定义的常量,也可以是用户自己定义的常量。
Wparam通常是一个与消息有关的常量值,也可以是窗口或控件的句柄。
LParam通常是一个指向内存中数据的指针。
Result是消息处理的返回值。
Wparam、Lparam和Result都是32位的,如果想访问其中的低16位或高16位可以分别使用WparamLo、WparamHi、LParamLo、LparamHi、ResultLo和ResultHi。
在Delphi中除了通用的Tmessage外,还为每个Windows定义了一个特殊的消息记录。
我们可以浏览Message.pas文件,下面是键盘的消息记录:TWMKey = packed recordMsg: Cardinal;CharCode: Word;Unused: Word;KeyData: Longint;Result: Longint;与键盘相关的消息如:WM_KEYDOWN、WM_KEYUP、WM_CHAR、WM_SYSKEYDOWN WM_SYSKEYUP、WM_SYSCHAR的记录也被定义为TWMkey。
学习 Message(1): 消息发送消息流程:譬如用鼠标左键点击一下窗体, 系统会收到一条 WM_LBUTTONDOWN 消息;当鼠标抬起, 系统又会收到 WM_LBUTTONUP 消息.系统收到消息后, 会告诉窗体发生的事情, 然后窗体再做出反应; 当然窗体能否做出反应要看窗体是否有相应的响应代码.要响应 WM_LBUTTONDOWN 消息, 我们需要在 OnMouseDown 事件中写代码;要响应 WM_LBUTTONUP 消息, 我们需要在 OnMouseUp 事件中写代码;当然我们也可以通过其他方法(譬如消息方法)来响应消息.关于消息流程, 这样说其实不够完整, 更详细的流程可以参见:/del/archive/2008/04/08/1142768.html系列.消息发送:我们通过鼠标、键盘等操作, 就在有意无意地随时向系统发送消息; 但有时我也需要间接发送消息, 譬如: 我们并不需要点击窗体, 也可以向窗体发送一条WM_LBUTTONDOWN 消息, 从而引发 OnMouseDown 事件的执行.发送消息的常用函数:function SendMessage(hWnd: HWND; {目标句柄}Msg: UINT; {要发送的消息(编号)}wParam: WPARAM; {消息参数}lParam: LPARAM {消息参数}): LRESULT; stdcall; {返回处理结果}function PostMessage(hWnd: HWND; {目标句柄}Msg: UINT; {要发送的消息(编号)}wParam: WPARAM; {消息参数}lParam: LPARAM {消息参数}): BOOL; stdcall; {是否发送成功}function Perform(Msg: Cardinal; {要发送的消息(编号)}WParam: WPARAM; {消息参数}LParam: LPARAM {消息参数}): LRESULT; {返回处理结果}SendMessage 发送消息后会等待处理结果;PostMessage 把消息送人等待处理的消息队列.发送 SendMessage 消息好比: 你扔一颗手榴弹到敌群中, 并期待看到血肉横飞的场面;发送 PostMessage 消息好比: 你在敌人就要路过的地方埋上一颗地雷, 你就不管了.其实 SendMessage 和 PostMessage 在实用过程中区别并不是很大.Perform 和上面两个不同, 它是控件的一个方法, 最早定义在 TControl 类, TControl 的子孙都有这个方法, TControl 之上的父类们不需要处理消息, 也没有消息要处理.Perform 和 SendMessage 更相似, 但貌似少了一个参数, 其实没有, 只是换了一下位置, 看它的调用对比:SendMessage(控件句柄, 消息, 参数, 参数);控件.Perform(消息, 参数, 参数);消息和消息参数:消息就是一个整数, 就是微软给做的一个编号:0 .. WM_USER-1: Windows 使用WM_USER .. $7FFF: 窗体类使用$8000 .. $BFFF: 保留$C000 .. $FFFF: 应用程序使用大于 $FFFF: 保留Delphi 也自定义了一些消息, 分别是 CM_ 和 CN_ 打头.消息参数是个比较复杂的概念, 因为不同的消息有不同的参数规定, 其实学习消息的过程就是学习消息参数.不过有些消息的参数就特简单, 譬如 WM_CLOSE 是关闭窗口的消息, 它不需要什么参数(不需要也要按格式写个数).下面的例子使用四种不同的方法来关闭窗口:unit Unit1;interfaceusesWindows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,Dialogs, StdCtrls;typeTForm1 = class(TForm)Button1: TButton;Button2: TButton;Button3: TButton;Button4: TButton;procedure Button1Click(Sender: TObject);procedure Button2Click(Sender: TObject);procedure Button3Click(Sender: TObject);procedure Button4Click(Sender: TObject);end;varForm1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);beginClose;end;procedure TForm1.Button2Click(Sender: TObject);beginSendMessage(Self.Handle, WM_CLOSE, 0, 0);end;procedure TForm1.Button3Click(Sender: TObject);beginPostMessage(Self.Handle, WM_CLOSE, 0, 0);end;procedure TForm1.Button4Click(Sender: TObject);beginSelf.Perform(WM_CLOSE, 0, 0);end;end.发送 WM_MOUSEMOVE 消息代码文件:unit Unit1;interfaceusesWindows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,Dialogs, StdCtrls, ExtCtrls;typeTForm1 = class(TForm)Panel1: TPanel;Button1: TButton;Button2: TButton;Button3: TButton;procedure Panel1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);procedure Button1Click(Sender: TObject);procedure Button2Click(Sender: TObject);procedure Button3Click(Sender: TObject);end;varForm1: TForm1;implementation{$R *.dfm}{鼠标在 Panel1 中移动时, 我做了如下处理:}{1、显示鼠标在 Panel1 中的坐标}{2、显示是否同时按住了 Shift、Ctrl、Alt}procedure TForm1.Panel1MouseMove(Sender: TObject; Shift: TShiftState; X,Y: Integer);varList: TStringList;beginList := TStringList.Create;if ssShift in Shift then List.Add('Shift');if ssCtrl in Shift then List.Add('Ctrl');if ssAlt in Shift then List.Add('Alt');if List.Count > 0thenPanel1.Caption := Format('%s: %d, %d', [maText, X, Y]) elsePanel1.Caption := Format('%d, %d', [X, Y]);List.Free;end;{向 Panel1 发送 WM_MOUSEMOVE 消息}{第一个消息参数是 0, 表示没有按任何辅助键}{第二个消息参数是 0, 相当于把鼠标移动到(0,0)坐标}procedure TForm1.Button1Click(Sender: TObject);beginPanel1.Perform(WM_MOUSEMOVE, 0, 0);end;{向 Panel1 发送 WM_MOUSEMOVE 消息}{第二个消息参数在 WM_MOUSEMOVE 消息中表示鼠标坐标位置; 参数是32位整数, 低16位是X, 高16位是Y}{这里给的坐标是控件的中心点}procedure TForm1.Button2Click(Sender: TObject);varx,y,LParam: Integer;beginx := Panel1.ClientWidth div2;y := Panel1.ClientHeight div2;LParam := y shl16or x;Panel1.Perform(WM_MOUSEMOVE, 0, LParam);end;{向 Panel1 发送 WM_MOUSEMOVE 消息}{消息的第一个参数是表示正在按下哪个辅助键和鼠标的状态}{这里发送的消息是: 按着 Shift 键, 鼠标移动到 (20,10) 处}procedure TForm1.Button3Click(Sender: TObject);constx = 20;y = 10;beginPanel1.Perform(WM_MOUSEMOVE, MK_SHIFT, y shl16or x);end;end.响应 WM_LBUTTONDOWN 消息代码文件:unit Unit1;interfaceusesWindows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,Dialogs, StdCtrls;typeTForm1 = class(TForm)Memo1: TMemo;procedure FormCreate(Sender: TObject);procedure FormMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);protected{这句的输入方法: Ctrl+空格; 输入 moused 确认; 然后执行Shift+Ctrl+C}procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X: Integer; Y: Integer); override;{这句的输入方法: Ctrl+空格; 输入 wmlbuttondo 确认; 然后执行Shift+Ctrl+C}procedure WMLButtonDown(var Message: TWMLButtonDown); messageWM_LBUTTONDOWN;end;varForm1: TForm1;implementation{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);beginMemo1.Clear;end;{响应 WM_LBUTTONDOWN 消息的方法 1: OnMouseDown 事件}procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);beginMemo1.Lines.Add('OnMouseDown');end;{响应 WM_LBUTTONDOWN 消息的方法 2: 覆盖父类的 MouseDown 方法} procedure TForm1.MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer);begininherited; {可以去掉或下移这句再试}Memo1.Lines.Add('MouseDown');end;{响应 WM_LBUTTONDOWN 消息的方法 3: 直接使用消息方法}procedure TForm1.WMLButtonDown(var Message: TWMLButtonDown);beginMemo1.Lines.Add('WM_LBUTTONDOWN');inherited; {可以去掉或上移这句再试}end;end.学习 Message(4): 通过Application.OnMessage 或TApplicationEvents 响应消息通过 Application.OnMessage 响应消息:interfaceusesWindows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,Dialogs, StdCtrls;typeTForm1 = class(TForm)Memo1: TMemo;procedure FormCreate(Sender: TObject);{这个自定义过程要复合 Application.OnMessage 的参数格式}procedure MyMessage(var Msg: tagMSG; var Handled: Boolean); end;varForm1: TForm1;implementation{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);beginMemo1.Clear;Application.OnMessage := MyMessage; {让 Application.OnMessage 执行自定义过程}end;{响应 WM_MOUSEMOVE 以外的所有消息}procedure TForm1.MyMessage(var Msg: tagMSG; var Handled: Boolean); beginif Msg.message <> WM_MOUSEMOVE thenMemo1.Lines.Add('$' + IntToHex(Msg.message, 4));end;end.通过 TApplicationEvents 响应消息, 需要在设计时添加 TApplicationEvents 组件, 并给它添加 OnMessage 事件:interfaceusesWindows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,Dialogs, StdCtrls, AppEvnts;typeTForm1 = class(TForm)Memo1: TMemo;ApplicationEvents1: TApplicationEvents;procedure FormCreate(Sender: TObject);procedure ApplicationEvents1Message(var Msg: tagMSG; var Handled: Boolean);end;varForm1: TForm1;implementation{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);beginMemo1.Clear;end;{响应 WM_MOUSEMOVE 以外的所有消息}procedure TForm1.ApplicationEvents1Message(var Msg: tagMSG;var Handled: Boolean);beginif Msg.message <> WM_MOUSEMOVE thenMemo1.Lines.Add('$' + IntToHex(Msg.message, 4));end;end.学习 Message(5): 关于TApplicationEvents.OnMessage 的第二个参数TApplicationEvents.OnMessage 的第二个参数 Handled 如果是 True, 表示消息已经处理过了, 到此为止.unit Unit1;interfaceusesWindows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,Dialogs, AppEvnts, StdCtrls;typeTForm1 = class(TForm)Memo1: TMemo;ApplicationEvents1: TApplicationEvents;procedure ApplicationEvents1Message(var Msg: tagMSG; var Handled: Boolean);procedure FormMouseDown(Sender: TObject; Button: TMouseButton;Shift: TShiftState; X, Y: Integer);end;varForm1: TForm1;implementation{$R *.dfm}procedure TForm1.ApplicationEvents1Message(var Msg: tagMSG;var Handled: Boolean);beginif Msg.message = WM_LBUTTONDOWN thenbeginMemo1.Lines.Add('OnMessage');Handled := False; {Handled 默认是 False, 这句可以省略}// Handled := True; {如果这样, 下面 OnMouseDown 事件将不会得到执行}end;end;procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);beginMemo1.Lines.Add('OnMouseDown');end;end.我们可以利用这个特性来屏蔽一些消息, 譬如给 TWebBrowser 屏蔽右键菜单: unit Unit1;interfaceusesWindows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,Dialogs, OleCtrls, SHDocVw, AppEvnts;typeTForm1 = class(TForm)WebBrowser1: TWebBrowser;ApplicationEvents1: TApplicationEvents;procedure FormCreate(Sender: TObject);procedure ApplicationEvents1Message(var Msg: tagMSG; var Handled: Boolean);end;varForm1: TForm1;implementation{$R *.dfm}procedure TForm1.ApplicationEvents1Message(var Msg: tagMSG;var Handled: Boolean);beginif IsChild(WebBrowser1.Handle, Msg.hwnd) and (Msg.message =WM_RBUTTONDOWN) thenbeginHandled := True;end;end;procedure TForm1.FormCreate(Sender: TObject);beginWindowState := wsMaximized;WebBrowser1.Align := alTop;WebBrowser1.Navigate('/del/');end;end.学习 Message(6): 也可以直接覆盖祖先类的 WndProc 或 Dispatch 方法来响应消息覆盖: WndProcunit Unit1;interfaceusesWindows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,Dialogs;typeTForm1 = class(TForm)protectedprocedure WndProc(var Message: TMessage); override;end;varForm1: TForm1;implementation{$R *.dfm}procedure TForm1.WndProc(var Message: TMessage);begininherited;if Message.Msg = WM_LBUTTONDOWN thenShowMessage('WndProc');end;end.覆盖: Dispatchunit Unit1;interfaceusesWindows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,Dialogs;typeTForm1 = class(TForm)publicprocedure Dispatch(var Message); override;end;varForm1: TForm1;implementation{$R *.dfm}procedure TForm1.Dispatch(var Message);begininherited;if TMessage(Message).Msg = WM_LBUTTONDOWN thenShowMessage('Dispatch');end;end.学习 Message(7): OnMessage 只相应消息队列中的消息Perform、SendMessage 会直接发送消息到窗体过程;PostMessage 是把消息放入消息队列.因为 Application.OnMessage 只接收队列中的消息,所以 Perform、SendMessage 发送的消息, OnMessage 收不到.测试如下:代码文件:unit Unit1;interfaceusesWindows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,Dialogs, AppEvnts, StdCtrls;typeTForm1 = class(TForm)ApplicationEvents1: TApplicationEvents;Button1: TButton;Button2: TButton;Button3: TButton;procedure ApplicationEvents1Message(var Msg: tagMSG; var Handled: Boolean);procedure Button1Click(Sender: TObject);procedure Button2Click(Sender: TObject);procedure Button3Click(Sender: TObject);end;varForm1: TForm1;implementation{$R *.dfm}{通过 ApplicationEvents1.OnMessage 接受鼠标双击窗体的消息; 可以双击窗体一试}procedure TForm1.ApplicationEvents1Message(var Msg: tagMSG;var Handled: Boolean);beginif Msg.message = WM_LBUTTONDBLCLK thenbeginShowMessage('WM_LBUTTONDBLCLK');Handled := True;end;end;{通过 Perform 向窗体发送 WM_LBUTTONDBLCLK 消息; OnMessage 收不到} procedure TForm1.Button1Click(Sender: TObject);beginSelf.Perform(WM_LBUTTONDBLCLK, 0, 0);end;{通过 SendMessage 向窗体发送 WM_LBUTTONDBLCLK 消息; OnMessage 收不到} procedure TForm1.Button2Click(Sender: TObject);beginSendMessage(Self.Handle, WM_LBUTTONDBLCLK, 0, 0);end;{通过 PostMessage 向窗体发送 WM_LBUTTONDBLCLK 消息; OnMessage 可以收到}procedure TForm1.Button3Click(Sender: TObject);beginPostMessage(Self.Handle, WM_LBUTTONDBLCLK, 0, 0);end;end.学习 Message(8): 使用不同的消息结构下面是同样的四段程序, 但分别使用了不同的三种消息结构: TWMMouseMove、TWMMouse、TMessageunit Unit1;interfaceusesWindows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,Dialogs;typeTForm1 = class(TForm)protectedprocedure WMMouseMove(var Message: TWMMouseMove); messageWM_MOUSEMOVE;end;varForm1: TForm1;implementation{$R *.dfm}procedure TForm1.WMMouseMove(var Message: TWMMouseMove);varx,y: Integer;beginx := Message.XPos;y := Message.YPos;Text := Format('%d, %d', [x,y]);end;end.unit Unit1;interfaceusesWindows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,Dialogs;typeTForm1 = class(TForm)protectedprocedure WMMouseMove(var Message: TWMMouse); message WM_MOUSEMOVE; end;varForm1: TForm1;implementation{$R *.dfm}procedure TForm1.WMMouseMove(var Message: TWMMouse);varx,y: Integer;beginx := Message.XPos;y := Message.YPos;Text := Format('%d, %d', [x,y]);end;end.unit Unit1;interfaceusesWindows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,Dialogs;typeTForm1 = class(TForm)protectedprocedure WMMouseMove(var Message: TMessage); message WM_MOUSEMOVE; end;varForm1: TForm1;implementation{$R *.dfm}procedure TForm1.WMMouseMove(var Message: TMessage);varx,y: Integer;beginx := Message.LParamLo;y := Message.LParamHi;Text := Format('%d, %d', [x,y]);end;end.学习 Message(8): 使用不同的消息结构下面是同样的四段程序, 但分别使用了不同的三种消息结构: TWMMouseMove、TWMMouse、TMessageunit Unit1;interfaceusesWindows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,Dialogs;typeTForm1 = class(TForm)protectedprocedure WMMouseMove(var Message: TWMMouseMove); messageWM_MOUSEMOVE;end;varForm1: TForm1;implementation{$R *.dfm}procedure TForm1.WMMouseMove(var Message: TWMMouseMove);varx,y: Integer;beginx := Message.XPos;y := Message.YPos;Text := Format('%d, %d', [x,y]);end;end.unit Unit1;interfaceusesWindows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,Dialogs;typeTForm1 = class(TForm)protectedprocedure WMMouseMove(var Message: TWMMouse); message WM_MOUSEMOVE; end;varForm1: TForm1;implementation{$R *.dfm}procedure TForm1.WMMouseMove(var Message: TWMMouse);varx,y: Integer;beginx := Message.XPos;y := Message.YPos;Text := Format('%d, %d', [x,y]);end;end.unit Unit1;interfaceusesWindows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,Dialogs;typeTForm1 = class(TForm)protectedprocedure WMMouseMove(var Message: TMessage); message WM_MOUSEMOVE; end;varForm1: TForm1;implementation{$R *.dfm}procedure TForm1.WMMouseMove(var Message: TMessage);varx,y: Integer;beginx := Message.LParamLo;y := Message.LParamHi;Text := Format('%d, %d', [x,y]);end;end.1、TMsg 吻合系统消息结构; TMessage 方便VCL 使用.2、它们不一样; 但信息可以沟通.3、TMessage 可以兼容VCL 中的其他消息结构; TMsg 不兼容.4、因为Delphi 能够方便地使用API, TMsg 在Delphi 中也是回避不了的, 譬如:OnMessage 事件的消息参数就是TMsg 类型的.学习 Message(10): WM_CancelMode 消息测试启动模式窗口, 当前窗口会收到一条 WM_CancelMode 消息; 改消息无参数.本例效果图:代码文件:unit Unit1;interfaceusesWindows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,Dialogs, StdCtrls;typeTForm1 = class(TForm)Memo1: TMemo;Button1: TButton;Button2: TButton;Button3: TButton;Button4: TButton;procedure FormCreate(Sender: TObject);procedure Button1Click(Sender: TObject);procedure Button2Click(Sender: TObject);procedure Button3Click(Sender: TObject);procedure Button4Click(Sender: TObject);protectedprocedure WMCancelMode(var Message: TWMCancelMode); message WM_CANCELMODE;end;varForm1: TForm1;implementation{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);beginMemo1.Align := alLeft;Memo1.ScrollBars := ssVertical;Memo1.Clear;Position := poScreenCenter;end;procedure TForm1.Button1Click(Sender: TObject);beginShowMessage('ShowMessage');end;procedure TForm1.Button2Click(Sender: TObject);beginInputBox('InputBox','Input','');end;procedure TForm1.Button3Click(Sender: TObject);beginMessageBox(Handle, 'Test', 'MessageBox', 0);end;procedure TForm1.Button4Click(Sender: TObject);varform: TForm;beginform := TForm.Create(Self);form.Caption := 'Test';form.Width := Width div2;form.Height := Height div2;form.Position := poDesktopCenter;form.ShowModal;end;procedure TForm1.WMCancelMode(var Message: TWMCancelMode);beginMemo1.Lines.Add('收到 WMCancelMode 消息');Message.Result := 0;end;end.学习 Message(11): 测试 TWMMouse 结构相关的鼠标消息和 TWMMouse 一致的消息结构有:TWMLButtonDblClk {左键双击}TWMLButtonDown {左键按下}TWMLButtonUp {左键抬起}TWMMButtonDblClk {中键双击}TWMMButtonDown {中键按下}TWMMButtonUp {中键抬起}TWMMouseMove {鼠标移动}TWMRButtonDblClk {右键双击}TWMRButtonDown {右键按下}TWMRButtonUp {右键抬起}代码文件:unit Unit1;interfaceusesWindows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,Dialogs, StdCtrls;typeTForm1 = class(TForm)Memo1: TMemo;CheckBox1: TCheckBox;Button1: TButton;procedure FormCreate(Sender: TObject);procedure Button1Click(Sender: TObject);protectedprocedure WMLButtonDblClk(var Message: TWMLButtonDblClk); message WM_LBUTTONDBLCLK;procedure WMLButtonDown(var Message: TWMLButtonDown); messageWM_LBUTTONDOWN;procedure WMLButtonUp(var Message: TWMLButtonUp); messageWM_LBUTTONUP;procedure WMMButtonDblClk(var Message: TWMMButtonDblClk); message WM_MBUTTONDBLCLK;procedure WMMButtonDown(var Message: TWMMButtonDown); messageWM_MBUTTONDOWN;procedure WMMButtonUp(var Message: TWMMButtonUp); messageWM_MBUTTONUP;procedure WMMouseMove(var Message: TWMMouseMove); messageWM_MOUSEMOVE;procedure WMRButtonDblClk(var Message: TWMRButtonDblClk); message WM_RBUTTONDBLCLK;procedure WMRButtonDown(var Message: TWMRButtonDown); messageWM_RBUTTONDOWN;procedure WMRButtonUp(var Message: TWMRButtonUp); messageWM_RBUTTONUP;end;varForm1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);beginMemo1.Clear;end;procedure TForm1.FormCreate(Sender: TObject);beginMemo1.Align := alLeft;Memo1.ScrollBars := ssVertical;Memo1.Clear;Position := poScreenCenter;CheckBox1.Caption := '接受鼠标移动消息';Button1.Caption := '清空列表';end;procedure TForm1.WMLButtonDblClk(var Message: TWMLButtonDblClk); varx,y: Integer;s: string;beginx := Message.XPos;y := Message.YPos;s := Format('鼠标左键双击: %d,%d', [x,y]);Memo1.Lines.Add(s);Message.Result := 0;end;procedure TForm1.WMLButtonDown(var Message: TWMLButtonDown);varx,y: Integer;s: string;beginx := Message.XPos;y := Message.YPos;s := Format('鼠标左键按下: %d,%d', [x,y]);Memo1.Lines.Add(s);Message.Result := 0;end;procedure TForm1.WMLButtonUp(var Message: TWMLButtonUp);varx,y: Integer;s: string;beginx := Message.XPos;y := Message.YPos;s := Format('鼠标左键抬起: %d,%d', [x,y]);Memo1.Lines.Add(s);Message.Result := 0;end;procedure TForm1.WMMButtonDblClk(var Message: TWMMButtonDblClk); varx,y: Integer;s: string;beginx := Message.XPos;y := Message.YPos;s := Format('鼠标中键双击: %d,%d', [x,y]);Memo1.Lines.Add(s);Message.Result := 0;end;procedure TForm1.WMMButtonDown(var Message: TWMMButtonDown);varx,y: Integer;s: string;beginx := Message.XPos;y := Message.YPos;s := Format('鼠标中键按下: %d,%d', [x,y]);Memo1.Lines.Add(s);Message.Result := 0;end;procedure TForm1.WMMButtonUp(var Message: TWMMButtonUp);varx,y: Integer;s: string;beginx := Message.XPos;y := Message.YPos;s := Format('鼠标中键抬起: %d,%d', [x,y]);Memo1.Lines.Add(s);Message.Result := 0;end;procedure TForm1.WMMouseMove(var Message: TWMMouseMove);varx,y: Integer;s: string;beginif not CheckBox1.Checked then Exit;x := Message.XPos;y := Message.YPos;s := Format('鼠标移动: %d,%d', [x,y]);Memo1.Lines.Add(s);Message.Result := 0;end;procedure TForm1.WMRButtonDblClk(var Message: TWMRButtonDblClk); varx,y: Integer;s: string;beginx := Message.XPos;y := Message.YPos;s := Format('鼠标右键双击: %d,%d', [x,y]);Memo1.Lines.Add(s);Message.Result := 0;end;procedure TForm1.WMRButtonDown(var Message: TWMRButtonDown);varx,y: Integer;s: string;beginx := Message.XPos;y := Message.YPos;s := Format('鼠标右键按下: %d,%d', [x,y]);Memo1.Lines.Add(s);Message.Result := 0;end;procedure TForm1.WMRButtonUp(var Message: TWMRButtonUp);varx,y: Integer;s: string;beginx := Message.XPos;y := Message.YPos;s := Format('鼠标右键抬起: %d,%d', [x,y]);Memo1.Lines.Add(s);Message.Result := 0;end;end.学习 Message(11): 测试 TWMMouse 结构相关的鼠标消息和 TWMMouse 一致的消息结构有:TWMLButtonDblClk {左键双击}TWMLButtonDown {左键按下}TWMLButtonUp {左键抬起}TWMMButtonDblClk {中键双击}TWMMButtonDown {中键按下}TWMMButtonUp {中键抬起}TWMMouseMove {鼠标移动}TWMRButtonDblClk {右键双击}TWMRButtonDown {右键按下}TWMRButtonUp {右键抬起}代码文件:unit Unit1;interfaceusesWindows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,Dialogs, StdCtrls;typeTForm1 = class(TForm)Memo1: TMemo;CheckBox1: TCheckBox;Button1: TButton;procedure FormCreate(Sender: TObject);procedure Button1Click(Sender: TObject);protectedprocedure WMLButtonDblClk(var Message: TWMLButtonDblClk); message WM_LBUTTONDBLCLK;procedure WMLButtonDown(var Message: TWMLButtonDown); message WM_LBUTTONDOWN;procedure WMLButtonUp(var Message: TWMLButtonUp); messageWM_LBUTTONUP;procedure WMMButtonDblClk(var Message: TWMMButtonDblClk); message WM_MBUTTONDBLCLK;procedure WMMButtonDown(var Message: TWMMButtonDown); message WM_MBUTTONDOWN;procedure WMMButtonUp(var Message: TWMMButtonUp); messageWM_MBUTTONUP;procedure WMMouseMove(var Message: TWMMouseMove); messageWM_MOUSEMOVE;procedure WMRButtonDblClk(var Message: TWMRButtonDblClk); message WM_RBUTTONDBLCLK;procedure WMRButtonDown(var Message: TWMRButtonDown); message WM_RBUTTONDOWN;procedure WMRButtonUp(var Message: TWMRButtonUp); messageWM_RBUTTONUP;end;varForm1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);beginMemo1.Clear;end;procedure TForm1.FormCreate(Sender: TObject);beginMemo1.Align := alLeft;Memo1.ScrollBars := ssVertical;Memo1.Clear;Position := poScreenCenter;CheckBox1.Caption := '接受鼠标移动消息';Button1.Caption := '清空列表';end;procedure TForm1.WMLButtonDblClk(var Message: TWMLButtonDblClk); varx,y: Integer;s: string;beginx := Message.XPos;y := Message.YPos;s := Format('鼠标左键双击: %d,%d', [x,y]);Memo1.Lines.Add(s);Message.Result := 0;end;procedure TForm1.WMLButtonDown(var Message: TWMLButtonDown);varx,y: Integer;s: string;beginx := Message.XPos;y := Message.YPos;s := Format('鼠标左键按下: %d,%d', [x,y]);Memo1.Lines.Add(s);Message.Result := 0;end;procedure TForm1.WMLButtonUp(var Message: TWMLButtonUp);varx,y: Integer;s: string;beginx := Message.XPos;y := Message.YPos;s := Format('鼠标左键抬起: %d,%d', [x,y]);Memo1.Lines.Add(s);Message.Result := 0;end;procedure TForm1.WMMButtonDblClk(var Message: TWMMButtonDblClk); varx,y: Integer;s: string;beginx := Message.XPos;y := Message.YPos;s := Format('鼠标中键双击: %d,%d', [x,y]);Memo1.Lines.Add(s);Message.Result := 0;end;procedure TForm1.WMMButtonDown(var Message: TWMMButtonDown);varx,y: Integer;s: string;beginx := Message.XPos;y := Message.YPos;s := Format('鼠标中键按下: %d,%d', [x,y]);Memo1.Lines.Add(s);Message.Result := 0;end;procedure TForm1.WMMButtonUp(var Message: TWMMButtonUp);varx,y: Integer;s: string;beginx := Message.XPos;y := Message.YPos;s := Format('鼠标中键抬起: %d,%d', [x,y]);Memo1.Lines.Add(s);Message.Result := 0;end;procedure TForm1.WMMouseMove(var Message: TWMMouseMove);varx,y: Integer;s: string;beginif not CheckBox1.Checked then Exit;x := Message.XPos;y := Message.YPos;s := Format('鼠标移动: %d,%d', [x,y]);Memo1.Lines.Add(s);Message.Result := 0;end;procedure TForm1.WMRButtonDblClk(var Message: TWMRButtonDblClk); varx,y: Integer;s: string;beginx := Message.XPos;y := Message.YPos;s := Format('鼠标右键双击: %d,%d', [x,y]);Memo1.Lines.Add(s);Message.Result := 0;end;procedure TForm1.WMRButtonDown(var Message: TWMRButtonDown); varx,y: Integer;s: string;beginx := Message.XPos;y := Message.YPos;s := Format('鼠标右键按下: %d,%d', [x,y]);Memo1.Lines.Add(s);Message.Result := 0;end;procedure TForm1.WMRButtonUp(var Message: TWMRButtonUp);varx,y: Integer;s: string;beginx := Message.XPos;y := Message.YPos;s := Format('鼠标右键抬起: %d,%d', [x,y]);Memo1.Lines.Add(s);Message.Result := 0;end;end.学习 Message(13): 解析TWMMouse.Keys 参数本例效果图:代码文件:unit Unit1;interfaceusesWindows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,Dialogs;typeTForm1 = class(TForm)protectedprocedure MyMouseDown(Msg: TWMMouse);procedure WMLButtonDown(var Message: TWMLButtonDown); messageWM_LBUTTONDOWN;procedure WMMButtonDown(var Message: TWMMButtonDown); messageWM_MBUTTONDOWN;procedure WMRButtonDown(var Message: TWMRButtonDown); messageWM_RBUTTONDOWN;end;varForm1: TForm1;implementation{$R *.dfm}procedure TForm1.MyMouseDown(Msg: TWMMouse);varList: TStringList;beginList := TStringList.Create;if Msg.Keys and MK_SHIFT <> 0then List.Add('Shift');if Msg.Keys and MK_CONTROL <> 0then List.Add('Ctrl');if GetKeyState(VK_MENU) < 0then List.Add('Alt');if Msg.Keys and MK_LBUTTON <> 0then List.Add('Left');if Msg.Keys and MK_MBUTTON <> 0then List.Add('Middle');if Msg.Keys and MK_RBUTTON <> 0then List.Add('Right');Canvas.TextOut(Msg.XPos, Msg.YPos, maText);List.Free;end;procedure TForm1.WMLButtonDown(var Message: TWMLButtonDown);beginMyMouseDown(Message);end;procedure TForm1.WMMButtonDown(var Message: TWMMButtonDown);beginMyMouseDown(Message);end;procedure TForm1.WMRButtonDown(var Message: TWMRButtonDown);beginMyMouseDown(Message);end;end.学习 Message(14): 区分左右 Shift、Ctrl、Alt代码文件:unit Unit1;。