如何使窗口最顶层只有一次

如何使窗口最顶层只有一次

问题描述:

我想在C#应用程序中创建一个最顶层的窗口。我需要做到这一点,以便在窗口加载时(应用程序由第三方软件调用),它将位于顶部,如果需要,用户可以浏览到其他窗口。如何使窗口最顶层只有一次

我用

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

,但它不具有任何影响。

+0

如果您的应用运行两次会发生什么?我喜欢用高兴的盔甲来决定死亡的窗户形式。 – asawyer 2011-02-15 21:25:33

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

如果形式在不同的应用程序,你可以通过调用FindWindow函数API得到你想要把到前窗,以及SetForegroundWindow API来把它前面,了解更多信息,可以阅读这些喜欢

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

SetForegroun dWindow http://msdn.microsoft.com/en-us/library/ms633539(VS.85).aspx

我可以建议你使用本机API。 SetWindowPos函数来自user32.dll

这样的事情,但它应该转换为C#代码,我认为这不会有困难。 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 

TopMost只适用于你的应用程序中的表格。如果你想要把你的形式在您的计算机上运行的其他应用程序上,我建议你先看看这个问题:

Bringing window to the front in c# using win 32 api

this.Activate() 

应该做的伎俩。