WindowControlService.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using System;
  2. using System.Windows.Forms;
  3. namespace Cbdx.Tests.Services
  4. {
  5. internal class WindowControlService : IWindowControlService
  6. {
  7. private readonly TestForm _form;
  8. public WindowControlService(TestForm form)
  9. {
  10. _form = form;
  11. }
  12. WindowState IWindowControlService.GetWindowState()
  13. {
  14. return (WindowState)_form.WindowState;
  15. }
  16. void IWindowControlService.SetWindowState(WindowState windowState)
  17. {
  18. if (_form.InvokeRequired) _form.Invoke(new Action(() => _form.WindowState = (FormWindowState)windowState));
  19. else _form.WindowState = (FormWindowState)windowState;
  20. }
  21. void IWindowControlService.SetWindowTitle(string title)
  22. {
  23. if (_form.InvokeRequired) _form.Invoke(new Action(() => _form.Text = title));
  24. else _form.Text = title;
  25. }
  26. void IWindowControlService.CloseWindow()
  27. {
  28. if (_form.InvokeRequired) _form.Invoke(new Action(() => _form.Close()));
  29. else _form.Close();
  30. }
  31. void IWindowControlService.Reload()
  32. {
  33. if (_form.InvokeRequired) _form.Invoke(new Action(() => _form.CreateOrReloadCefBrowserControl()));
  34. else _form.CreateOrReloadCefBrowserControl();
  35. }
  36. void IWindowControlService.DragMove()
  37. {
  38. if (_form.InvokeRequired)
  39. {
  40. _form.Invoke(new Action(() =>
  41. {
  42. ReleaseCapture();
  43. SendMessage(_form.Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
  44. }));
  45. }
  46. else
  47. {
  48. ReleaseCapture();
  49. SendMessage(_form.Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
  50. }
  51. }
  52. public const int WM_NCLBUTTONDOWN = 0xA1;
  53. public const int HT_CAPTION = 0x2;
  54. [System.Runtime.InteropServices.DllImportAttribute("user32.dll")]
  55. public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
  56. [System.Runtime.InteropServices.DllImportAttribute("user32.dll")]
  57. public static extern bool ReleaseCapture();
  58. }
  59. }