溫馨提示×

在C#中使用SetWindowPos來移動窗口

c#
小云
300
2023-09-26 03:06:37
欄目: 編程語言

在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參數來保持窗口尺寸和位置不變,只移動窗口。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女