一个很老的经典问题!delphi发送组合键,欢迎大家来讨论。(100分)

  • 主题发起人 鸢都剑客
  • 开始时间

鸢都剑客

Unregistered / Unconfirmed
GUEST, unregistred user!
问题要求。<br>&nbsp; &nbsp;1.发送组合键(ALT+Z);<br>&nbsp; &nbsp;2.发送目标为外部程序,并且目标程序为非激活状态。(即目标程序不是当前窗口)。<br>注:大家可以用记事本测试。给它发关一个ALT+F看一下能否打开菜单。<br><br>另注:请大家自己先测试一下再贴代码。不要从网上随便搜个代码就来贴。先看好。<br>欢迎大家讨论。
 
我先发一下。<br>1.我在网上找的。好像是postmessage的最后一个参数,我试了各种方法都没成功。有做的来聊聊。
 
先找到窗口句柄 剩下就不说了
 
to :eek:nyliu 找到窗口句柄,已知了.后怎么做才行.关键的没说.<br>这是我试几种方法:GMHandle 是已知的.<br>方法一,不能功<br>&nbsp; PostMessage(GmHandle,WM_KEYDOWN,VK_MENU,0);<br>&nbsp; PostMessage(GmHandle,WM_KEYDOWN,90,0);<br>&nbsp; Sleep(100);<br>&nbsp; PostMessage(GmHandle,WM_KEYUP,90,0);<br>&nbsp; PostMessage(GmHandle,WM_KEYDOWN,VK_MENU,0);<br>方法二:不成功<br>&nbsp; PostThreadMessage(GmHandle,WM_SYSKEYDOWN,90,(1 shl 29));<br>&nbsp; 看到说,最后一个参数的第29们是1代表ALT键按下.<br>&nbsp; 可是测试不成功.
 
postmessage不行,要用keybd_event函数。还需要代码吗?
 
那就给你发个代码:<br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp; npad:HWND;<br>&nbsp; i,j:integer;<br>begin<br>&nbsp; Npad := FindWindow(nil, '无标题 - 记事本');<br>&nbsp; if npad &lt;&gt; 0 then begin<br>&nbsp; &nbsp; SetForegroundWindow(NPad);<br>&nbsp; &nbsp; keybd_event(VK_MENU, 0, 0, 0);<br>&nbsp; &nbsp; keybd_event(ord('F'), MapVirtualKey(ord('F'), 0), 0, 0);<br>&nbsp; &nbsp; keybd_event(ord('F'), MapVirtualKey(ord('F'), 0), KEYEVENTF_KEYUP, 0);<br>&nbsp; &nbsp; keybd_event(VK_MENU, 0, KEYEVENTF_KEYUP, 0);<br>&nbsp; end;<br>end;
 
to: gulang<br>&nbsp; keybd_event 这个函数只能在窗本是激活状态下才能.注意看第二条.<br>不过还是欢你的到来!!<br>我目前查到地资料能对非当前窗口发关组合键的函数有这几个:<br>PostMessage <br>SendMessage<br>PostThreadMessage<br>这几个函数.
 
鸢都剑客,keybd_event 这个函数只能在窗本是激活状态下才能<br>所以 &nbsp;gulang 使用了SetForegroundWindow将这个窗口激活嘛<br><br>你是需要不激活记事本窗口直接打开菜单?
 
可能就这意思,[:)]
 
to :wjiachun gulang <br>是的,我需要不激活就可发送消息。[:)]<br>欢迎介wjiachun 参与本问题中来。 同时没想到gulang还在继续观注这个贴子。[:D] 谢谢。
 
今天没人来啊!!!自己顶一下。
 
怎么没人来了啊!!!我搜到过刘麻子的贴子,可经我测试没能能过.郁闷!!!
 
继续努力顶,顶到来人止!!!
 
嗯 路过 再次关注<br>你这个消息是准备发送给某程序了 但消息应该是发给Windows系统的消息队列的 程序没激活 Windows怎么把消息从队列里派给程序呢?
 
to:wjiachun<br>我用postmessage发送字符,发送单个键,在这种情况下是可以实现的;就是组合键发不过去.郁闷.在网上找了好多试了都不行.
 
咱过. 给你一篇文章, 原文标题: 介绍如何通过代码实现模拟按键的函数<br>http://www.cnblogs.com/iinsnian/archive/2008/06/09/1216192.html
 
有没有考虑过HOOK呢?<br>通过键盘HOOK,拦截到所要得到的键盘消息,然后再sendmessage(hwnd,Wm_user,0,pointer)<br>在自己的程序里处理wm_user消息<br>QQ的ctrl+alt+z据说就是这样做的
 
to: p95963<br>你那连接羡并不能实现我地需求......不过同样感谢你们参与.<br><br>to: 枝上柳绵<br>发送消息的目标并不是我写的程序,是第三方程序.谢谢参与.
 
没人来啊,自己顶先。
 
procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp;hCurrentWindow,hWnd1:HWnd;<br>&nbsp;szText:array[0..254]of char;<br>&nbsp;Wind : String;<br>begin<br>&nbsp;hCurrentWindow:=GetWindow(Handle,GW_HWNDFIRST);<br>&nbsp;While hCurrentWindow&lt;&gt;0 Do<br>&nbsp;Begin<br>&nbsp; &nbsp;If GetWindowText(hCurrentWindow,@szText,255)&lt;&gt;0 then<br>&nbsp; &nbsp; &nbsp;if pos('无标题 - 记事本',Strpas(@szText))&lt;&gt;0 then begin<br>&nbsp; &nbsp; &nbsp; &nbsp;hWnd1 := GetWindow(hCurrentWindow,GW_Child);;<br><br>&nbsp; &nbsp; &nbsp; &nbsp;SetForegroundWindow(hWnd1);<br>&nbsp; &nbsp; &nbsp; &nbsp;keybd_event(VK_MENU , MapVirtualKey(VK_MENU , 0 ), 0 , 0 );<br>&nbsp; &nbsp; &nbsp; &nbsp;sleep(100);<br>&nbsp; &nbsp; &nbsp; &nbsp;keybd_event(Ord('F'), MapVirtualKey(Ord('F'), 0 ), 0 , 0 );<br>&nbsp; &nbsp; &nbsp; &nbsp;keybd_event(VK_MENU , MapVirtualKey(VK_MENU ,0),KEYEVENTF_KEYUP, 0);<br>&nbsp; &nbsp; &nbsp; &nbsp;keybd_event(Ord('F'), MapVirtualKey(Ord('F'),0),KEYEVENTF_KEYUP, 0);<br>&nbsp; &nbsp; &nbsp; sleep(200);<br>&nbsp; &nbsp; &nbsp; &nbsp;exit;<br>&nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; &nbsp;hCurrentWindow:=GetWindow(hCurrentWindow,GW_HWNDNEXT);<br>&nbsp;end;<br>&nbsp;hCurrentWindow:=GetWindow(Handle,GW_HWNDFIRST);<br>&nbsp;end;<br><br>凑个数,以前写得东西<br><br>我只能做到先激活再发消息<br><br>大侠见笑了
 

Similar threads

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