控制另一进程, 为什么我的代码不行 较难,有兴趣的高手进来 搞定了请吃饭 ( 积分: 200 )

  • 主题发起人 主题发起人 ivyzeng
  • 开始时间 开始时间
I

ivyzeng

Unregistered / Unconfirmed
GUEST, unregistred user!
以下代码是控制 Acrobat Reader 全屏,<br> &nbsp;在 Acrobat reader 中按 CTRL+L 会全屏,<br> &nbsp;我用Spy++ 跟踪出消息为 4条 , 发到 caption 为 'AVPageView' 的窗口上,<br> 我在程序中发送同样的消息,为什么达不到全屏的效果。 (用Spy++ 发送的消息 与 按“CTRL+L”是一致的)<br><br>procedure TForm1.SpeedButton5Click(Sender: TObject);<br>var<br> &nbsp;wK,wK2 :WORD;<br> &nbsp;bC &nbsp;:Byte;<br> &nbsp;dwK : DWORD;<br> &nbsp;dwK2 : DWORD;<br><br> &nbsp;hWnd,hWnd1 : ULONG;<br> <br> &nbsp;function GetWnd(name : string) : ULONG;<br> &nbsp;var<br> &nbsp; &nbsp;alist : TList;<br> &nbsp; &nbsp;strCap : pchar;<br> &nbsp; &nbsp;b : boolean;<br> &nbsp; &nbsp;s : string;<br> &nbsp;begin<br> &nbsp; &nbsp; result := 0;<br> &nbsp; &nbsp; alist := TList.Create;<br> &nbsp; &nbsp; hWnd := GetDeskTopWindow();<br> &nbsp; &nbsp; &nbsp;GetMem(strCap,255);<br> &nbsp; &nbsp; &nbsp;alist.Clear;<br> &nbsp; &nbsp; alist.Add(Pointer(hWnd));<br> &nbsp; &nbsp; while (alist.Count &amp;gt; 0) &nbsp;do<br> &nbsp; &nbsp; begin<br> &nbsp; &nbsp; &nbsp; hWnd := &nbsp;ULONG(alist.Items[0]);<br> &nbsp; &nbsp; &nbsp; alist.Delete(0);<br><br> &nbsp; &nbsp; &nbsp; &nbsp; GetWindowText(hWnd,strCap,255);<br><br> &nbsp; &nbsp; &nbsp; &nbsp; s := strpas(strCap);<br><br> &nbsp; &nbsp; &nbsp; &nbsp; if s = name then<br> &nbsp; &nbsp; &nbsp; &nbsp; begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result := &nbsp;hWnd;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; exit;<br> &nbsp; &nbsp; &nbsp; &nbsp; end;<br> &nbsp; &nbsp; &nbsp; &nbsp; hWnd1 := GetWindow(hWnd,GW_Child);<br> &nbsp; &nbsp; &nbsp; &nbsp; if (hWnd1 &amp;gt; 0) then<br> &nbsp; &nbsp; &nbsp; &nbsp; begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alist.Add(Pointer(hWnd1));<br> &nbsp; &nbsp; &nbsp; &nbsp; end;<br><br> &nbsp; &nbsp; &nbsp; &nbsp; hWnd1 := GetWindow(hWnd,GW_HWNDNEXT);<br> &nbsp; &nbsp; &nbsp; &nbsp; if (hWnd1 &amp;gt; 0) then<br> &nbsp; &nbsp; &nbsp; &nbsp; begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alist.Add(Pointer(hWnd1));<br> &nbsp; &nbsp; &nbsp; &nbsp; end;<br><br> &nbsp; &nbsp; end;<br> &nbsp; &nbsp; FreeMem(strCap,255);<br> &nbsp; &nbsp; alist.Free;<br> &nbsp;end;<br><br>begin<br> &nbsp; &nbsp; hWnd := GetWnd('AVPageView');<br> &nbsp;if (hWnd &amp;gt; 0) then<br> &nbsp;begin<br> &nbsp; &nbsp;//SendMessage(hWnd,WM_KEYDOWN,13 ,1);<br> &nbsp; bC := 29;<br><br> &nbsp;wK := MAKEWORD(29,0);<br> &nbsp;wK2 := MAKEWORD(38,0);<br> &nbsp;dwK := MAKELONG(1,wK);<br> &nbsp;dwK2 := MAKELONG(1,wK2);<br><br> &nbsp; &nbsp;PostMessage(hWnd,WM_KEYDOWN,VK_CONTROL ,dwK);<br> &nbsp; &nbsp;PostMessage(hWnd,WM_KEYDOWN,Ord('L') ,dwK2);<br> &nbsp; &nbsp;PostMessage(hWnd,WM_KEYUP,Ord('L') ,dwK2);<br> &nbsp; &nbsp;PostMessage(hWnd,WM_KEYUP,VK_CONTROL ,dwK);<br> &nbsp;end;<br>end;<br>
 
以下代码是控制 Acrobat Reader 全屏,<br> &nbsp;在 Acrobat reader 中按 CTRL+L 会全屏,<br> &nbsp;我用Spy++ 跟踪出消息为 4条 , 发到 caption 为 'AVPageView' 的窗口上,<br> 我在程序中发送同样的消息,为什么达不到全屏的效果。 (用Spy++ 发送的消息 与 按“CTRL+L”是一致的)<br><br>procedure TForm1.SpeedButton5Click(Sender: TObject);<br>var<br> &nbsp;wK,wK2 :WORD;<br> &nbsp;bC &nbsp;:Byte;<br> &nbsp;dwK : DWORD;<br> &nbsp;dwK2 : DWORD;<br><br> &nbsp;hWnd,hWnd1 : ULONG;<br> <br> &nbsp;function GetWnd(name : string) : ULONG;<br> &nbsp;var<br> &nbsp; &nbsp;alist : TList;<br> &nbsp; &nbsp;strCap : pchar;<br> &nbsp; &nbsp;b : boolean;<br> &nbsp; &nbsp;s : string;<br> &nbsp;begin<br> &nbsp; &nbsp; result := 0;<br> &nbsp; &nbsp; alist := TList.Create;<br> &nbsp; &nbsp; hWnd := GetDeskTopWindow();<br> &nbsp; &nbsp; &nbsp;GetMem(strCap,255);<br> &nbsp; &nbsp; &nbsp;alist.Clear;<br> &nbsp; &nbsp; alist.Add(Pointer(hWnd));<br> &nbsp; &nbsp; while (alist.Count &amp;gt; 0) &nbsp;do<br> &nbsp; &nbsp; begin<br> &nbsp; &nbsp; &nbsp; hWnd := &nbsp;ULONG(alist.Items[0]);<br> &nbsp; &nbsp; &nbsp; alist.Delete(0);<br><br> &nbsp; &nbsp; &nbsp; &nbsp; GetWindowText(hWnd,strCap,255);<br><br> &nbsp; &nbsp; &nbsp; &nbsp; s := strpas(strCap);<br><br> &nbsp; &nbsp; &nbsp; &nbsp; if s = name then<br> &nbsp; &nbsp; &nbsp; &nbsp; begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result := &nbsp;hWnd;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; exit;<br> &nbsp; &nbsp; &nbsp; &nbsp; end;<br> &nbsp; &nbsp; &nbsp; &nbsp; hWnd1 := GetWindow(hWnd,GW_Child);<br> &nbsp; &nbsp; &nbsp; &nbsp; if (hWnd1 &amp;gt; 0) then<br> &nbsp; &nbsp; &nbsp; &nbsp; begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alist.Add(Pointer(hWnd1));<br> &nbsp; &nbsp; &nbsp; &nbsp; end;<br><br> &nbsp; &nbsp; &nbsp; &nbsp; hWnd1 := GetWindow(hWnd,GW_HWNDNEXT);<br> &nbsp; &nbsp; &nbsp; &nbsp; if (hWnd1 &amp;gt; 0) then<br> &nbsp; &nbsp; &nbsp; &nbsp; begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alist.Add(Pointer(hWnd1));<br> &nbsp; &nbsp; &nbsp; &nbsp; end;<br><br> &nbsp; &nbsp; end;<br> &nbsp; &nbsp; FreeMem(strCap,255);<br> &nbsp; &nbsp; alist.Free;<br> &nbsp;end;<br><br>begin<br> &nbsp; &nbsp; hWnd := GetWnd('AVPageView');<br> &nbsp;if (hWnd &amp;gt; 0) then<br> &nbsp;begin<br> &nbsp; &nbsp;//SendMessage(hWnd,WM_KEYDOWN,13 ,1);<br> &nbsp; bC := 29;<br><br> &nbsp;wK := MAKEWORD(29,0);<br> &nbsp;wK2 := MAKEWORD(38,0);<br> &nbsp;dwK := MAKELONG(1,wK);<br> &nbsp;dwK2 := MAKELONG(1,wK2);<br><br> &nbsp; &nbsp;PostMessage(hWnd,WM_KEYDOWN,VK_CONTROL ,dwK);<br> &nbsp; &nbsp;PostMessage(hWnd,WM_KEYDOWN,Ord('L') ,dwK2);<br> &nbsp; &nbsp;PostMessage(hWnd,WM_KEYUP,Ord('L') ,dwK2);<br> &nbsp; &nbsp;PostMessage(hWnd,WM_KEYUP,VK_CONTROL ,dwK);<br> &nbsp;end;<br>end;<br>
 
看这个方式对你有没有用<br><br>http://www.delphibbs.com/keylife/iblog_show.asp?xid=7541
 
ActiveHandle: THandle; &nbsp;//当前活动窗体ThreadID<br>CurHandle: THandle;<br>AHwnd:Hwnd;<br><br>CurHandle := GetCurrentThreadID;<br>AHwnd:=GetWnd('AVPageView');<br>ActiveHandle := GetWindowThreadProcessID(AHwnd, nil);<br><br>AttachThreadInput(CurHandle, ActiveHandle, True);<br>try<br> &nbsp; &nbsp; PostMessage(AHwnd,WM_KEYDOWN,VK_CONTROL ,dwK);<br> &nbsp; &nbsp; PostMessage(AHwnd,WM_KEYDOWN,Ord('L') ,dwK2);<br> &nbsp; &nbsp; PostMessage(AHwnd,WM_KEYUP,Ord('L') ,dwK2);<br> &nbsp; &nbsp; PostMessage(AHwnd,WM_KEYUP,VK_CONTROL ,dwK);<br>finally<br> &nbsp; &nbsp;AttachThreadInput(CurHandle, ActiveHandle, False);<br>end;
 
to wk_knife:<br>我按照你给的代码,还是不行 ,是不是还有什么玄机
 
procedure TForm1.Button1Click(Sender: TObject);<br>begin<br> &nbsp;//让目标窗口最大化 $10262是用其它工具发现的Acrobat Reader主窗口Handle<br> &nbsp;//我的Acrobat Reader的版本与你不一样.没看到AVPageView<br> &nbsp;ShowWindow($10262,SW_SHOWMAXIMIZED);<br> &nbsp;//对着最大化的Acrobat Reader窗口按键<br> &nbsp;keybd_event(VK_CONTROL , MapVirtualKey(VK_CONTROL , 0 ), 0 , 0 );<br> &nbsp;keybd_event(Ord('L'), MapVirtualKey(Ord('L'), 0 ), 0 , 0 );<br> &nbsp;keybd_event(VK_CONTROL , MapVirtualKey(VK_CONTROL ,0),KEYEVENTF_KEYUP, 0);<br> &nbsp;keybd_event(Ord('L'), MapVirtualKey(Ord('L'),0),KEYEVENTF_KEYUP, 0);<br>end;
 
to uc<br> 请问$10262 &nbsp;是用什么找出来的,我用spy++找到主窗,按你所说的不行
 
总算试成功了。<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&amp;lt;&amp;gt;0 Do<br> &nbsp;Begin<br> &nbsp; &nbsp;If GetWindowText(hCurrentWindow,@szText,255)&amp;gt;0 then<br> &nbsp; &nbsp; &nbsp;if pos('Adobe Reader',Strpas(@szText))&amp;gt;0 then begin<br> &nbsp; &nbsp; &nbsp; &nbsp;hWnd1 := GetWindow(hCurrentWindow,GW_Child);;<br> &nbsp; &nbsp; &nbsp; &nbsp;SetForegroundWindow(hWnd1);<br> &nbsp; &nbsp; &nbsp; &nbsp;sleep(100);<br> &nbsp; &nbsp; &nbsp; &nbsp;keybd_event(VK_CONTROL , MapVirtualKey(VK_CONTROL , 0 ), 0 , 0 );<br> &nbsp; &nbsp; &nbsp; &nbsp;sleep(100);<br> &nbsp; &nbsp; &nbsp; &nbsp;keybd_event(Ord('L'), MapVirtualKey(Ord('L'), 0 ), 0 , 0 );<br> &nbsp; &nbsp; &nbsp; &nbsp;sleep(100);<br> &nbsp; &nbsp; &nbsp; &nbsp;keybd_event(VK_CONTROL , MapVirtualKey(VK_CONTROL ,0),KEYEVENTF_KEYUP, 0);<br> &nbsp; &nbsp; &nbsp; &nbsp;sleep(100);<br> &nbsp; &nbsp; &nbsp; &nbsp;keybd_event(Ord('L'), MapVirtualKey(Ord('L'),0),KEYEVENTF_KEYUP, 0);<br> &nbsp; &nbsp; &nbsp; &nbsp;sleep(100);<br> &nbsp; &nbsp; &nbsp; &nbsp;exit;<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp;hCurrentWindow:=GetWindow(hCurrentWindow,GW_HWNDNEXT);<br> &nbsp;end;<br>
 
spy++显示的主要有窗口、进程、线程。<br>在窗口页,每一条的内容就是--窗口句柄+文本+类名。<br>如果你确定知道窗口的文本+类名,可以用findwindow得到窗口句柄。<br>用ShowWindow(h,SW_SHOWMAXIMIZED);可使该窗口成为当前窗口。对着当前窗口模拟ctrl+L,当然跟按...的效果是一样的。<br>
 
楼上的方法不错。
 
楼上,应该是说我的吧。<br><br>呵呵,臭美一下
 
呵呵,OK了,<br>不过还有一个继续研究一下,<br>为什么在用ActiveX &nbsp;控件又不行了<br><br>jianguobu, uc, 高手请再关注一下,马上给分
 
不明白你的意思<br><br>你的说把我写的类似代码做成ActiveX就不行了?
 
将 pdf.ocx 放在Form上,运行 ,<br> &nbsp;这时我按CTRL_L 可以<br>但程序不可以,<br>你们提供的方法,如果在Desktop上启动 Reader 可以达到效果
 
晕,这部份又得划不少时间来试了。<br>应该与窗口有关系的。
 
我试了一下午, <br> &nbsp;分析了窗口结构 &nbsp;从下到上是<br> AVPageView -&amp;gt; AVScrollView -&amp;gt; AVSplitterView -&amp;gt;AVSplitationPageView-&amp;gt;AVSplitterView-&amp;gt;E:/EBook/Ice-1.3.0-C.pdf (打开文件)-&amp;gt; ATL 的窗口 -&amp;gt; Form<br><br>我每个都试过了 可不行, 如果 单独 启动 Reader 都可以
 
不是,现在的窗口是你的Form窗口
 
把你做好的代码给我,我来试。<br><br>呵呵,你这个问题需要另外加分才行,我今天上午试了好长时间。
 
做个demo给我呀
 
后退
顶部