1 //
2 // System.Windows.Forms.Design.Native
3 //
4 // Authors:
5 //	  Ivan N. Zlatev (contact i-nZ.net)
6 //
7 // (C) 2006-2007 Ivan N. Zlatev
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28 
29 using System;
30 using System.Drawing;
31 using System.Windows.Forms;
32 using System.Runtime.InteropServices;
33 using System.Reflection;
34 
35 
36 namespace System.Windows.Forms.Design
37 {
38 
39 	internal class Native
40 	{
41 
42 		private static Type _xplatuiType;
43 
Native()44 		static Native ()
45 		{
46 			Assembly assembly = Assembly.Load (Consts.AssemblySystem_Windows_Forms);
47 			if (assembly == null)
48 				throw new InvalidOperationException ("Can't load System.Windows.Forms assembly.");
49 
50 			_xplatuiType = assembly.GetType ("System.Windows.Forms.XplatUI");
51 			if (_xplatuiType == null)
52 				throw new InvalidOperationException ("Can't find the System.Windows.Forms.XplatUI type.");
53 		}
54 
InvokeMethod(string methodName, object[] args)55 		private static object InvokeMethod (string methodName, object[] args)
56 		{
57 			return InvokeMethod (methodName, args, null);
58 		}
59 
60 		// will also match types
InvokeMethod(string methodName, object[] args, Type[] types)61 		private static object InvokeMethod (string methodName, object[] args, Type[] types)
62 		{
63 			MethodInfo method = null;
64 
65 			if (types != null) {
66 				method = _xplatuiType.GetMethod (methodName, BindingFlags.NonPublic | BindingFlags.Static |
67 																BindingFlags.InvokeMethod, null, types, null);
68 			} else {
69 				method = _xplatuiType.GetMethod (methodName, BindingFlags.NonPublic | BindingFlags.Static |
70 												 BindingFlags.InvokeMethod);
71 			}
72 
73 			if (method == null)
74 				throw new InvalidOperationException (methodName + " not found!");
75 
76 			return method.Invoke (null, args);
77 		}
78 
DefWndProc(ref Message m)79 		public static void DefWndProc (ref Message m)
80 		{
81 			object[] args = new object[] { m };
82 			m.Result = (IntPtr) InvokeMethod ("DefWndProc", args);
83 			m = (Message) args[0];
84 		}
85 
SendMessage(IntPtr hwnd, Msg message, IntPtr wParam, IntPtr lParam)86 		public static IntPtr SendMessage (IntPtr hwnd, Msg message, IntPtr wParam, IntPtr lParam)
87 		{
88 
89 			Assembly assembly = Assembly.Load (Consts.AssemblySystem_Windows_Forms);
90 			Type refType = assembly.GetType ("System.Windows.Forms.Message&");
91 			object[] args = new object[] { Message.Create (hwnd, (int)message, wParam, lParam) };
92 			InvokeMethod ("SendMessage", args, new Type[] { refType });
93 			return ((Message)args[0]).Result;
94 		}
95 
PointToClient(Control control, Point point)96 		public static Point PointToClient (Control control, Point point)
97 		{
98 			if (control == null)
99 				throw new ArgumentNullException ("control");
100 
101 			object[] args = new object[] { control.Handle, point.X, point.Y };
102 			InvokeMethod ("ScreenToClient", args);
103 			return new Point ((int) args[1], (int) args[2]);
104 		}
105 
SetParent(IntPtr childHandle, IntPtr parentHandle)106 		public static IntPtr SetParent (IntPtr childHandle, IntPtr parentHandle)
107 		{
108 			return (IntPtr) InvokeMethod ("SetParent", new object[] { childHandle, parentHandle });
109 		}
110 
111 
112 #region Helpers
HiWord(int dword)113 		public static int HiWord (int dword)
114 		{
115 			// 12345678 -> 12340000 -> 00001234
116 			return ((dword >> 16) & 0x0000ffff);
117 		}
118 
LoWord(int dword)119 		public static int LoWord (int dword)
120 		{
121 			// 12345678 -> 00005678
122 			return (dword & 0x0000ffff);
123 		}
124 
LParam(int hiword, int loword)125 		public static IntPtr LParam (int hiword, int loword)
126 		{
127 			// results [hiword|loword] dword
128 			//
129 			return (IntPtr)((loword << 16) | (hiword & 0x0000FFFF));
130 		}
131 #endregion
132 
133 		public enum Msg {
134 			WM_CREATE				 = 0x0001,
135 			WM_SETFOCUS			   = 0x0007,
136 			WM_PAINT				  = 0X000F,
137 			WM_CANCELMODE			 = 0x001F,
138 			WM_SETCURSOR			  = 0x0020,
139 			WM_CONTEXTMENU			= 0x007B,
140 			WM_NCHITTEST			  = 0x0084,
141 			//
142 			// AccessabilityObject
143 			//
144 			WM_GETOBJECT			  = 0x003D,
145 			//
146 			// Mouse input - Client area
147 			//
148 			WM_MOUSEFIRST			 = 0x0200,
149 			WM_MOUSEMOVE			  = 0x0200,
150 			WM_LBUTTONDOWN			= 0x0201,
151 			WM_LBUTTONUP			  = 0x0202,
152 			WM_LBUTTONDBLCLK		  = 0x0203,
153 			WM_RBUTTONDOWN			= 0x0204,
154 			WM_RBUTTONUP			  = 0x0205,
155 			WM_RBUTTONDBLCLK		  = 0x0206,
156 			WM_MBUTTONDOWN			= 0x0207,
157 			WM_MBUTTONUP			  = 0x0208,
158 			WM_MBUTTONDBLCLK		  = 0x0209,
159 			WM_MOUSEWHEEL			 = 0x020A,
160 			WM_MOUSELAST			  = 0x020A,
161 			WM_NCMOUSEHOVER		   = 0x02A0,
162 			WM_MOUSEHOVER			 = 0x02A1,
163 			WM_NCMOUSELEAVE		   = 0x02A2,
164 			WM_MOUSELEAVE			 = 0x02A3,
165 			//
166 			// Mouse input - Non-client area
167 			//
168 			WM_NCMOUSEMOVE			= 0x00A0,
169 			WM_NCLBUTTONDOWN		  = 0x00A1,
170 			WM_NCLBUTTONUP			= 0x00A2,
171 			WM_NCLBUTTONDBLCLK		= 0x00A3,
172 			WM_NCRBUTTONDOWN		  = 0x00A4,
173 			WM_NCRBUTTONUP			= 0x00A5,
174 			WM_NCRBUTTONDBLCLK		= 0x00A6,
175 			WM_NCMBUTTONDOWN		  = 0x00A7,
176 			WM_NCMBUTTONUP			= 0x00A8,
177 			WM_NCMBUTTONDBLCLK		= 0x00A9,
178 			//
179 			// Keyboard input
180 			//
181 			WM_KEYFIRST	   = 0x0100,
182 			WM_KEYDOWN		= 0x0100,
183 			WM_KEYUP		  = 0x0101,
184 			WM_CHAR		   = 0x0102,
185 			WM_DEADCHAR	   = 0x0103,
186 			WM_SYSKEYDOWN	 = 0x0104,
187 			WM_SYSKEYUP	   = 0x0105,
188 			WM_SYS1CHAR	   = 0x0106,
189 			WM_SYSDEADCHAR	= 0x0107,
190 			WM_KEYLAST		= 0x0108,
191 			//
192 			// Scrolling
193 			//
194 			WM_HSCROLL		= 0x0114,
195 			WM_VSCROLL		= 0x0115,
196 
197 			//
198 			// IME - International Text
199 			//
200 			WM_IME_SETCONTEXT		 = 0x0281,
201 			WM_IME_NOTIFY			 = 0x0282,
202 			WM_IME_CONTROL			= 0x0283,
203 			WM_IME_COMPOSITIONFULL	= 0x0284,
204 			WM_IME_SELECT			 = 0x0285,
205 			WM_IME_CHAR			   = 0x0286,
206 			WM_IME_REQUEST			= 0x0288,
207 			WM_IME_KEYDOWN			= 0x0290,
208 			WM_IME_KEYUP			  = 0x0291,
209 
210 			// MWF Custom msgs
211 			//
212 			WM_MOUSE_ENTER			= 0x0401,
213 		}
214 	}
215 }
216 
217