将外部应用程序移到屏幕前面 [英] Move external application to the front of the screen
问题描述
我正在运行的应用程序需要调用一个单独的应用程序来进行一些扫描.我通过启动一个新的 System.Diagnostics.Process
来调用另一个应用程序.一旦我获得了这个过程,我就会调用一个方法来为该应用程序提供焦点.我尝试了两种不同的方法来让外部应用程序成为焦点,但都不起作用.有人可以帮忙吗?
The application that I'm running needs to call a separate app to do some scanning. I'm calling the other application by starting a new System.Diagnostics.Process
. Once I get that process, I call a method to give that application the focus. I've tried two different ways to give that external app the focus, but neither are working. Could someone help?
代码如下:
using System.Runtime.InteropServices;
[DllImport("user32.dll")]
private static extern bool ShowWindow(IntPtr hWnd, uint windowStyle);
[DllImport("user32.dll")]
private static extern bool SetWindowPos(IntPtr hWnd,
IntPtr hWndInsertAfter, int x, int y, int cx, int cy, uint uFlags);
[DllImport("user32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
private static void GiveSpecifiedAppTheFocus(int processID)
{
try
{
Process p = Process.GetProcessById(processID);
ShowWindow(p.MainWindowHandle, 1);
SetWindowPos(p.MainWindowHandle, new IntPtr(-1), 0, 0, 0, 0, 3);
//SetForegroundWindow(p.MainWindowHandle);
}
catch
{
throw;
}
}
第一个场景使用ShowWindow
和SetWindowPos
方法,另一个方法使用SetForegroundWindow
方法.两者都行不通...
First scenario uses the ShowWindow
and SetWindowPos
methods, the other method uses the SetForegroundWindow
method. Neither will work...
我是否使用了错误的方法,或者我使用的代码中有错误?谢谢大家!
Am I using the wrong methods, or do I have an error in the code that I'm using? Thanks all!
推荐答案
使用 SetWindowPos,但是当您不想让窗口位于最顶层时再次调用它,将第二个参数设置为 -2 (HWND_NOTOPMOST) 而不是-1(HWND_TOPMOST)
Use SetWindowPos, but whenever you don't want the window to be the topmost anymore call it again with the second parameter set to -2 (HWND_NOTOPMOST) instead of -1(HWND_TOPMOST)
这篇关于将外部应用程序移到屏幕前面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!