在WIN98下,如何把其他进程的窗口激活,从后台切换到前台?(100分)

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

iamphp

Unregistered / Unconfirmed
GUEST, unregistred user!
  我想设计一个WIN98下的程序实现几个其他窗口之间切换的功能。<br>  我拿两个IE窗口A和B来做试验,把焦点在A和B之间交替切换,结<br>果发现只有第一次是成功的,也就是焦点从我的进程转到A那一次,接<br>着,就只是使B状态栏上闪烁,并没有真正切换到前台。<br>  我用的是: &nbsp; &nbsp; &nbsp; SetForegroundWindow(true_h);<br>  但是,如果人为地把我的进程窗口先聚焦(用鼠标点一下),然后<br>第一次又是成功的,接着,又......。为什么会这样?怎样解决这个问<br>题?<br>  我曾经用过:<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SetForegroundWindow(true_h);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SetActiveWindow(true_h);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; windows.SetFocus(true_h);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sendmessage(true_h,wm_syscommand,sc_restore,0);<br>  这几个过程的组合,始终最好的只有SetForegroundWindow(true_h)。<br>另外曾经有人说过用SwitchToThisWindow(wnd:HWND),但是我用的时候却<br>说我的进程非法操作!<br><br>
 
试试bringwindowtotop
 
B在状态栏上闪烁说明它已取得焦点。<br>BringWindowToTop应该可以。<br><br>
 
试过啦!<br>没反应!<br>
 
不可能啊,我试过没问题,方法如下:<br>1.通过findwindow获得窗口句柄<br>2.使用bringwindowtotop使它从后台到前台.<br>如果是IE注意caption的写法,例如:<br>'大富翁论坛 - Microsoft Internet Explorer'<br>"大富翁论坛"与"-"之间有一空格.<br><br>还有一点要提醒的是:如果ie窗口为最小化状态,此方法失效.
 
我有一个笨招,但挺管用的。<br>procedure TGetDot.Timer1Timer(Sender: TObject);<br>var<br>&nbsp; &nbsp;hCurWindow,CurActiveWindow: HWnd; &nbsp;// 窗口句柄<br>&nbsp; &nbsp;WinText: array [0..255] of char;<br>begin<br>&nbsp; &nbsp; &nbsp;CurActiveWindow:=GetActiveWindow();<br>&nbsp; &nbsp; &nbsp;hCurWindow := GetWindow(Handle, GW_HWNDFIRST);<br>&nbsp; &nbsp; &nbsp;// 获取第一个窗口的句柄<br>&nbsp; &nbsp; &nbsp;while hCurWindow &lt;&gt; 0 do<br>&nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 获取窗口的名称,并查找是否为目标窗体,注意窗体标题要准确<br>&nbsp; &nbsp; &nbsp;if GetWindowText(hCurWindow, @WinText, 255)&gt;0 then<br>&nbsp; &nbsp; &nbsp; &nbsp;if ( WinText='民主湖聊天室 - Microsoft Internet Explorer') then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//激活指定窗口,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SendMessage(hCurWindow,WM_SYSCOMMAND,SC_MINIMIZE,0);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SendMessage(hCurWindow,WM_SYSCOMMAND,SC_RESTORE,0);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//将鼠标控制转到指定窗口,并在指定坐标产生鼠标按键事件<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SetCapture(hCurWindow);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;mouse_event(MOUSEEVENTF_LEFTDOWN+MOUSEEVENTF_LEFTUP,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;720,475,0,GetMessageExtraInfo());<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sleep(5000); //给指定程序以反应事件的时间,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;可更改为Application.ProcessMessages<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//激活自身的窗体<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SendMessage(Form1.Handle,WM_SYSCOMMAND,SC_MINIMIZE,0);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SendMessage(Form1.Handle,WM_SYSCOMMAND,SC_RESTORE,0);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 获取下一个窗口的句柄<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hCurWindow:=GetWindow(hCurWindow, GW_HWNDNEXT);<br>&nbsp; &nbsp; &nbsp;end;<br>end;<br>
 
&nbsp; &nbsp; &nbsp; &nbsp;//找到前一实例,将其还原,并移到前台<br>&nbsp; &nbsp; &nbsp; &nbsp; HWND &nbsp; &nbsp;hWnd = FindWindow(ClassName,Caption);<br>&nbsp; &nbsp; &nbsp; &nbsp; //Borland公司的窗口最小化可能是将窗口隐含。所以考虑窗口最小化和不可见两种状态<br>&nbsp; &nbsp; &nbsp; &nbsp; if ( IsIconic(hWnd) || !IsWindowVisible(hWnd) )<br>&nbsp; &nbsp; &nbsp; &nbsp; {<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ShowWindow(hWnd,SW_SHOWNORMAL);<br>&nbsp; &nbsp; &nbsp; &nbsp; }<br>&nbsp; &nbsp; &nbsp; &nbsp; SetForegroundWindow(hWnd);<br>&nbsp; &nbsp; &nbsp; &nbsp; BringWindowToTop(hWnd);<br>这是C++Builder程序,试验通过<br>
 
我的意思是指:在我的程序以外的其它两个窗口之间互相切换到前台。<br>如果单单是使某一个窗口移到前台,很简单。
 
&nbsp; &nbsp;hwndtoshow:=FindWindow;<br>&nbsp; &nbsp; SetForegroundWindow(hwndtoshow);<br>&nbsp; &nbsp; ShowWindow(hwndtoshow, SW_RESTORE);<br>
 
先最小化后激活
 
来迟了<br>iamphp不如你就把大家的办法综合一下,把SetForegroundWindow(); &nbsp;ShowWindow();<br>bringwindowtotop()都用上,应该可以的<br><br>
 
多人接受答案了。
 
后退
顶部