我正在运行的应用程序需要调用单独的应用程序来进行一些扫描。我通过启动一个新的应用程序来调用另一个应用程序System.Diagnostics.Process
。一旦获得该流程,我就会调用一个方法来让该应用程序获得焦点。我尝试了两种不同的方法来让外部应用程序获得焦点,但都不起作用。有人可以帮忙吗?
这是代码:
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
and SetWindowPos
方法,另一种方法使用SetForegroundWindow
方法。两者都行不通...
我是否使用了错误的方法,或者我正在使用的代码中有错误?谢谢大家!
使用 SetWindowPos,但每当您不希望窗口成为最顶层时,请再次调用它,并将第二个参数设置为 -2 (HWND_NOTOPMOST) 而不是 -1(HWND_TOPMOST)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)