//发送快捷键到指定窗口
class procedure CWindowOperator.SendAccelerator(myHwnd, wID: integer);
begin
{
发送快捷键的消息是WM_COMMAND
WM_COMMAND的解释:
The WM_COMMAND message is sent when the user selects a command item from a menu, when a control sends a notification message to its parent window, or when an accelerator keystroke is translated.
WM_COMMAND
wNotifyCode = HIWORD(wParam); // notification code
wID = LOWORD(wParam); // item, control, or accelerator identifier
hwndCtl = (HWND) lParam; // handle of control
Parameters
wNotifyCode: Value of the high-order word of wParam. Specifies the notification code if the message is from a control. If the message is from an accelerator, this parameter is 1. If the message is from a menu, this parameter is 0.
wID: Value of the low-order word of wParam. Specifies the identifier of the menu item, control, or accelerator.
hwndCtl: Value of lParam. Identifies the control sending the message if the message is from a control. Otherwise, this parameter is NULL.
}
//设置Combobox选取items里的某一项
class procedure CWindowOperator.ComboboxSetCurSel(myHwnd,iIndex: integer);
var
myParentHwnd: integer;
begin
//改变Combobox当前选择项
SendMessage(myHwnd,CB_SETCURSEL,iIndex,0);
//模拟按下主菜单的菜单项
class procedure CWindowOperator.MenuClick(myHwnd, wID: integer);
begin
{
按下主菜单的菜单项的消息是WM_COMMAND
WM_COMMAND的解释:
The WM_COMMAND message is sent when the user selects a command item from a menu, when a control sends a notification message to its parent window, or when an accelerator keystroke is translated.
WM_COMMAND
wNotifyCode = HIWORD(wParam); // notification code
wID = LOWORD(wParam); // item, control, or accelerator identifier
hwndCtl = (HWND) lParam; // handle of control
Parameters
wNotifyCode: Value of the high-order word of wParam. Specifies the notification code if the message is from a control. If the message is from an accelerator, this parameter is 1. If the message is from a menu, this parameter is 0.
wID: Value of the low-order word of wParam. Specifies the identifier of the menu item, control, or accelerator.
hwndCtl: Value of lParam. Identifies the control sending the message if the message is from a control. Otherwise, this parameter is NULL.
}
SendMessage(myHwnd,WM_COMMAND,wID,0);
end;
class procedure CWindowOperator.ButtonClick(myHwnd: integer);
var
myParentHwnd: integer;
begin
//发送WM_LBUTTONDOWN(注,OnClick事件此句可以不要,有了此句按钮会多一个下陷的动作,焦点会在按钮上,同时按钮OnMouseDown、OnMouseUp事件会有响应)
//PostMessage(myHwnd,WM_LBUTTONDOWN,MK_LBUTTON,0);
class function CWindowOperator.GetWindowByStyle(hWindow, iStyle,iMaxText: integer): integer;
var
hMisdn :integer;
i :integer;
begin
result:= 0;
for i :=1 to 45 do
begin
hMisdn:=CWindowOperator.FindChildWindow(hWindow,'TCUIEdit','',i);
//iStyle=1409351872 iMaxText=15
if (GetWindowLong(hMisdn,GWL_STYLE)=iStyle) and (SendMessage(hMisdn,EM_GETLIMITTEXT,0,0)=iMaxText) then
begin
result:= hMisdn;
end;
end;
end;
class function ComparePosition(Item1, Item2: Pointer): Integer;
var
Window1_X,Window1_Y : integer;
Window2_X,Window2_Y : integer;
begin
Window1_X := CWindowProperty(Item1).m_Position.X;
Window1_Y := CWindowProperty(Item1).m_Position.Y;
Window2_X := CWindowProperty(Item2).m_Position.X;
Window2_Y := CWindowProperty(Item2).m_Position.Y;
//如果Y的位置不等,Y小的窗口(上面的窗口)排在前面
if Window1_Y < Window2_Y then
result := -1
else if Window1_Y > Window2_Y then
result := 1
else
begin
//如果Y的位置相等,X小的窗口(左边的窗口)排在前面
if Window1_X < Window2_X then
result := -1
else if Window1_X < Window2_X then
result := 1
else
result := 0;
end;
end;
{
//根据类名和标题查找窗口,返回窗口句柄
//参数:strClassName -- 窗口类名
// strWindowName -- 窗口标题
}
class function CWindowOperator.FindWindow(strClassName, strWindowName: string): THandle;
begin
if (strClassName = '') and (strWindowName <> '') then
result := Windows.FindWindow(nil,PChar(strWindowName))
else if (strWindowName = '') and (strClassName <> '') then
result := Windows.FindWindow(PChar(strClassName),nil)
else if (strWindowName <> '') and (strClassName <> '') then
result := Windows.FindWindow(PChar(strClassName),PChar(strWindowName))
else
result := 0;
end;
{
//查找指定类名的特定子窗口(包括子窗口的子窗口),返回子窗口句柄
//参数:hWndParent -- 父窗口句柄
// strClassName -- 子窗口类名(如果为空,表示不判断)
// strWindowName -- 子窗口标题(如果为空,表示不判断)
// iIndex -- 子窗体顺序号(按屏幕从上到下,从左到右的顺序),从0开始
}
class function CWindowOperator.FindChildWindow(hWndParent:THandle; strClassName,strWindowName:string; iIndex:integer): THandle;
var
myList : TList ;
AWindowProperty: CWindowProperty;
i : integer ;
begin
myList := TList.Create();
//2. 对myList中的窗口,删除ClassName <> strClassName 的窗口
strClassName := Uppercase(strClassName);
if strClassName <> '' then
begin
for i := myList.Count-1 downto 0 do
begin
AWindowProperty := CWindowProperty(myList.Items[i]);
if Uppercase(AWindowProperty.m_ClassName) <> strClassName then
begin
AWindowProperty.Free();
myList.Delete(i);
end;
end;
end;
//3. 对myList中的窗口,删除Caption <> strWindowName 的窗口
strWindowName := Uppercase(strWindowName);
if strWindowName <> '' then
begin
for i := myList.Count-1 downto 0 do
begin
AWindowProperty := CWindowProperty(myList.Items[i]);
if Uppercase(AWindowProperty.m_Caption) <> strWindowName then
begin
AWindowProperty.Free();
myList.Delete(i);
end;
end;
end;
if iIndex > myList.Count -1 then
begin
result := 0;
end
else
begin
//4. 对myList中的窗口,按照屏幕位置从上到下,从左到右排序
myList.Sort(@ComparePosition);
//5. 返回窗口句柄
result := CWindowProperty(myList.Items[iIndex]).m_hwnd;
end;
//释放myList
for i := myList.Count-1 downto 0 do
begin
AWindowProperty := CWindowProperty(myList.Items[i]);
AWindowProperty.Free();
myList.Delete(i);
end;
myList.Free();
end;
//取得窗口标题
class function CWindowOperator.GetWindowText(myHwnd: integer): string;
var
buf : array[0..256] of char;
begin
SendMessage(myHwnd,WM_GETTEXT,40,integer(@buf));
result := string(buf);
end;
//设置窗口标题
class procedure CWindowOperator.SetWindowText(myHwnd:integer; strText:string);
begin
SendMessage(myHwnd,WM_SETTEXT,0,LongInt(Pchar(strText)));
end;
//用指定文本替换编辑控件中的当前选定内容(edit,richedit,memo,ComboBox)
class procedure CWindowOperator.EditReplaceSel(myHwnd:integer; strText:string);
begin
SendMessage(myHwnd,EM_REPLACESEL,1,integer(PChar(strText))); //1--表示可以撤销,0--不可撤销
end;
CWindowOperator = class
public
class function FindWindow(strClassName,strWindowName:string):THandle;//根据类名和标题查找窗口,返回窗口句柄
//查找特定子窗口(包括子窗口的子窗口),返回子窗口句柄
//参数:hWndParent -- 父窗口句柄
// strClassName -- 子窗口类名(如果为空,表示不判断)
// strWindowName -- 子窗口标题(如果为空,表示不判断)
// iIndex -- 子窗体顺序号(按屏幕从上到下,从左到右的顺序),从0开始
class function FindChildWindow(hWndParent:THandle; strClassName,strWindowName:string; iIndex:integer):THandle;
class function GetWindowText(myHwnd:integer):string ;//取得窗口标题
class procedure SetWindowText(myHwnd:integer; strText:string);//设置窗口标题
class procedure EditReplaceSel(myHwnd:integer; strText:string);//用指定文本替换编辑控件中的当前选定内容(edit,richedit,memo,ComboBox,DBCombobox)
class procedure ComboboxSetCurSel(myHwnd:integer; iIndex:integer);//设置Combobox选取items里的某一项(DBCombobox不是从Combobox继承的,是从edit继承的,因此不行)
class procedure SendKey(myHwnd:integer; VKey:Word);//发送模拟键到指定窗口
class procedure SendEnterKey(myHwnd:integer);//发送回车到指定窗口
class procedure SendAccelerator(myHwnd:integer; wID:integer);//发送快捷键到指定窗口,myHwnd=窗口句柄,wID=快捷键id
class procedure ButtonClick(myHwnd:integer);//模拟按下按钮
class procedure MenuClick(myHwnd:integer; wID:integer);//模拟按下主菜单的菜单项,myHwnd窗口句柄,wID=菜单项id
class function GetWindowByStyle(hWindow:integer;iStyle:integer;iMaxText:integer):integer;//Coded by Fr
end;
to naughtyboy(重归起跑线) :
我使窗体显示出来的办法是用的api函数findwindow和showwindow,所以仅仅得到的是窗体的句柄,现在的问题也就是:得到窗口的句柄之后如何才能判断窗体上有没有组件button1?下面是我的代码:请指教 !!
function myHookProc(nCode: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall;
var
handle : THandle;
i : integer;
begin
Result:= 0;
if nCode<0 then
begin
Result:= CallNextHookEx(MyHook, nCode, wParam, lParam);
Exit;
end
else
IF NCODE= HC_ACTION THEN
begin
lParam := 0;
if (wParam=ord('m')) or (wParam=ord('M')) or (wParam=vk_f10) then
begin
handle := findwindow('TForm1','Form1') ;
ShowWindow(handle,SW_Normal);
//得到Form1的句柄handle后,如何判断form1上是否有组件button1呢?
to skertone() :
我刚用你的程序测试了下,发现不还是不行的,代码如下:(先按Button2 hide form,在按f12或f10都没反映 ),我想还是要用键盘hook才行吧
procedure TForm1.FormCreate(Sender: TObject);
begin
HotKeyID := GlobalAddAtom('NewHotKeyRegister');
if HotKeyID <> 0 then
RegisterHotKey(Handle,HotKeyID,0,VK_F12)
end;
procedure TForm1.HotKeyPress(var Msg: TMessage);
begin
if (Msg. LParamHi = VK_F10) AND (Msg. LParamLo = 0) then
begin
button1.Click ;
end;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
UnregisterHotKey(Handle,HotKeyID);
DeleteAtom(HotKeyID);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Showmessage('sdfsdf');
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
hide;
end;
type
TForm1 = class(TForm)
Label1: TLabel;
Memo1: TMemo;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
HotKeyID: Integer;
procedure HotKeyPress(var Msg: TMessage);Message WM_HOTKEY;
public
{ Public declarations }
end;
var
Form1: TForm1;
ii: Integer = 1;
implementation
{$R *.DFM}
procedure TForm1.HotKeyPress(var Msg: TMessage);
var
bb: Boolean;
iTemp: Integer;
begin
if (Msg. LParamHi = VK_F12) AND (Msg. LParamLo = 0) then
begin
if FileExists('E:\Sierra\Counter-Strike\cstrike\pldecal.wad') then
if DeleteFile('E:\Sierra\Counter-Strike\cstrike\pldecal.wad') then
Memo1.Lines.Add('Delete CS-Logo');
if FileExists('E:\logo\'+ IntToStr(ii) + '.wad') then
if CopyFile(PChar('E:\Logo\'+ IntToStr(ii) + '.wad'),'E:\Sierra\Counter-Strike\cstrike\pldecal.wad',bb) then
Memo1.Lines.Add('Change CS-Logo' + IntToStr(ii) + '.wad');
Msg.Result := 1;
Inc(ii);
if ii > 3 then ii := 1;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
HotKeyID := GlobalAddAtom('NewHotKeyRegister');
if HotKeyID <> 0 then
RegisterHotKey(Handle,HotKeyID,0,VK_F12)
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
UnregisterHotKey(Handle,HotKeyID);
DeleteAtom(HotKeyID);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
HotKeyID := GlobalAddAtom('NewHotKeyRegister');
if HotKeyID <> 0 then
RegisterHotKey(Handle,HotKeyID,0,VK_F12)
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
UnregisterHotKey(Handle,HotKeyID);
DeleteAtom(HotKeyID);
end;
procedure TForm1.HotKeyPress(var Msg: TMessage);
begin
if (Msg. LParamHi = VK_F10) AND (Msg. LParamLo = 0) then
begin
Button1Click(nil);
不用发送消息
在你要按F10的窗口写:
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if key=121 then //F10
form2.button1.click;
end;