C# 在Windows 8上设置前景窗口
我读过一些关于使用Win32的user32.dll,使用C#强制在前台显示窗口的方法C# 在Windows 8上设置前景窗口,c#,.net,winapi,dllimport,user32,C#,.net,Winapi,Dllimport,User32,我读过一些关于使用Win32的user32.dll,使用C#强制在前台显示窗口的方法 除了一种情况外,这些都能很好地工作。 在Windows 8上,如果“开始”菜单或Windows应用商店应用程序位于前台,则这些应用程序将失败 我只需要使这项工作时,开始菜单是在前台虽然。 是否有一种隐藏的方法来实现这一点?DispatcherHelper.CheckBeginInvokeOnUI(异步()=> DispatcherHelper.CheckBeginInvokeOn
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);
}
}
}
{
尝试
{
如果(!this.IsActive)
{
//按windows按钮
InputSimulator.SimulateKeyPress(VirtualKeyCode.LWIN);
}
等待任务。延迟(1000);
ApplicationRunningHelper.GetCurrentProcessOnFocus();
}
捕获(例外情况除外)
{
...
}
});
公共静态类ApplicationRunningHelper
{
[DllImport(“user32.dll”)]
私有静态外部bool setforegroundindow(IntPtr hWnd);
[DllImport(“user32.dll”)]
私有静态外部bool ShowWindowAsync(IntPtr hWnd、intncmdshow);
[DllImport(“user32.dll”)]
私有静态外部边界(IntPtr hWnd);
[DllImport(“user32.dll”,SetLastError=true)]
公共静态外部单元GetWindowThreadProcessId(IntPtr hWnd,out单元lpdwProcessId);
//如果不需要ProcessId,请使用此重载并传递
//第二个参数为IntPtr.Zero
[DllImport(“user32.dll”)]
公共静态外部单元GetWindowThreadProcessId(IntPtr hWnd,IntPtr ProcessId);
[DllImport(“kernel32.dll”)]
公共静态外部uint GetCurrentThreadId();
///getForeGroundIndow函数返回
///前景窗口。
[DllImport(“user32.dll”)]
公共静态外部IntPtr GetForegroundWindow();
[DllImport(“user32.dll”)]
公共静态外部bool AttachThreadInput(uint-idAttach、uint-idAttachTo、bool-fAttach);
[DllImport(“user32.dll”,SetLastError=true)]
公共静态外部布尔布林温多图(IntPtr hWnd);
[DllImport(“user32.dll”,SetLastError=true)]
公共静态外部bool Bringwindowtop(HandleRef hWnd);
[DllImport(“user32.dll”)]
公共静态外部布尔显示窗口(IntPtr hWnd、uint nCmdShow);
//一个来源
私有静态int SW_HIDE=0;
专用静态int SW_SHOWNORMAL=1;
专用静态int SW_=2;
私有静态int SW_showmized=3;
专用静态int SW_SHOWNOACTIVATE=4;
专用静态int SW_RESTORE=9;
专用静态int SW_SHOWDEFAULT=10;
//其他来源
专用静态int SW_SHOW=5;
///
///检查当前进程是否已在运行。如果正在运行,请将焦点设置为
///现有进程并返回true,否则返回false。
///
///
公共静态bool GetCurrentProcessOnFocus()
{
尝试
{
Process me=Process.GetCurrentProcess();
Process[]arrprocess=Process.GetProcessesByName(me.ProcessName);
IntPtr hWnd=ARR进程[0]。MainWindowHandle;
强制力(hWnd);
返回true;
}
捕获(例外)
{
返回false;
}
}
公共静态无效力ForeGroundIndow(IntPtr hWnd)
{
uint foreThread=GetWindowThreadProcessId(GetForegroundWindow(),IntPtr.Zero);
uint appThread=GetCurrentThreadId();
Constuint SW_SHOW=5;
if(前线程!=appThread)
{
AttachThreadInput(前线程、appThread、true);
布林温多托(hWnd);
展示窗口(hWnd、SW_展示);
AttachThreadInput(前线程、appThread、false);
}
其他的
{
布林温多托(hWnd);
展示窗口(hWnd、SW_展示);
}
}
}
我可能错了,但我相信你可以完成这项工作。问题是搜索窗口已经是最顶端的,所以你需要获得该窗口的句柄并将其更改为zOrder,然后使用相同的函数将窗口设置为最顶端。我自己没有尝试过,但我遇到一些报告说它没有:我也不能尝试,因为我没有windows 8,但您需要了解的是,所有windows 8窗口都是使用GDI的最终版本,它们有正常的句柄,您可以像以前一样使用Win32函数,因此我相信值得您尝试。