几个比较好用的 Windows API 在C#中的用法

#region Windows Api 定义

/// 
/// 模拟鼠标事件
/// 
/// 鼠标事件的Enum
/// X座标
/// Y座标
/// 
/// 
[DllImport("user32.dll")]
public static extern void mouse_event(MouseEvents dwFlags, long dx, long dy, long cButtons, long dwExtraInfo);

/// 
/// 定义模拟鼠标的常数
/// 
public enum MouseEvents : uint
{
    MOUSEEVENTF_LEFTDOWN = 0x2,
    MOUSEEVENTF_LEFTUP = 0x4,
    MOUSEEVENTF_MIDDLEDOWN = 0x20,
    MOUSEEVENTF_MIDDLEUP = 0x40,
    MOUSEEVENTF_MOVE = 0x1,
    MOUSEEVENTF_ABSOLUTE = 0x8000,
    MOUSEEVENTF_RIGHTDOWN = 0x8,
    MOUSEEVENTF_RIGHTUP = 0x10,
}

/// 
/// 按指定标题寻找窗口,以获得指定窗口的句柄,为空则返回零。
/// 
/// 如未知则设置为NULL
/// 窗口名
/// 整型数据,代表窗口句柄
[DllImport("user32.dll")]
public static extern int FindWindow(String lpClassName, String lpWindowName);

/// 
/// 根据窗口句柄,获得窗口的类名
/// 
/// 窗口句柄
/// 缓冲区
/// 最大字节数
/// 整型,代表类名
[DllImport("user32.dll")]
public static extern int GetClassName(int hwnd, System.Text.StringBuilder buf, int nMaxCount);

/// 
/// 根据窗口句柄,获得指定窗口的标题。
/// 
/// 窗口句柄
/// 缓冲区
/// 最大字节数
/// 返回标题
[DllImport("user32.dll")]
public static extern int GetWindowText(int hwnd, System.Text.StringBuilder buf, int nMaxCount);

/// 
/// 根据窗口句柄,设置指定窗口为前置窗口。
/// 
/// 窗口句柄
[DllImport("user32.dll")]
public static extern void SetForegroundWindow(int hwnd);

/// 
/// 根据窗口句柄和显示的样式,显示窗口。
/// 
/// 窗口句柄
/// 显示窗口的样式。
/// 成功与否。
[DllImport("user32.dll")]
public static extern bool ShowWindow(int hWnd, nCmdShow nCmdShow);
public enum nCmdShow : uint
{
    SW_FORCEMINIMIZE = 0x0,
    SW_HIDE = 0x1,
    SW_MAXIMIZE = 0x2,
    SW_MINIMIZE = 0x3,
    SW_RESTORE = 0x4,
    SW_SHOW = 0x5,
    SW_SHOWDEFAULT = 0x6,
    SW_SHOWMAXIMIZED = 0x7,
    SW_SHOWMINIMIZED = 0x8,
    SW_SHOWMINNOACTIVE = 0x9,
    SW_SHOWNA = 0xA,
    SW_SHOWNOACTIVATE = 0xB,
    SW_SHOWNORMAL = 0xC,
}

/// 
/// 捕捉当前鼠标位置
/// 
/// 传入参数,代表鼠标的当前位置
/// 
[DllImport("user32.dll")]
public static extern long GetCursorPos(ref System.Drawing.Point lpPoint);
#endregion
Contributors: FHL