简体   繁体   English

如何仅使窗口最顶部一次

[英]How to make a window topmost only one time

I want to make a window topmost in a C# application. 我想在C#应用程序中使窗口最上方。 i need to make it so that while the window is loaded(the application is invoked by a third party software) it will be on top and user can browse to other windows if he needed. 我需要这样做,以便在加载窗口(应用程序由第三方软件调用)时将其置于顶部,并且用户可以根据需要浏览到其他窗口。

I used 我用了

this.Topmost = true; 
this.TopMost=false; 

but it dont have any effect. 但它没有任何作用。

TopMost only applies to forms within your application. TopMost仅适用于您的应用程序中的表单。 If you want to bring your form to the top of other applications running on your computer, I suggest you take a look at this question: 如果您想将表单放在计算机上运行的其他应用程序的顶部,建议您看一下以下问题:

Bringing window to the front in c# using win 32 api 使用Win 32 API在C#中将窗口置于最前面

UPDATE 更新
READ THIS http://social.msdn.microsoft.com/forums/en-US/winforms/thread/bf3117f8-d83d-4b00-8e4f-7398b559a2dd/ 阅读此http://social.msdn.microsoft.com/forums/en-US/winforms/thread/bf3117f8-d83d-4b00-8e4f-7398b559a2dd/

If the forms are in different applications, you can get the window you want to bring to front by calling the FindWindow API, and SetForegroundWindow API to bring it to front, for more information, you can read these likes 如果表单在不同的应用程序中,则可以通过调用FindWindow API和SetForegroundWindow API将其置于最前面来获取要显示的窗口,有关更多信息,您可以阅读以下内容:

FindWindow http://msdn.microsoft.com/en-us/library/ms633499(VS.85).aspx FindWindow http://msdn.microsoft.com/zh-cn/library/ms633499(VS.85).aspx

SetForegroundWindow http://msdn.microsoft.com/en-us/library/ms633539(VS.85).aspx SetForegroundWindow http://msdn.microsoft.com/zh-cn/library/ms633539(VS.85).aspx

I can suggest you to use native API. 我可以建议您使用本机API。 SetWindowPos function from user32.dll 来自user32.dll的SetWindowPos函数

something like this, but it should be converted to C# code, i think it wouldn't be difficult. 像这样的东西,但应该将其转换为C#代码,我认为这并不困难。 HWND_TOPMOST flag is just what you needed HWND_TOPMOST标志正是您需要的

 RECT rect;
// get the current window size and position
GetWindowRect(hWnd, &rect );
// now change the size, position, and Z order
// of the window.
SetWindowPos(hWnd ,       // handle to window
HWND_TOPMOST,  // placement-order handle
rect.left,     // horizontal position
rect.top,      // vertical position
rect.right,  // width
rect.bottom, // height
SWP_SHOWWINDOW );// window-positioning options
this.Activate() 

应该可以。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM