프로젝트

일반

사용자정보

통계
| 개정판:

t1 / TFDContents / Assets / KinectScripts / Samples / MouseControl.cs @ 3

이력 | 보기 | 이력해설 | 다운로드 (7.49 KB)

1
// This script can be used to control the system mouse - position of the mouse cursor and clicks
2
// Author: Akhmad Makhsadov
3
//
4

    
5
using UnityEngine;
6
using System.Collections;
7
using System.Runtime.InteropServices;
8
using System;
9

    
10
public class MouseControl
11
{
12
    // Import function mouse_event() from WinApi
13
    [DllImport("User32.dll")] 
14
    private static extern void mouse_event(MouseFlags dwFlags, int dx, int dy, int dwData, System.UIntPtr dwExtraInfo);
15

    
16
    // Flags needed to specify the mouse action 
17
    [System.Flags]
18
    private enum MouseFlags 
19
	{ 
20
        Move = 0x0001, 
21
        LeftDown = 0x0002, 
22
        LeftUp = 0x0004, 
23
        RightDown = 0x0008,
24
        RightUp = 0x0010,
25
        Absolute = 0x8000,
26
    }
27
                
28
//    public static int MouseXSpeedCoef = 45000; // Cursor rate in Х direction
29
//    public static int MouseYSpeedCoef = 45000; // Cursor rate in Y direction
30

    
31
	[StructLayout(LayoutKind.Sequential)]
32
	public struct RECT
33
	{
34
		public int Left;        // x position of upper-left corner
35
		public int Top;         // y position of upper-left corner
36
		public int Right;       // x position of lower-right corner
37
		public int Bottom;      // y position of lower-right corner
38
	}
39
	
40
	[DllImport("user32.dll")]
41
	//[return: MarshalAs(UnmanagedType.Bool)]
42
	static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);
43
	
44
	[DllImport("user32.dll")]
45
	static extern IntPtr GetActiveWindow();
46

    
47
	enum GetWindow_Cmd : uint 
48
	{
49
		GW_HWNDFIRST = 0,
50
		GW_HWNDLAST = 1,
51
		GW_HWNDNEXT = 2,
52
		GW_HWNDPREV = 3,
53
		GW_OWNER = 4,
54
		GW_CHILD = 5,
55
		GW_ENABLEDPOPUP = 6
56
	}
57
	
58
	[DllImport("user32.dll", SetLastError = true)]
59
	static extern IntPtr GetWindow(IntPtr hWnd, GetWindow_Cmd uCmd);
60

    
61
	const int MONITOR_DEFAULTTONULL = 0;
62
	const int MONITOR_DEFAULTTOPRIMARY = 1;
63
	const int MONITOR_DEFAULTTONEAREST = 2;
64

    
65
	[DllImport("user32.dll")]
66
	static extern IntPtr MonitorFromWindow(IntPtr hwnd, uint dwFlags);
67

    
68
	[StructLayout(LayoutKind.Sequential)]
69
	private struct MONITORINFO
70
	{
71
		public int cbSize;
72
		public RECT rcMonitor;
73
		public RECT rcWork;
74
		public uint dwFlags;
75
	}
76

    
77
	[DllImport("user32.dll")]
78
	static extern bool GetMonitorInfo(IntPtr hMonitor, ref MONITORINFO lpmi);
79

    
80
	
81
//	private static int windowX = 0;
82
//	private static int windowY = 0;
83
//	private static int winSizeX = 0;
84
//	private static int winSizeY = 0;
85

    
86
	private static Vector2 monitorSize = Vector2.zero;
87
	private static MONITORINFO monitorInfo = new MONITORINFO();
88
	private static bool winRectPrinted = false;
89

    
90

    
91
    // Public function to move the mouse cursor to the specified position
92
    public static void MouseMove(Vector3 screenCoordinates, GUIText debugText)
93
    {
94
		int windowX = 0;
95
		int windowY = 0;
96
		int winSizeX = 0;
97
		int winSizeY = 0;
98

    
99
		bool isConvertToFullScreen = Screen.fullScreen;
100

    
101
		IntPtr hWnd = GetActiveWindow();
102
		hWnd = GetClosestWindow(hWnd, Screen.width, Screen.height);
103

    
104
		if (hWnd != IntPtr.Zero) 
105
		{
106
			RECT winRect;
107

    
108
			if (GetWindowRect(hWnd, out winRect)) 
109
			{
110
				winSizeX = winRect.Right - winRect.Left;
111
				winSizeY = winRect.Bottom - winRect.Top;
112

    
113
				windowX = winRect.Left + (winSizeX - (int)Screen.width) / 2;
114
				
115
				if (!isConvertToFullScreen)
116
				{
117
					windowY = winRect.Top + (winSizeY - (int)Screen.height + 36) / 2;
118
				}
119
				else
120
				{
121
					windowY = winRect.Top + (winSizeY - (int)Screen.height) / 2;
122
				}
123

    
124
				// get display resolution
125
				if (monitorSize == Vector2.zero)
126
				{
127
					monitorInfo.cbSize = Marshal.SizeOf (monitorInfo);
128

    
129
					IntPtr hMonitoŕ = MonitorFromWindow (hWnd, MONITOR_DEFAULTTONEAREST);
130
					if (!GetMonitorInfo (hMonitoŕ, ref monitorInfo))
131
					{
132
						monitorInfo.rcMonitor.Left = monitorInfo.rcMonitor.Top = 0;
133
						monitorInfo.rcMonitor.Right = Screen.currentResolution.width - 1;
134
						monitorInfo.rcMonitor.Bottom = Screen.currentResolution.height - 1;
135

    
136
						monitorInfo.rcWork.Left = monitorInfo.rcWork.Top = 0;
137
						monitorInfo.rcWork.Right = Screen.currentResolution.width - 1;
138
						monitorInfo.rcWork.Bottom = Screen.currentResolution.height - 1;
139
					}
140

    
141
					monitorSize.x = monitorInfo.rcMonitor.Right - monitorInfo.rcMonitor.Left + 1;
142
					monitorSize.y = monitorInfo.rcMonitor.Bottom - monitorInfo.rcMonitor.Top + 1;
143
				}
144

    
145
				if (!winRectPrinted)
146
				{
147
					Debug.Log (string.Format ("monSize: ({0}, {1})", monitorSize.x, monitorSize.y));
148
					Debug.Log (string.Format ("scrSize: ({0}, {1})", Screen.width, Screen.height));
149
					Debug.Log (string.Format ("winRect: ({0}, {1}, {2}, {3})", winRect.Left, winRect.Top, winRect.Right, winRect.Bottom));
150
					Debug.Log (string.Format ("winPos: ({0}, {1})", windowX, windowY));
151
					winRectPrinted = true;
152
				}
153
			}
154
		}
155
		else 
156
		{
157
			if (monitorSize == Vector2.zero) 
158
			{
159
				monitorSize.x = Screen.currentResolution.width;
160
				monitorSize.y = Screen.currentResolution.height;
161
			}
162
		}
163

    
164
		int mouseX = 0;
165
		int mouseY = 0;
166

    
167
		if(!isConvertToFullScreen)
168
		{
169
			float screenX = windowX + screenCoordinates.x * Screen.width;
170
			float screenY = windowY + (1f - screenCoordinates.y) * Screen.height;
171

    
172
			float screenRelX = screenX / monitorSize.x;
173
			float screenRelY = screenY / monitorSize.y;
174
			
175
//			if(debugText)
176
//			{
177
//				if(!debugText.text.Contains("ScrPos"))
178
//				{
179
//					string sDebug = string.Format("\nScrPos: ({0:F0}, {1:F0})", screenX, screenY);
180
//					debugText.text += sDebug;
181
//					//Debug.Log (sDebug);
182
//				}
183
//			}
184

    
185
			mouseX = (int)(screenRelX * 65535);
186
			mouseY = (int)(screenRelY * 65535);
187
		}
188
		else
189
		{
190
			mouseX = (int)(screenCoordinates.x * 65535);
191
			mouseY = (int)((1f - screenCoordinates.y) * 65535);
192
		}
193

    
194
		mouse_event(MouseFlags.Absolute | MouseFlags.Move, mouseX, mouseY, 0, System.UIntPtr.Zero);
195
    }
196

    
197
	// find the closest matching child window to the screen size
198
	private static IntPtr GetClosestWindow(IntPtr hWndMain, int scrWidth, int scrHeight)
199
	{
200
		if(hWndMain == IntPtr.Zero)
201
			return hWndMain;
202
		
203
		IntPtr hWnd = hWndMain;
204
		RECT winRect;
205
		
206
		if(GetWindowRect(hWndMain, out winRect))
207
		{
208
			int winSizeX = winRect.Right - winRect.Left;
209
			int winSizeY = winRect.Bottom - winRect.Top;
210
			int winDiff = Math.Abs(winSizeX - scrWidth) + Math.Abs(winSizeY - scrHeight);
211

    
212
			IntPtr hWndChild = GetWindow(hWndMain, GetWindow_Cmd.GW_CHILD);
213
			int winDiffMin = winDiff;
214
			
215
			while(hWndChild != IntPtr.Zero)
216
			{
217
				if(GetWindowRect(hWndChild, out winRect))
218
				{
219
					winSizeX = winRect.Right - winRect.Left;
220
					winSizeY = winRect.Bottom - winRect.Top;
221
					winDiff = Math.Abs(winSizeX - scrWidth) + Math.Abs(winSizeY - scrHeight - 36);
222
					
223
					if(scrWidth <= winSizeX && scrHeight <= winSizeY && winDiff <= winDiffMin)
224
					{
225
						hWnd = hWndChild;
226
						winDiffMin = winDiff;
227
					}
228
				}
229
				
230
				hWndChild = GetWindow(hWndChild, GetWindow_Cmd.GW_HWNDNEXT);
231
			}
232
		}
233
		
234
		return hWnd;
235
	}
236
	
237
	// Public function to emulate a mouse button click (left button)
238
    public static void MouseClick()
239
    {
240
        mouse_event(MouseFlags.LeftDown, 0, 0, 0, System.UIntPtr.Zero);
241
        mouse_event(MouseFlags.LeftUp, 0, 0, 0, System.UIntPtr.Zero);
242
    }
243
	
244
    // Public function to emulate a mouse drag event (left button)
245
    public static void MouseDrag()
246
    {
247
        mouse_event(MouseFlags.LeftDown, 0, 0, 0, System.UIntPtr.Zero);
248
    }
249
	
250
    // Public function to emulate a mouse release event (left button)
251
    public static void MouseRelease()
252
    {
253
        mouse_event(MouseFlags.LeftUp, 0, 0, 0, System.UIntPtr.Zero);
254
    }
255
	
256
}
257

    
258