在C#中使用SetWindowPos来移动窗口

   2025-02-20 9390
核心提示:在C#中,可以使用SetWindowPos函数来移动窗口。以下是一个示例代码:using System;using System.Runtime.InteropServices;namesp

在C#中,可以使用SetWindowPos函数来移动窗口。以下是一个示例代码:

using System;using System.Runtime.InteropServices;namespace WindowMovement{class Program{// 导入SetWindowPos函数[DllImport("user32.dll")]public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);// 定义窗口句柄常量const int HWND_TOP = 0;const uint SWP_NOSIZE = 0x0001;const uint SWP_NOMOVE = 0x0002;static void Main(string[] args){// 获取当前程序的窗口句柄IntPtr hWnd = System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle;// 移动窗口到新的位置(例如:将窗口移动到坐标(100,100)的位置)SetWindowPos(hWnd, (IntPtr)HWND_TOP, 100, 100, 0, 0, SWP_NOSIZE | SWP_NOMOVE);}}}

在上述代码中,SetWindowPos函数用来移动窗口的位置。它接受多个参数,包括窗口句柄、要插入的窗口句柄、新的窗口位置的X和Y坐标、窗口尺寸的cx和cy、以及一些标志位参数。在示例中,我们使用了HWND_TOP参数来将窗口置于最顶层,并使用SWP_NOSIZESWP_NOMOVE参数来保持窗口尺寸和位置不变,只移动窗口。

 
 
更多>同类维修知识
推荐图文
推荐维修知识
点击排行
网站首页  |  关于我们  |  联系方式  |  用户协议  |  隐私政策  |  网站留言