Windows 用户界面 (UI) API 是一组用于创建和管理 Windows 窗口、控件以及处理用户输入的函数和数据结构。这些 API 可以通过 C# 中的 Platform Invocation Services (P/Invoke) 来调用。下面是一些常用的 Windows 用户界面 API,以及它们在 C# 中的使用示例。
MessageBox: 显示一个消息框
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int MessageBox(IntPtr hWnd, string text, string caption, uint type);
private void btnMessageBox_Click(object sender, EventArgs e)
{
string message = "Hello, this is a message!";
string caption = "MessageBox Example";
MessageBox(IntPtr.Zero, message, caption, 0);
}
- hWnd: 父窗口的句柄,如果为 IntPtr.Zero,则消息框将显示在屏幕中央。
- text: 要显示的消息文本。
- caption: 消息框的标题。
- type: 消息框的类型,例如可以是 0 表示普通消息框。 0: 显示一个包含“确定”按钮的消息框。 1: 显示一个包含“确定”和“取消”按钮的消息框。 2: 显示一个包含“中止”、“重试”和“忽略”按钮的消息框。 3: 显示一个包含“是”、“否”和“取消”按钮的消息框。 4: 显示一个包含“是”和“否”按钮的消息框。 5: 显示一个包含“重试”和“取消”按钮的消息框。 6: 显示一个包含“取消”、“重试”和“继续”按钮的消息框。
SetWindowText: 设置窗口文本
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool SetWindowText(IntPtr hWnd, string lpString);
private void btnSetWindowText_Click(object sender, EventArgs e)
{
IntPtr hwnd = this.Handle; // 获取窗口句柄
SetWindowText(hwnd, "New Window Text");
}
- hWnd: 窗口的句柄。
- lpString: 要设置的窗口文本。
GetWindowText: 获取窗口文本
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
private void btnGetWindowText_Click(object sender, EventArgs e)
{
IntPtr hwnd = this.Handle; // 获取窗口句柄
StringBuilder sb = new StringBuilder(256);
GetWindowText(hwnd, sb, 256);
string windowText = sb.ToString();
System.Windows.Forms.MessageBox.Show(windowText);
}
- hWnd: 窗口的句柄。
- lpString: 用于接收窗口文本的 StringBuilder 对象。
- nMaxCount: lpString 的最大容量。
ShowWindow: 显示或隐藏窗口
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
private void btnShowWindow_Click(object sender, EventArgs e)
{
string className = "Notepad"; // 要查找的窗口类名
string windowName = "Untitled - Notepad"; // 要查找的窗口标题
IntPtr hWnd = FindWindow(className, windowName);// 获取窗口句柄
ShowWindow(hWnd, 1); // 1 表示显示窗口
// 或者
//ShowWindow(hWnd, 0); // 0 表示隐藏窗口
}
- hWnd: 窗口的句柄。
- nCmdShow: 控制窗口如何显示的参数,例如可以是 1 表示显示窗口,0 表示隐藏窗口。
SetWindowPos: 设置窗口位置
[DllImport("user32.dll")]
public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
private void btnSetWindowPos_Click(object sender, EventArgs e)
{
string className = "Notepad"; // 要查找的窗口类名
string windowName = "Untitled - Notepad"; // 要查找的窗口标题
IntPtr hWnd = FindWindow(className, windowName);// 获取窗口句柄
SetWindowPos(hWnd, IntPtr.Zero, 100, 100, 200, 200, 0x0040); // 0x0040 表示 SWP_NOMOVE
}
- hWnd: 窗口的句柄。
- hWndInsertAfter: 确定窗口的 Z 顺序。
- X, Y: 窗口左上角的位置。
- cx, cy: 窗口的宽度和高度。
- uFlags: 控制窗口大小和位置的标志。 SWP_NOSIZE (0x0001): 维持当前大小(忽略 cx 和 cy 参数)。 SWP_NOMOVE (0x0002): 维持当前位置(忽略 X 和 Y 参数)。 SWP_NOZORDER (0x0004): 维持当前 Z 顺序(hWndInsertAfter 参数被忽略)。 SWP_NOREDRAW (0x0008): 不重绘改变的部分。 SWP_NOACTIVATE (0x0010): 不激活窗口。 SWP_FRAMECHANGED (0x0020): 强制发送 WM_NCCALCSIZE 消息,即使窗口的大小和位置没有改变。 SWP_SHOWWINDOW (0x0040): 显示窗口。 SWP_HIDEWINDOW (0x0080): 隐藏窗口。 SWP_NOCOPYBITS (0x0100): 不复制客户区到屏幕上。 SWP_NOOWNERZORDER (0x0200): 不改变 z 顺序的所有者窗口的位置。 SWP_NOSENDCHANGING (0x0400): 不发送 WM_WINDOWPOSCHANGING 和 WM_WINDOWPOSCHANGED 消息。
GetCursorPos: 获取鼠标光标位置
[StructLayout(LayoutKind.Sequential)]
public struct POINT
{
public int X;
public int Y;
}
[DllImport("user32.dll")]
public static extern bool GetCursorPos(out POINT lpPoint);
private void btnGetCursorPos_Click(object sender, EventArgs e)
{
POINT point;
GetCursorPos(out point);
this.Text = "Cursor Position X: " + point.X + " Y: " + point.Y;
}
- lpPoint: 用于接收鼠标位置的 POINT 结构体。
TileWindows:将指定的窗口按照指定的排列方式进行平铺。
[DllImport("user32.dll")]
public static extern bool TileWindows(IntPtr hwndParent, uint wHow, IntPtr lpRect, uint cKids, IntPtr lpKids);
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr GetDesktopWindow();
private void btnTileWindows_Click(object sender, EventArgs e)
{
// 获取桌面窗口的句柄
IntPtr desktopHandle = GetDesktopWindow();
// 平铺窗口
bool result = TileWindows(desktopHandle, 0, IntPtr.Zero, 0, IntPtr.Zero);
if (!result)
{
int error = Marshal.GetLastWin32Error();
Console.WriteLine("Failed to tile windows. Error code: " + error);
}
}
- hwndParent: 指定要进行平铺操作的父窗口的句柄。通常可以使用桌面窗口的句柄来进行整个桌面窗口的平铺操作。
- wHow: 指定平铺的方式。可以是以下值之一: MDITILE_HORIZONTAL (0x0000): 水平平铺多文档界面 (MDI) 子窗口。 MDITILE_VERTICAL (0x0001): 垂直平铺 MDI 子窗口。 MDITILE_SKIPDISABLED (0x0002): 仅包括可见的 MDI 子窗口。跳过隐藏或最小化的 MDI 子窗口。
- lpRect: 一个指向矩形结构的指针,用于指定平铺操作的区域。通常可以传入 IntPtr.Zero,表示使用整个父窗口的客户区域进行平铺。
- cKids: 子窗口的数量。通常可以传入 0,表示对所有子窗口进行平铺。
- lpKids: 一个指向子窗口句柄数组的指针,用于指定要进行平铺操作的子窗口。通常可以传入 IntPtr.Zero,表示对所有子窗口进行平铺。
EnumDesktopWindows:函数用于枚举桌面上的顶层窗口。
public delegate bool EnumWindowsProc(IntPtr hWnd, int lParam);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool EnumDesktopWindows(IntPtr hDesktop, EnumWindowsProc lpfn, IntPtr lParam);
private void btnEnumDesktopWindows_Click(object sender, EventArgs e)
{
EnumDesktopWindows(IntPtr.Zero, new EnumWindowsProc(EnumWindowCallback), IntPtr.Zero);
}
bool EnumWindowCallback(IntPtr hWnd, int lParam)
{
// 打印窗口句柄
StringBuilder sb = new StringBuilder(256);
GetWindowText(hWnd, sb, 256);
listBox1.Items.Add(sb.ToString());
return true; // 返回 true 以继续枚举
}
- hDesktop: 指定要枚举窗口的桌面句柄。通常可以传入 IntPtr.Zero,表示枚举默认桌面上的窗口。
- lpfn: 一个指向 EnumWindowsProc 委托的指针,该委托是用于枚举窗口的回调函数。回调函数的定义应该是 bool EnumWindowsProc(IntPtr hWnd, int lParam)。
- lParam: 一个用户定义的参数,会传递给回调函数。可以用于传递额外的信息给回调函数。
希望这些示例能够帮助您了解如何在 C# 中使用 Windows 用户界面 API 进行窗口操作和鼠标位置控制。如果您有任何其他问题,欢迎随时向我提问。