Delphi SendMessage 使用

查找另外一个窗口的句柄: handle := FindWindow(nil,PChar('窗口的标题'));//查到窗体句柄

查找子窗体:childHandle := FindWindowEx(handle,0,'子窗体类','子窗体标题'); 

SendMessage(TreeView.Handle,TVM_SETBKCOLOR,0,RGB(255,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_RADIOBUTTON,1);  改变Button风格
SendMessage(ComboBox.Handle,CB_SETDropPEDWIDTH,300,0);  改变CBDownWidth SendMessage这个函数有很多奇妙的用途
举例:
1,获得MEMO中的光标位置.
Lops:=SendMessage(memol.Handle,EM_LINEFROMCHAR,Memol.SelStart,0);//光标所在的行号
Cpos:=SendMessage(meno1.Handle,EM_LINEINDEX,Lpos,0);//光标所在的字符位置
LineLength:=SendMessage(memol.handle,EM_LINELENGTH,Cpos,0);//这行的字符数.
2,开关显示器.
SendMessage(Handle, WM_SYSCOMMAND, SC_MONITORPOWER,2);//关闭显示器.
SendMessage(Handle, WM_SYSCOMMAND, SC_MONITORPOWER, -1);//打开显示器.
3,有人问我如何让Memo翻页,我试了一下,
Next: 
sendmessage(memo1.handle,wm_keydown,VK_NEXT,-1); 
sendmessage(memo1.handle,wm_keyup,VK_NEXT,-1); 
Prev: 
sendmessage(memo1.handle,wm_keydown,VK_PRIOR,-1); 
sendmessage(memo1.handle,wm_keyup,VK_PRIOR,-1); VK_PRIOR Page Up 键 0x21
VK_NEXT Page Down 键 0x22
VK_END End 键 0x23
VK_HOME Home 键 0x24
VK_LEFT 左箭头键 0x25
VK_UP 上箭头键 0x26
VK_RIGHT 右箭头键 0x27
VK_DOWN 下箭头键 0x28
具体例子unit Unit1; 

interface 

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

type
TForm1 = 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;
begin
h:= FindWindow(nil,'abc.txt - 记事本');
h:= FindWindowEx(h,0,'edit',nil);
SendMessage(h,WM_SETTEXT,255,Integer(PChar('我来测度了')));
ShowMessage( IntToStr(h));
end;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值