紧急求助!如何通过窗口句柄使隐藏窗口(hide方法)出现
点击窗体的关闭按钮,使其隐藏至任务栏的托盘中。这是一个单实例的程序,运行时,需要在程序的入口处判断是否已经有该进程在运行,如果没有则正常运行;已有程序则根据该进程的句柄。可是如果已运行该程序,且程序隐藏在托盘中(通过form.hide()方法),则无法通过快捷方式将其显示。我发现迅雷和msn都可以实现类似功能,希望各位能给予解答。
我尝试使用API使窗体显示,可是只能在最小化时显示,隐藏后则无法显示。以下是程序入口处发现已有进程运行时的显示窗体代码
其中instance.MainWindowHandle表示进程的主窗口句柄。
public static void HandleRunningInstance(Process instance)
{
SetForegroundWindow(instance.MainWindowHandle);
ShowWindowAsync(instance.MainWindowHandle, 9);
SendMessage(instance.MainWindowHandle, 0x18, GetFocus(),IntPtr.Zero);
}
[DllImport("User32.dll")]
private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);
[DllImport("User32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("User32.dll")]
private static extern IntPtr SendMessage(IntPtr hwnd, uint msg, IntPtr wParam, IntPtr lParam);
[DllImport("User32.dll")]
private static extern IntPtr GetFocus();