Saturday, December 5, 2009

怎样保证一个窗体始终是处在最顶层的

[C#]
public bool TopMost {get; set;}

属性值
如果为 true,则将窗体显示为最顶层窗体;否则,为 false。默认为 false。

TopMost只是设置窗口为本程序的最顶层窗口。

而不是系统的,要设置系统的,可以用API:setwindowspos:

[DllImport("user32.dll")]
private static extern int SetWindowPos (int hwnd, int hWndInsertAfter, int x, int y, int cx, int cy, int wFlags);
//int x:窗口新的x坐标。如hwnd是一个子窗口,则x用父窗口的客户区坐标表示
//int y:窗口新的y坐标。如hwnd是一个子窗口,则y用父窗口的客户区坐标表示
//int cx:指定新的窗口宽度
//int cy:指定新的窗口高度
private const int HWND_TOPMOST = -1;
private const int SWP_SHOWWINDOW = 0x40;

SetWindowPos(this.Handle, HWND_TOPMOST, 0, 0, 500, 500, 0x40);

No comments: