【问题标题】:Bring to forward window when minimized最小化时转到前向窗口
【发布时间】:2013-11-08 12:54:15
【问题描述】:

我想知道如何提前一个特定的窗口。 SetForegroundWindow 在窗口未最小化时有效!!但是当最小化窗口时,SetForegroundWindow 不起作用...

这是我的代码:

        int IdRemoto = int.Parse(textBoxID.Text);

        Process[] processlist = Process.GetProcessesByName("AA_v3.3");

        foreach (Process process in processlist)
        {
            if (!String.IsNullOrEmpty(process.MainWindowTitle))
            {
                if (IdRemoto.ToString() == process.MainWindowTitle)
                    SetForegroundWindow(process.MainWindowHandle);  
            }
        }


[DllImport("user32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);

【问题讨论】:

    标签: c# window foreground


    【解决方案1】:

    您可以使用IsIconic() API 检查窗口是否最小化,然后使用ShowWindow() 将其恢复:

    public const int SW_RESTORE = 9;
    
    [DllImport("user32.dll")]
    public static extern bool IsIconic(IntPtr handle);
    
    [DllImport("user32.dll")]
    public static extern bool ShowWindow(IntPtr handle, int nCmdShow);
    
    [DllImport("user32.dll")]
    public static extern int SetForegroundWindow(IntPtr handle);
    
    private void BringToForeground(IntPtr extHandle)
    {
        if (IsIconic(extHandle))
        {
            ShowWindow(extHandle, SW_RESTORE);
        }
        SetForegroundWindow(extHandle);
    }
    

    【讨论】:

      【解决方案2】:

      您可以将ShowWindow 与您已有的结合使用,这是您的示例,稍作修改:

          int IdRemoto = int.Parse(textBoxID.Text);
      
          Process[] processlist = Process.GetProcessesByName("AA_v3.3");
      
          foreach (Process process in processlist)
          {
              if (!String.IsNullOrEmpty(process.MainWindowTitle))
              {
                  if (IdRemoto.ToString() == process.MainWindowTitle)
                  {
                      ShowWindow(process.MainWindowHandle, 9);
                      SetForegroundWindow(process.MainWindowHandle);  
                  }
              }
          }
      
      
         [DllImport("user32.dll")]
         private static extern bool SetForegroundWindow(IntPtr hWnd);
         [DllImport("user32.dll")]
         private static extern bool ShowWindow(IntPtr hWind, int nCmdShow);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-21
        • 2011-05-27
        • 1970-01-01
        • 2011-09-17
        • 2015-07-21
        • 1970-01-01
        相关资源
        最近更新 更多