1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
//调用系统函数 将鼠标移动到相应位置 [DllImport( "user32.dll" , EntryPoint = "SetCursorPos" )]
public
extern static bool SetCursorPos( int
x, int
y);
//获取当前鼠标的绝对位置 [StructLayout(LayoutKind.Sequential)] public
struct POINT
{
public
int X;
public
int Y;
}
[DllImport( "User32" )]
public
extern static bool GetCursorPos( out
POINT p);
//是否显示鼠标箭头 [DllImport( "User32" )]
public
extern static int ShowCursor( bool
bShow);
//调用系统函数 模拟鼠标事件函数
[DllImport( "user32" , EntryPoint = "mouse_event" )]
private
static extern int mouse_event( int
dwFlags, int
dx, int
dy, int
cButtons, int
dwExtraInfo);
例如: private
const int MOUSEEVENTF_LEFTDOWN = 0x0002; //模拟鼠标左键按下
private
const int MOUSEEVENTF_LEFTUP = 0x0004; //模拟鼠标左键抬起
private
const int MOUSEEVENTF_RIGHTDOWN = 0x0008; //模拟鼠标右键按下
private
const int MOUSEEVENTF_RIGHTUP = 0x0010; //模拟鼠标右键抬起
private
const int MOUSEEVENTF_ABSOLUTE = 0x8000; //鼠标绝对位置
mouse_event(MOUSEEVENTF_LEFTDOWN|MOUSEEVENTF_LEFTUP | MOUSEEVENTF_ABSOLUTE, 0, 0, 0, 0); //点击 其中 int dx, int dy没有效果
[DllImport( "user32.dll" )]
static
extern IntPtr SetActiveWindow(IntPtr hWnd); //激活窗体
[DllImport( "user32.dll" )]
[ return : MarshalAs(UnmanagedType.Bool)]
static
extern bool SetForegroundWindow(IntPtr hWnd); //把窗体顶层显示
//获取窗体的句柄,其中第一个参数可以为null,第二个参数是任务管理器中“应用程序的 名称” [DllImport( "user32.dll" )]
static
extern IntPtr FindWindow( string
strClass, string
strWindow);
//获取窗口的相对的位置 高宽 [DllImport( "user32.dll" )]
private
static extern bool GetWindowRect(IntPtr hWnd, out
RECT lpRect);
[DllImport( "user32.dll" )]
private
static extern bool IsZoomed(IntPtr hWnd); //判断窗口最大化,最大化则返回true
//以什么样的方式显示窗体 [DllImport( "user32.dll" )]
private
static extern bool ShowWindow(IntPtr hWnd, int
nCmdShow);
//ShowWindow参数 private
const int SW_SHOWNORMAL = 1;
private
const int SW_RESTORE = 9;
private
const int SW_SHOWNOACTIVATE = 4;
//SendMessage参数
private
const int WM_KEYDOWN = 0X100;
private
const int WM_KEYUP = 0X101;
private
const int WM_SYSCHAR = 0X106;
private
const int WM_SYSKEYUP = 0X105;
private
const int WM_SYSKEYDOWN = 0X104;
private
const int WM_CHAR = 0X102;
ShowWindow(ParenthWnd, SW_RESTORE); //还原窗口
|