您的位置:首页 > 其它

如何使任意Windows窗口置顶

2009-01-16 08:55 387 查看
在论坛中看到有人问如何可以让任意Windows窗口置顶,这里其实可以使用Windows API函数SetWindowsPos做到。以下是示例代码:

示例代码演示将一个新打开的记事本程序置顶

1 [DllImport("user32.dll", SetLastError = true)]

2 public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

3

4 [DllImport("user32.dll")]

5 public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy, uint uFlags);

6

7 public static readonly IntPtr HWND_TOPMOST = new IntPtr(-1); //窗体置顶

8 public static readonly IntPtr HWND_NOTOPMOST = new IntPtr(-2); //取消窗体置顶

9 public const uint SWP_NOMOVE = 0x0002; //不调整窗体位置

10 public const uint SWP_NOSIZE = 0x0001; //不调整窗体大小

11

12 private void button1_Click(object sender, EventArgs e)

13 {

14 //找到默认的打开的记事本程序

15 IntPtr notepadHandle = FindWindow(null, "无标题 - 记事本");

16 if (notepadHandle == null || notepadHandle == IntPtr.Zero)

17 return;

18 SetWindowPos(notepadHandle, HWND_TOPMOST, 1, 1, 1, 1, SWP_NOMOVE | SWP_NOSIZE);

19 }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: