C# 使用窗口句柄在最上面创建一个窗口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1528473/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 18:27:56  来源:igfitidea点击:

Make a window topmost using a window handle

c#windowsprocess

提问by James Cadd

After launching an application using the Process class I'd like to make that window topmost. Currently, my app is the topmost window so when i launch the other app it doesn't display. One thing that came to mind is that I could set topmost = false for my application before launching the process, the problem with this is I want to give the process ample time to load up before displaying it to the user, so I'd like more control over when I switch the other application to the topmost.

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

采纳答案by Reed Copsey

You need to use P/Invoke with SetWindowPosto accopmlish this:

你需要使用P/Invoke 和 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);