如何用发消息的方法模拟按下组合键?(条件是不使用keybd_event方法) ( 积分: 100 )

  • 主题发起人 主题发起人 DF7C3
  • 开始时间 开始时间
D

DF7C3

Unregistered / Unconfirmed
GUEST, unregistred user!
我想用发消息的方法来引发DBGridEh的ctrl&nbsp;+&nbsp;F&nbsp;搜索。<br><br>[red]我知道用keybd_event的方法可以实现。[/red]<br><br>我只是想弄明白用sendmessage和postmessage的方法能否实现发送组合键,<br>如果能够的话,如何实现,谢谢!
 
sendmessage和postmessage<br>不能发送。<br>用注册热键的方法,行得通。
 
Type<br>&nbsp;&nbsp;TSMsg&nbsp;=&nbsp;Packed&nbsp;Record<br>&nbsp;&nbsp;&nbsp;&nbsp;Shift:&nbsp;TShiftState;<br>&nbsp;&nbsp;&nbsp;&nbsp;Key:&nbsp;Word;<br>&nbsp;&nbsp;End;<br><br>Procedure&nbsp;SendMsg(AhWnd:&nbsp;Hwnd;&nbsp;M:&nbsp;TSMsg);<br>Begin<br>&nbsp;&nbsp;If&nbsp;ssShift&nbsp;In&nbsp;M.Shift&nbsp;Then<br>&nbsp;&nbsp;&nbsp;&nbsp;PostMessage(AhWnd,&nbsp;WM_KEYDOWN,&nbsp;VK_SHIFT,&nbsp;0);<br>&nbsp;&nbsp;If&nbsp;ssAlt&nbsp;In&nbsp;M.Shift&nbsp;Then<br>&nbsp;&nbsp;&nbsp;&nbsp;PostMessage(AhWnd,&nbsp;WM_KEYDOWN,&nbsp;VK_MENU,&nbsp;0);<br>&nbsp;&nbsp;If&nbsp;ssCtrl&nbsp;In&nbsp;M.Shift&nbsp;Then<br>&nbsp;&nbsp;&nbsp;&nbsp;PostMessage(AhWnd,&nbsp;WM_KEYDOWN,&nbsp;VK_CONTROL,&nbsp;0);<br><br>&nbsp;&nbsp;PostMessage(AhWnd,&nbsp;WM_KEYDOWN,&nbsp;M.Key,&nbsp;0);<br>&nbsp;&nbsp;PostMessage(AhWnd,&nbsp;WM_KEYUP,&nbsp;M.Key,&nbsp;0);<br><br>&nbsp;&nbsp;If&nbsp;ssShift&nbsp;In&nbsp;M.Shift&nbsp;Then<br>&nbsp;&nbsp;&nbsp;&nbsp;PostMessage(AhWnd,&nbsp;WM_KEYUP,&nbsp;VK_SHIFT,&nbsp;0);<br>&nbsp;&nbsp;If&nbsp;ssAlt&nbsp;In&nbsp;M.Shift&nbsp;Then<br>&nbsp;&nbsp;&nbsp;&nbsp;PostMessage(AhWnd,&nbsp;WM_KEYUP,&nbsp;VK_MENU,&nbsp;0);<br>&nbsp;&nbsp;If&nbsp;ssCtrl&nbsp;In&nbsp;M.Shift&nbsp;Then<br>&nbsp;&nbsp;&nbsp;&nbsp;PostMessage(AhWnd,&nbsp;WM_KEYUP,&nbsp;VK_CONTROL,&nbsp;0);<br>End;<br><br>Function&nbsp;EnumChildWndProc(AhWnd:&nbsp;Hwnd;&nbsp;AlParam:&nbsp;Pointer):&nbsp;Boolean;&nbsp;Stdcall;<br>Begin//遍历窗口句柄内的所有控件并发送键消息<br>&nbsp;&nbsp;SendMsg(AhWnd,&nbsp;TSMsg(AlParam^));<br>&nbsp;&nbsp;result&nbsp;:=&nbsp;true;<br>End;<br><br><br>定时发送<br>Procedure&nbsp;TFrmMain.TimerTimer(Sender:&nbsp;TObject);<br>Var<br>&nbsp;&nbsp;M:&nbsp;TSMsg;<br>&nbsp;&nbsp;ThreadID:&nbsp;DWord;<br>Begin<br>&nbsp;&nbsp;Inc(CurV);<br>&nbsp;&nbsp;If&nbsp;CurV&nbsp;=&nbsp;Up.IntValue&nbsp;Then<br>&nbsp;&nbsp;Begin<br>&nbsp;&nbsp;&nbsp;&nbsp;CurV&nbsp;:=&nbsp;0;<br>&nbsp;&nbsp;&nbsp;&nbsp;ShortCutToKey(HotKey2.HotKey,&nbsp;M.Key,&nbsp;M.Shift);<br>&nbsp;&nbsp;&nbsp;&nbsp;If&nbsp;ChkGol.Checked&nbsp;Then<br>&nbsp;&nbsp;&nbsp;&nbsp;Begin<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ThreadID&nbsp;:=&nbsp;GetWindowThreadProcessId(Hwd,&nbsp;Nil);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AttachThreadInput(GetCurrentThreadId,&nbsp;ThreadID,&nbsp;true);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//ShowWindow(Hwd,&nbsp;SW_SHOWNOACTIVATE);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SetForegroundWindow(Hwd);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;If&nbsp;ssShift&nbsp;In&nbsp;M.Shift&nbsp;Then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;keybd_event(VK_SHIFT,&nbsp;MapVirtualKey(VK_SHIFT,&nbsp;0),&nbsp;0,&nbsp;0);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;If&nbsp;ssAlt&nbsp;In&nbsp;M.Shift&nbsp;Then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;keybd_event(VK_MENU,&nbsp;MapVirtualKey(VK_MENU,&nbsp;0),&nbsp;0,&nbsp;0);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;If&nbsp;ssCtrl&nbsp;In&nbsp;M.Shift&nbsp;Then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;keybd_event(VK_CONTROL,&nbsp;MapVirtualKey(VK_CONTROL,&nbsp;0),&nbsp;0,&nbsp;0);<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;keybd_event(M.Key,&nbsp;MapVirtualKey(M.Key,&nbsp;0),&nbsp;0,&nbsp;0);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;keybd_event(M.Key,&nbsp;MapVirtualKey(M.Key,&nbsp;0),&nbsp;KEYEVENTF_KEYUP,&nbsp;0);<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;If&nbsp;ssShift&nbsp;In&nbsp;M.Shift&nbsp;Then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;keybd_event(VK_SHIFT,&nbsp;MapVirtualKey(VK_SHIFT,&nbsp;0),&nbsp;KEYEVENTF_KEYUP,&nbsp;0);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;If&nbsp;ssAlt&nbsp;In&nbsp;M.Shift&nbsp;Then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;keybd_event(VK_MENU,&nbsp;MapVirtualKey(VK_MENU,&nbsp;0),&nbsp;KEYEVENTF_KEYUP,&nbsp;0);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;If&nbsp;ssCtrl&nbsp;In&nbsp;M.Shift&nbsp;Then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;keybd_event(VK_CONTROL,&nbsp;MapVirtualKey(VK_CONTROL,&nbsp;0),&nbsp;KEYEVENTF_KEYUP,&nbsp;0);<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AttachThreadInput(GetCurrentThreadId,&nbsp;ThreadID,&nbsp;False);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ShowWindow(Hwd,&nbsp;SW_RESTORE);<br>&nbsp;&nbsp;&nbsp;&nbsp;End&nbsp;Else<br>&nbsp;&nbsp;&nbsp;&nbsp;Begin<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SendMsg(Hwd,&nbsp;M);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;EnumChildWindows(Hwd,&nbsp;@EnumChildWndProc,&nbsp;LongInt(@M));<br>&nbsp;&nbsp;&nbsp;&nbsp;End;<br>&nbsp;&nbsp;End;<br>End;
 
begin&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{按下A键}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Edit1.SetFocus;<br>&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;keybd_event(VK_SHIFT,&nbsp;0,&nbsp;0,&nbsp;0);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;keybd_event(ord('A'),&nbsp;0,&nbsp;0,&nbsp;0);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//keybd_event(VK_SHIFT,&nbsp;0,&nbsp;KEYEVENTF_KEYUP,&nbsp;0);<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{按下左Window键然后选择“运行”<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;keybd_event(VK_LWIN,&nbsp;0,&nbsp;0,&nbsp;0);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;keybd_event(ord('R'),&nbsp;0,&nbsp;0,&nbsp;0);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;keybd_event(VK_LWIN,&nbsp;0,&nbsp;KEYEVENTF_KEYUP,&nbsp;0);}<br>&nbsp;&nbsp;&nbsp;&nbsp;end;
 
可以用sendinput
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
759
import
I
D
回复
0
查看
1K
DelphiTeacher的专栏
D
后退
顶部