Windows 用户界面 (UI) API 是一组用于创建和管理 Windows 窗口、控件以及处理用户输入的函数和数据结构。这些 API 可以通过 C# 中的 Platform Invocation Services (P/Invoke) 来调用。下面是一些常用的 Windows 用户界面 API,以及它们在 C# 中的使用示例。
C#[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);
}
C#[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");
}
C#[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);
}
C#[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 表示隐藏窗口
}
C#[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
}
C#[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;
}
C#[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,表示对所有子窗口进行平铺。C#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 进行窗口操作和鼠标位置控制。如果您有任何其他问题,欢迎随时向我提问。
本文作者:技术老小子
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!