我读过一些使用 C# 强制窗口在前台显示的方法,其中使用了 Win32 的 user32.dll。
- How do I bring an unmanaged application window to front, and make it the active window for (simulated) user input
- https://shlomio.wordpress.com/2012/09/04/solved-setforegroundwindow-win32-api-not-always-works/
除了一种情况外,这些都可以完美地工作。 在 Windows 8 上,如果“开始”菜单或 Windows 应用商店应用程序位于前台,这些操作将会失败。
我只需要在“开始”菜单位于前台时才能完成此操作。 有没有隐藏的方法来实现这一点?
最佳答案
DispatcherHelper.CheckBeginInvokeOnUI(async () =>
{
try
{
if (!this.IsActive)
{
//pressing windows button
InputSimulator.SimulateKeyPress(VirtualKeyCode.LWIN);
}
await Task.Delay(1000);
ApplicationRunningHelper.GetCurrentProcessOnFocus();
}
catch (Exception ex)
{
...
}
});
public static class ApplicationRunningHelper
{
[DllImport("user32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
private static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
[DllImport("user32.dll")]
private static extern bool IsIconic(IntPtr hWnd);
[DllImport("user32.dll", SetLastError = true)]
public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
// When you don't want the ProcessId, use this overload and pass
// IntPtr.Zero for the second parameter
[DllImport("user32.dll")]
public static extern uint GetWindowThreadProcessId(IntPtr hWnd, IntPtr ProcessId);
[DllImport("kernel32.dll")]
public static extern uint GetCurrentThreadId();
/// The GetForegroundWindow function returns a handle to the
/// foreground window.
[DllImport("user32.dll")]
public static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
public static extern bool AttachThreadInput(uint idAttach, uint idAttachTo, bool fAttach);
[DllImport("user32.dll", SetLastError = true)]
public static extern bool BringWindowToTop(IntPtr hWnd);
[DllImport("user32.dll", SetLastError = true)]
public static extern bool BringWindowToTop(HandleRef hWnd);
[DllImport("user32.dll")]
public static extern bool ShowWindow(IntPtr hWnd, uint nCmdShow);
//one source
private static int SW_HIDE = 0;
private static int SW_SHOWNORMAL = 1;
private static int SW_SHOWMINIMIZED = 2;
private static int SW_SHOWMAXIMIZED = 3;
private static int SW_SHOWNOACTIVATE = 4;
private static int SW_RESTORE = 9;
private static int SW_SHOWDEFAULT = 10;
//other source
private static int SW_SHOW = 5;
/// <summary>
/// check if current process already running. if runnung, set focus to
/// existing process and returns true otherwise returns false.
/// </summary>
/// <returns></returns>
public static bool GetCurrentProcessOnFocus()
{
try
{
Process me = Process.GetCurrentProcess();
Process[] arrProcesses = Process.GetProcessesByName(me.ProcessName);
IntPtr hWnd = arrProcesses[0].MainWindowHandle;
ForceForegroundWindow(hWnd);
return true;
}
catch (Exception)
{
return false;
}
}
public static void ForceForegroundWindow(IntPtr hWnd)
{
uint foreThread = GetWindowThreadProcessId(GetForegroundWindow(), IntPtr.Zero);
uint appThread = GetCurrentThreadId();
const uint SW_SHOW = 5;
if (foreThread != appThread)
{
AttachThreadInput(foreThread, appThread, true);
BringWindowToTop(hWnd);
ShowWindow(hWnd, SW_SHOW);
AttachThreadInput(foreThread, appThread, false);
}
else
{
BringWindowToTop(hWnd);
ShowWindow(hWnd, SW_SHOW);
}
}
}
关于c# - 在 Windows 8 上设置前景窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17370939/