代码之家  ›  专栏  ›  技术社区  ›  James Cadd

使用窗口手柄将窗口设为最顶层

  •  18
  • James Cadd  · 技术社区  · 15 年前

    在使用process类启动应用程序之后,我想将该窗口设置为最顶层。目前,我的应用程序是最上面的窗口,所以当我启动另一个应用程序时,它不会显示。我想到的一件事是,在启动进程之前,我可以为我的应用程序设置topmost=false,问题是我想在向用户显示它之前给进程足够的时间来加载它,所以当我将另一个应用程序切换到最顶层时,我希望有更多的控制权。

    1 回复  |  直到 7 年前
        1
  •  54
  •   Reed Copsey    15 年前

    你需要使用 P/Invoke with SetWindowPos 为此:

    [DllImport("user32.dll")]
    static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
    
    static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
    const UInt32 SWP_NOSIZE = 0x0001;
    const UInt32 SWP_NOMOVE = 0x0002;
    const UInt32 SWP_SHOWWINDOW = 0x0040;
    
    // Call this way:
    SetWindowPos(theWindowHandle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);