XplatUIWin32.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  1. // Permission is hereby granted, free of charge, to any person obtaining
  2. // a copy of this software and associated documentation files (the
  3. // "Software"), to deal in the Software without restriction, including
  4. // without limitation the rights to use, copy, modify, merge, publish,
  5. // distribute, sublicense, and/or sell copies of the Software, and to
  6. // permit persons to whom the Software is furnished to do so, subject to
  7. // the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be
  10. // included in all copies or substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  13. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  14. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  15. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  16. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  17. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  18. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  19. //
  20. // Copyright (c) 2004 Novell, Inc.
  21. //
  22. // Authors:
  23. // Peter Bartok [email protected]
  24. //
  25. //
  26. // $Revision: 1.13 $
  27. // $Modtime: $
  28. // $Log: XplatUIWin32.cs,v $
  29. // Revision 1.13 2004/08/11 22:20:59 pbartok
  30. // - Signature fixes
  31. //
  32. // Revision 1.12 2004/08/11 19:41:38 jordi
  33. // Fixes ClientRect
  34. //
  35. // Revision 1.11 2004/08/11 19:19:44 pbartok
  36. // - We had SetWindowPos and MoveWindow to set window positions and size,
  37. // removed MoveWindow. We have GetWindowPos, so it made sense to keep
  38. // SetWindowPos as matching counterpart
  39. // - Added some X11 sanity checking
  40. //
  41. // Revision 1.10 2004/08/11 18:55:46 pbartok
  42. // - Added method to calculate difference between decorated window and raw
  43. // client area
  44. //
  45. // Revision 1.9 2004/08/10 18:47:16 jordi
  46. // Calls InvalidateRect before UpdateWindow
  47. //
  48. // Revision 1.8 2004/08/10 17:36:17 pbartok
  49. // - Implemented several methods
  50. //
  51. // Revision 1.7 2004/08/09 20:55:59 pbartok
  52. // - Removed Run method, was only required for initial development
  53. //
  54. // Revision 1.6 2004/08/09 20:51:25 pbartok
  55. // - Implemented GrabWindow/ReleaseWindow methods to allow pointer capture
  56. //
  57. // Revision 1.5 2004/08/09 16:05:16 jackson
  58. // These properties are handled by the theme now.
  59. //
  60. // Revision 1.4 2004/08/06 15:53:39 jordi
  61. // X11 keyboard navigation
  62. //
  63. // Revision 1.3 2004/08/04 20:11:24 pbartok
  64. // - Added Invalidate handling
  65. //
  66. // Revision 1.2 2004/07/21 16:19:17 jordi
  67. // LinkLabel control implementation
  68. //
  69. // Revision 1.1 2004/07/09 05:21:25 pbartok
  70. // - Initial check-in
  71. //
  72. //
  73. // NOT COMPLETE
  74. using System;
  75. using System.Drawing;
  76. using System.ComponentModel;
  77. using System.Collections;
  78. using System.Diagnostics;
  79. using System.Runtime.InteropServices;
  80. /// Win32 Version
  81. namespace System.Windows.Forms {
  82. internal class XplatUIWin32 : XplatUIDriver {
  83. #region Local Variables
  84. private static XplatUIWin32 instance;
  85. private static int ref_count;
  86. private static IntPtr FosterParent;
  87. internal static Keys key_state;
  88. internal static MouseButtons mouse_state;
  89. internal static Point mouse_position;
  90. internal static WndProc wnd_proc;
  91. internal static bool themes_enabled;
  92. #endregion // Local Variables
  93. #region Private Structs
  94. [StructLayout(LayoutKind.Sequential)]
  95. private struct WNDCLASS {
  96. internal int style;
  97. internal WndProc lpfnWndProc;
  98. internal int cbClsExtra;
  99. internal int cbWndExtra;
  100. internal IntPtr hInstance;
  101. internal IntPtr hIcon;
  102. internal IntPtr hCursor;
  103. internal IntPtr hbrBackground;
  104. internal string lpszMenuName;
  105. internal string lpszClassName;
  106. }
  107. [StructLayout(LayoutKind.Sequential)]
  108. private struct RECT {
  109. internal int left;
  110. internal int top;
  111. internal int right;
  112. internal int bottom;
  113. }
  114. [StructLayout(LayoutKind.Sequential)]
  115. private struct PAINTSTRUCT {
  116. internal IntPtr hdc;
  117. internal int fErase;
  118. internal RECT rcPaint;
  119. internal int fRestore;
  120. internal int fIncUpdate;
  121. internal int Reserved1;
  122. internal int Reserved2;
  123. internal int Reserved3;
  124. internal int Reserved4;
  125. internal int Reserved5;
  126. internal int Reserved6;
  127. internal int Reserved7;
  128. internal int Reserved8;
  129. }
  130. internal enum ClassStyle {
  131. CS_VREDRAW = 0x00000001,
  132. CS_HREDRAW = 0x00000002,
  133. CS_KEYCVTWINDOW = 0x00000004,
  134. CS_DBLCLKS = 0x00000008,
  135. CS_OWNDC = 0x00000020,
  136. CS_CLASSDC = 0x00000040,
  137. CS_PARENTDC = 0x00000080,
  138. CS_NOKEYCVT = 0x00000100,
  139. CS_NOCLOSE = 0x00000200,
  140. CS_SAVEBITS = 0x00000800,
  141. CS_BYTEALIGNCLIENT = 0x00001000,
  142. CS_BYTEALIGNWINDOW = 0x00002000,
  143. CS_GLOBALCLASS = 0x00004000,
  144. CS_IME = 0x00010000
  145. }
  146. internal enum PeekMessageFlags {
  147. PM_NOREMOVE = 0x00000000,
  148. PM_REMOVE = 0x00000001,
  149. PM_NOYIELD = 0x00000002
  150. }
  151. internal enum VirtualKeys {
  152. VK_LBUTTON = 0x01,
  153. VK_RBUTTON = 0x02,
  154. VK_CANCEL = 0x03,
  155. VK_MBUTTON = 0x04,
  156. VK_XBUTTON1 = 0x05,
  157. VK_XBUTTON2 = 0x06,
  158. VK_BACK = 0x08,
  159. VK_TAB = 0x09,
  160. VK_CLEAR = 0x0C,
  161. VK_RETURN = 0x0D,
  162. VK_SHIFT = 0x10,
  163. VK_CONTROL = 0x11,
  164. VK_MENU = 0x12,
  165. VK_CAPITAL = 0x14,
  166. VK_ESCAPE = 0x1B,
  167. VK_SPACE = 0x20,
  168. VK_PRIOR = 0x21,
  169. VK_NEXT = 0x22,
  170. VK_END = 0x23,
  171. VK_HOME = 0x24,
  172. VK_LEFT = 0x25,
  173. VK_UP = 0x26,
  174. VK_RIGHT = 0x27,
  175. VK_DOWN = 0x28,
  176. VK_SELECT = 0x29,
  177. VK_EXECUTE = 0x2B,
  178. VK_SNAPSHOT = 0x2C,
  179. VK_HELP = 0x2F,
  180. VK_0 = 0x30,
  181. VK_1 = 0x31,
  182. VK_2 = 0x32,
  183. VK_3 = 0x33,
  184. VK_4 = 0x34,
  185. VK_5 = 0x35,
  186. VK_6 = 0x36,
  187. VK_7 = 0x37,
  188. VK_8 = 0x38,
  189. VK_9 = 0x39,
  190. VK_A = 0x41,
  191. VK_B = 0x42,
  192. VK_C = 0x43,
  193. VK_D = 0x44,
  194. VK_E = 0x45,
  195. VK_F = 0x46,
  196. VK_G = 0x47,
  197. VK_H = 0x48,
  198. VK_I = 0x49,
  199. VK_J = 0x4A,
  200. VK_K = 0x4B,
  201. VK_L = 0x4C,
  202. VK_M = 0x4D,
  203. VK_N = 0x4E,
  204. VK_O = 0x4F,
  205. VK_P = 0x50,
  206. VK_Q = 0x51,
  207. VK_R = 0x52,
  208. VK_S = 0x53,
  209. VK_T = 0x54,
  210. VK_U = 0x55,
  211. VK_V = 0x56,
  212. VK_W = 0x57,
  213. VK_X = 0x58,
  214. VK_Y = 0x59,
  215. VK_Z = 0x5A,
  216. VK_NUMPAD0 = 0x60,
  217. VK_NUMPAD1 = 0x61,
  218. VK_NUMPAD2 = 0x62,
  219. VK_NUMPAD3 = 0x63,
  220. VK_NUMPAD4 = 0x64,
  221. VK_NUMPAD5 = 0x65,
  222. VK_NUMPAD6 = 0x66,
  223. VK_NUMPAD7 = 0x67,
  224. VK_NUMPAD8 = 0x68,
  225. VK_NUMPAD9 = 0x69,
  226. VK_MULTIPLY = 0x6A,
  227. VK_ADD = 0x6B,
  228. VK_SEPARATOR = 0x6C,
  229. VK_SUBTRACT = 0x6D,
  230. VK_DECIMAL = 0x6E,
  231. VK_DIVIDE = 0x6F,
  232. VK_ATTN = 0xF6,
  233. VK_CRSEL = 0xF7,
  234. VK_EXSEL = 0xF8,
  235. VK_EREOF = 0xF9,
  236. VK_PLAY = 0xFA,
  237. VK_ZOOM = 0xFB,
  238. VK_NONAME = 0xFC,
  239. VK_PA1 = 0xFD,
  240. VK_OEM_CLEAR = 0xFE,
  241. VK_LWIN = 0x5B,
  242. VK_RWIN = 0x5C,
  243. VK_APPS = 0x5D,
  244. VK_LSHIFT = 0xA0,
  245. VK_RSHIFT = 0xA1,
  246. VK_LCONTROL = 0xA2,
  247. VK_RCONTROL = 0xA3,
  248. VK_LMENU = 0xA4,
  249. VK_RMENU = 0xA5
  250. }
  251. private enum GetSysColorIndex {
  252. COLOR_SCROLLBAR =0,
  253. COLOR_BACKGROUND =1,
  254. COLOR_ACTIVECAPTION =2,
  255. COLOR_INACTIVECAPTION =3,
  256. COLOR_MENU =4,
  257. COLOR_WINDOW =5,
  258. COLOR_WINDOWFRAME =6,
  259. COLOR_MENUTEXT =7,
  260. COLOR_WINDOWTEXT =8,
  261. COLOR_CAPTIONTEXT =9,
  262. COLOR_ACTIVEBORDER =10,
  263. COLOR_INACTIVEBORDER =11,
  264. COLOR_APPWORKSPACE =12,
  265. COLOR_HIGHLIGHT =13,
  266. COLOR_HIGHLIGHTTEXT =14,
  267. COLOR_BTNFACE =15,
  268. COLOR_BTNSHADOW =16,
  269. COLOR_GRAYTEXT =17,
  270. COLOR_BTNTEXT =18,
  271. COLOR_INACTIVECAPTIONTEXT =19,
  272. COLOR_BTNHIGHLIGHT =20,
  273. COLOR_3DDKSHADOW =21,
  274. COLOR_3DLIGHT =22,
  275. COLOR_INFOTEXT =23,
  276. COLOR_INFOBK =24,
  277. COLOR_DESKTOP =1,
  278. COLOR_3DFACE =16,
  279. COLOR_3DSHADOW =16,
  280. COLOR_3DHIGHLIGHT =20,
  281. COLOR_3DHILIGHT =20,
  282. COLOR_BTNHILIGHT =20
  283. }
  284. private enum LoadCursorType {
  285. IDC_ARROW =32512,
  286. IDC_IBEAM =32513,
  287. IDC_WAIT =32514,
  288. IDC_CROSS =32515,
  289. IDC_UPARROW =32516,
  290. IDC_SIZE =32640,
  291. IDC_ICON =32641,
  292. IDC_SIZENWSE =32642,
  293. IDC_SIZENESW =32643,
  294. IDC_SIZEWE =32644,
  295. IDC_SIZENS =32645,
  296. IDC_SIZEALL =32646,
  297. IDC_NO =32648,
  298. IDC_HAND =32649,
  299. IDC_APPSTARTING =32650,
  300. IDC_HELP =32651
  301. }
  302. #endregion
  303. #region Constructor & Destructor
  304. private XplatUIWin32() {
  305. WNDCLASS wndClass;
  306. bool result;
  307. // Handle singleton stuff first
  308. ref_count=0;
  309. // Now regular initialization
  310. key_state = Keys.None;
  311. mouse_state = MouseButtons.None;
  312. mouse_position = Point.Empty;
  313. themes_enabled = false;
  314. // Prepare 'our' window class
  315. wnd_proc = new WndProc(NativeWindow.WndProc);
  316. wndClass.style = (int)ClassStyle.CS_OWNDC;
  317. wndClass.lpfnWndProc = wnd_proc;
  318. wndClass.cbClsExtra = 0;
  319. wndClass.cbWndExtra = 0;
  320. wndClass.hbrBackground = (IntPtr)(GetSysColorIndex.COLOR_BTNFACE+1);
  321. wndClass.hCursor = Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);
  322. wndClass.hIcon = IntPtr.Zero;
  323. wndClass.hInstance = IntPtr.Zero;
  324. wndClass.lpszClassName = XplatUI.DefaultClassName;
  325. wndClass.lpszMenuName = "";
  326. result=Win32RegisterClass(ref wndClass);
  327. if (result==false) {
  328. Win32MessageBox(IntPtr.Zero, "Could not register the "+XplatUI.DefaultClassName+" window class, win32 error " + Win32GetLastError().ToString(), "Oops", 0);
  329. }
  330. FosterParent=Win32CreateWindow(0, "Static", "Foster Parent Window", (int)WindowStyles.WS_OVERLAPPEDWINDOW, 0, 0, 0, 0, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
  331. if (FosterParent==IntPtr.Zero) {
  332. Win32MessageBox(IntPtr.Zero, "Could not create foster window, win32 error " + Win32GetLastError().ToString(), "Oops", 0);
  333. }
  334. Console.WriteLine("#region #line XplatUI WIN32 Constructor called, result {0}", result);
  335. }
  336. ~XplatUIWin32() {
  337. Console.WriteLine("XplatUI Destructor called");
  338. }
  339. #endregion // Constructor & Destructor
  340. #region Private Support Methods
  341. private static IntPtr DefWndProc(IntPtr hWnd, Msg msg, IntPtr wParam, IntPtr lParam) {
  342. return Win32DefWindowProc(hWnd, msg, wParam, lParam);
  343. }
  344. #endregion // Private Support Methods
  345. #region Static Properties
  346. internal override Keys ModifierKeys {
  347. get {
  348. return key_state;
  349. }
  350. }
  351. internal override MouseButtons MouseButtons {
  352. get {
  353. return mouse_state;
  354. }
  355. }
  356. internal override Point MousePosition {
  357. get {
  358. return mouse_position;
  359. }
  360. }
  361. internal override bool DropTarget {
  362. get {
  363. return false;
  364. }
  365. set {
  366. if (value) {
  367. throw new NotImplementedException("Need to figure out D'n'D for Win32");
  368. }
  369. }
  370. }
  371. #endregion // Static Properties
  372. #region Singleton Specific Code
  373. public static XplatUIWin32 GetInstance() {
  374. if (instance==null) {
  375. instance=new XplatUIWin32();
  376. }
  377. ref_count++;
  378. return instance;
  379. }
  380. public int Reference {
  381. get {
  382. return ref_count;
  383. }
  384. }
  385. #endregion
  386. #region Public Static Methods
  387. internal override IntPtr InitializeDriver() {
  388. key_state=Keys.None;
  389. mouse_state=MouseButtons.None;
  390. mouse_position=Point.Empty;
  391. Console.WriteLine("#region #line XplatUI Win32 Constructor called");
  392. return IntPtr.Zero;
  393. }
  394. internal override void ShutdownDriver(IntPtr token) {
  395. Console.WriteLine("XplatUIWin32 ShutdownDriver called");
  396. }
  397. internal void Version() {
  398. Console.WriteLine("Xplat version $revision: $");
  399. }
  400. internal override void Exit() {
  401. Win32PostQuitMessage(0);
  402. }
  403. internal override void EnableThemes() {
  404. themes_enabled=true;
  405. }
  406. internal override IntPtr CreateWindow(CreateParams cp) {
  407. IntPtr WindowHandle;
  408. IntPtr ParentHandle;
  409. ParentHandle=cp.Parent;
  410. if ((ParentHandle==IntPtr.Zero) && (cp.Style & (int)WindowStyles.WS_CHILD)!=0) {
  411. // We need to use our foster parent window until this poor child gets it's parent assigned
  412. ParentHandle=FosterParent;
  413. }
  414. WindowHandle = Win32CreateWindow((uint)cp.ExStyle, cp.ClassName, cp.Caption, (uint)cp.Style, cp.X, cp.Y, cp.Width, cp.Height, ParentHandle, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
  415. if (WindowHandle==IntPtr.Zero) {
  416. uint error = Win32GetLastError();
  417. Win32MessageBox(IntPtr.Zero, "Error : " + error.ToString(), "Failed to create window", 0);
  418. }
  419. return WindowHandle;
  420. }
  421. internal override IntPtr CreateWindow(IntPtr Parent, int X, int Y, int Width, int Height) {
  422. CreateParams create_params = new CreateParams();
  423. create_params.Caption = "";
  424. create_params.X = X;
  425. create_params.Y = Y;
  426. create_params.Width = Width;
  427. create_params.Height = Height;
  428. create_params.ClassName=XplatUI.DefaultClassName;
  429. create_params.ClassStyle = 0;
  430. create_params.ExStyle=0;
  431. create_params.Parent=IntPtr.Zero;
  432. create_params.Param=0;
  433. return CreateWindow(create_params);
  434. }
  435. internal override void DestroyWindow(IntPtr handle) {
  436. Console.WriteLine("#region #line");
  437. return;
  438. }
  439. internal override void RefreshWindow(IntPtr handle) {
  440. Win32InvalidateRect(handle, IntPtr.Zero, true);
  441. Win32UpdateWindow(handle);
  442. }
  443. [MonoTODO("Add support for internal table of windows/DCs for cleanup")]
  444. internal override PaintEventArgs PaintEventStart(IntPtr handle) {
  445. IntPtr hdc;
  446. PAINTSTRUCT ps;
  447. PaintEventArgs paint_event;
  448. RECT rect;
  449. Rectangle clip_rect;
  450. clip_rect = new Rectangle();
  451. rect = new RECT();
  452. ps = new PAINTSTRUCT();
  453. if (Win32GetUpdateRect(handle, ref rect, false)) {
  454. hdc = Win32BeginPaint(handle, ref ps);
  455. // FIXME: Add the DC to internal list
  456. // FIXME: Figure out why the rectangle is always 0 size
  457. clip_rect = new Rectangle(ps.rcPaint.left, ps.rcPaint.top, ps.rcPaint.right-ps.rcPaint.left, ps.rcPaint.bottom-ps.rcPaint.top);
  458. // clip_rect = new Rectangle(rect.top, rect.left, rect.right-rect.left, rect.bottom-rect.top);
  459. } else {
  460. hdc = Win32GetDC(handle);
  461. // FIXME: Add the DC to internal list
  462. clip_rect = new Rectangle(rect.top, rect.left, rect.right-rect.left, rect.bottom-rect.top);
  463. }
  464. paint_event = new PaintEventArgs(Graphics.FromHdc(hdc), clip_rect);
  465. return paint_event;
  466. }
  467. internal override void PaintEventEnd(IntPtr handle) {
  468. // FIXME: Lookup in the internal list how to clean
  469. ;
  470. // paintEventArgs.Dispose();
  471. }
  472. internal override void SetWindowPos(IntPtr handle, int x, int y, int width, int height) {
  473. Win32MoveWindow(handle, x, y, width, height);
  474. return;
  475. }
  476. internal override void GetWindowPos(IntPtr handle, out int x, out int y, out int width, out int height) {
  477. RECT rect;
  478. Win32GetWindowRect(handle, out rect);
  479. Win32ScreenToClient(Win32GetParent(handle), ref rect);
  480. x = rect.left;
  481. y = rect.top;
  482. Win32GetClientRect(handle, out rect);
  483. width = rect.right - rect.left;
  484. height = rect.bottom - rect.top;
  485. return;
  486. }
  487. internal override void Activate(IntPtr handle) {
  488. Win32SetActiveWindow(handle);
  489. }
  490. internal override void Invalidate(IntPtr handle, Rectangle rc, bool clear) {
  491. RECT rect;
  492. rect.left=rc.Left;
  493. rect.top=rc.Top;
  494. rect.right=rc.Right;
  495. rect.bottom=rc.Bottom;
  496. Win32InvalidateRect(handle, ref rect, clear);
  497. }
  498. internal override IntPtr DefWndProc(ref Message msg) {
  499. msg.Result=Win32DefWindowProc(msg.HWnd, (Msg)msg.Msg, msg.WParam, msg.LParam);
  500. return msg.Result;
  501. }
  502. internal override void HandleException(Exception e) {
  503. StackTrace st = new StackTrace(e);
  504. Win32MessageBox(IntPtr.Zero, e.Message+st.ToString(), "Exception", 0);
  505. Console.WriteLine("{0}{1}", e.Message, st.ToString());
  506. }
  507. internal override void DoEvents() {
  508. MSG msg = new MSG();
  509. while (Win32PeekMessage(ref msg, IntPtr.Zero, 0, 0, (uint)PeekMessageFlags.PM_REMOVE)!=true) {
  510. if (msg.message==Msg.WM_PAINT) {
  511. XplatUI.TranslateMessage(ref msg);
  512. XplatUI.DispatchMessage(ref msg);
  513. }
  514. }
  515. }
  516. internal override bool PeekMessage(ref MSG msg, IntPtr hWnd, int wFilterMin, int wFilterMax, uint flags) {
  517. return Win32PeekMessage(ref msg, hWnd, wFilterMin, wFilterMax, flags);
  518. }
  519. internal override bool GetMessage(ref MSG msg, IntPtr hWnd, int wFilterMin, int wFilterMax) {
  520. return Win32GetMessage(ref msg, hWnd, wFilterMin, wFilterMax);
  521. }
  522. internal override bool TranslateMessage(ref MSG msg) {
  523. return Win32TranslateMessage(ref msg);
  524. }
  525. internal override bool DispatchMessage(ref MSG msg) {
  526. return Win32DispatchMessage(ref msg);
  527. }
  528. internal override bool Text(IntPtr handle, string text) {
  529. Win32SetWindowText(handle, text);
  530. return true;
  531. }
  532. internal override bool SetVisible(IntPtr handle, bool visible) {
  533. Console.WriteLine("Setting window visibility: {0}", visible);
  534. return true;
  535. }
  536. internal override bool IsVisible(IntPtr handle) {
  537. Console.WriteLine("Getting window visibility");
  538. return true;
  539. }
  540. internal override IntPtr SetParent(IntPtr handle, IntPtr parent) {
  541. return Win32SetParent(handle, parent);
  542. }
  543. internal override IntPtr GetParent(IntPtr handle) {
  544. return Win32GetParent(handle);
  545. }
  546. internal override void GrabWindow(IntPtr hWnd) {
  547. Win32SetCapture(hWnd);
  548. }
  549. internal override void ReleaseWindow(IntPtr hWnd) {
  550. Win32ReleaseCapture();
  551. }
  552. internal override bool CalculateWindowRect(IntPtr hWnd, ref Rectangle ClientRect, int Style, bool HasMenu, out Rectangle WindowRect) {
  553. RECT rect;
  554. rect.left=ClientRect.Left;
  555. rect.top=ClientRect.Top;
  556. rect.right=ClientRect.Right;
  557. rect.bottom=ClientRect.Bottom;
  558. if (!Win32AdjustWindowRectEx(ref rect, Style, HasMenu, 0)) {
  559. WindowRect = new Rectangle(ClientRect.Left, ClientRect.Top, ClientRect.Width, ClientRect.Height);
  560. return false;
  561. }
  562. WindowRect = new Rectangle(rect.left, rect.top, rect.right-rect.left, rect.bottom-rect.top);
  563. return true;
  564. }
  565. // Santa's little helper
  566. static void Where() {
  567. Console.WriteLine("Here: {0}", new StackTrace().ToString());
  568. }
  569. #endregion // Public Static Methods
  570. #region Win32 Imports
  571. [DllImport ("kernel32.dll", EntryPoint="GetLastError", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
  572. private extern static uint Win32GetLastError();
  573. [DllImport ("user32.dll", EntryPoint="CreateWindowExA", CallingConvention=CallingConvention.StdCall)]
  574. internal extern static IntPtr Win32CreateWindow(uint dwExStyle, string lpClassName, string lpWindowName, uint dwStyle, int x, int y, int nWidth, int nHeight, IntPtr hWndParent, IntPtr hMenu, IntPtr hInstance, IntPtr lParam);
  575. [DllImport ("user32.dll", EntryPoint="PeekMessageA", CallingConvention=CallingConvention.StdCall)]
  576. internal extern static bool Win32PeekMessage(ref MSG msg, IntPtr hWnd, int wFilterMin, int wFilterMax, uint flags);
  577. [DllImport ("user32.dll", EntryPoint="GetMessageA", CallingConvention=CallingConvention.StdCall)]
  578. internal extern static bool Win32GetMessage(ref MSG msg, IntPtr hWnd, int wFilterMin, int wFilterMax);
  579. [DllImport ("user32.dll", EntryPoint="TranslateMessage", CallingConvention=CallingConvention.StdCall)]
  580. internal extern static bool Win32TranslateMessage(ref MSG msg);
  581. [DllImport ("user32.dll", EntryPoint="DispatchMessageA", CallingConvention=CallingConvention.StdCall)]
  582. internal extern static bool Win32DispatchMessage(ref MSG msg);
  583. [DllImport ("user32.dll", EntryPoint="MoveWindow", CallingConvention=CallingConvention.StdCall)]
  584. internal extern static bool Win32MoveWindow(IntPtr hWnd, int x, int y, int width, int height);
  585. [DllImport ("user32.dll", EntryPoint="SetWindowTextA", CallingConvention=CallingConvention.StdCall)]
  586. internal extern static bool Win32SetWindowText(IntPtr hWnd, string lpString);
  587. [DllImport ("user32.dll", EntryPoint="SetParent", CallingConvention=CallingConvention.StdCall)]
  588. internal extern static IntPtr Win32SetParent(IntPtr hWnd, IntPtr hParent);
  589. [DllImport ("user32.dll", EntryPoint="RegisterClassA", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
  590. private extern static bool Win32RegisterClass(ref WNDCLASS wndClass);
  591. [DllImport ("user32.dll", EntryPoint="LoadCursorA", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
  592. private extern static IntPtr Win32LoadCursor(IntPtr hInstance, LoadCursorType type);
  593. [DllImport ("user32.dll", EntryPoint="DefWindowProcA", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
  594. private extern static IntPtr Win32DefWindowProc(IntPtr hWnd, Msg Msg, IntPtr wParam, IntPtr lParam);
  595. [DllImport ("user32.dll", EntryPoint="PostQuitMessage", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
  596. private extern static IntPtr Win32PostQuitMessage(int nExitCode);
  597. [DllImport ("user32.dll", EntryPoint="UpdateWindow", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
  598. private extern static IntPtr Win32UpdateWindow(IntPtr hWnd);
  599. [DllImport ("user32.dll", EntryPoint="GetUpdateRect", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
  600. private extern static bool Win32GetUpdateRect(IntPtr hWnd, ref RECT rect, bool erase);
  601. [DllImport ("user32.dll", EntryPoint="BeginPaint", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
  602. private extern static IntPtr Win32BeginPaint(IntPtr hWnd, ref PAINTSTRUCT ps);
  603. [DllImport ("user32.dll", EntryPoint="EndPaint", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
  604. private extern static bool Win32EndPaint(IntPtr hWnd, ref PAINTSTRUCT ps);
  605. [DllImport ("user32.dll", EntryPoint="GetDC", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
  606. private extern static IntPtr Win32GetDC(IntPtr hWnd);
  607. [DllImport ("user32.dll", EntryPoint="ReleaseDC", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
  608. private extern static IntPtr Win32ReleaseDC(IntPtr hWnd, IntPtr hDC);
  609. [DllImport ("user32.dll", EntryPoint="MessageBoxA", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
  610. private extern static IntPtr Win32MessageBox(IntPtr hParent, string pText, string pCaption, uint uType);
  611. [DllImport ("user32.dll", EntryPoint="InvalidateRect", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
  612. private extern static IntPtr Win32InvalidateRect(IntPtr hWnd, ref RECT lpRect, bool bErase);
  613. [DllImport ("user32.dll", EntryPoint="InvalidateRect", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
  614. private extern static IntPtr Win32InvalidateRect(IntPtr hWnd, IntPtr lpRect, bool bErase);
  615. [DllImport ("user32.dll", EntryPoint="SetCapture", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
  616. private extern static IntPtr Win32SetCapture(IntPtr hWnd);
  617. [DllImport ("user32.dll", EntryPoint="ReleaseCapture", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
  618. private extern static IntPtr Win32ReleaseCapture();
  619. [DllImport ("user32.dll", EntryPoint="GetWindowRect", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
  620. private extern static IntPtr Win32GetWindowRect(IntPtr hWnd, out RECT rect);
  621. [DllImport ("user32.dll", EntryPoint="GetClientRect", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
  622. private extern static IntPtr Win32GetClientRect(IntPtr hWnd, out RECT rect);
  623. [DllImport ("user32.dll", EntryPoint="ScreenToClient", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
  624. private extern static IntPtr Win32ScreenToClient(IntPtr hWnd, ref RECT rect);
  625. [DllImport ("user32.dll", EntryPoint="GetParent", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
  626. private extern static IntPtr Win32GetParent(IntPtr hWnd);
  627. [DllImport ("user32.dll", EntryPoint="SetActiveWindow", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
  628. private extern static IntPtr Win32SetActiveWindow(IntPtr hWnd);
  629. [DllImport ("user32.dll", EntryPoint="PostQuitMessage", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
  630. private extern static IntPtr Win32PostQuitMessage(IntPtr hWnd);
  631. [DllImport ("user32.dll", EntryPoint="AdjustWindowRectEx", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
  632. private extern static bool Win32AdjustWindowRectEx(ref RECT lpRect, int dwStyle, bool bMenu, int dwExStyle);
  633. #endregion
  634. }
  635. }