日期:2014-05-18  浏览次数:21288 次

Window API如何显示隐藏窗体(ShowWindowAsync/ShowWindow)
我在程序里做了 一台计算机只允许一个实例 

[DllImport("User32.dll")] 
//返回值:如果窗口原来可见,返回值为非零;如果函数原来被隐藏,返回值为零。
private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow); 
   

//API 常數定義 
  private const int SW_HIDE = 0; 
  private const int SW_NORMAL = 1; //正常弹出窗体 
  private const int SW_MAXIMIZE = 3; //最大化弹出窗体 
  private const int SW_SHOWNOACTIVATE = 4; 
  private const int SW_SHOW = 5; 
  private const int SW_MINIMIZE = 6; 
  private const int SW_RESTORE = 9; 
  private const int SW_SHOWDEFAULT = 10; 

ShowWindowAsync(instance.MainWindowHandle, SW_RESTORE); 
SetForegroundWindow(instance.MainWindowHandle); 

我发现当我程序最小化到托盘以后,instance.MainWindowHandle就为null了,所以窗体show不出来了。 
这个里面的参数我都试过了,哪个都不好使。难道隐藏窗体之后就无法用API 弹出窗体了吗?
希望真相帝给予支持。

------解决方案--------------------
可以尝试用FindWindow获取窗体句柄,然后再显示