WindowControlService.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. using System;
  2. using System.Windows.Forms;
  3. namespace Cbdx.Tests.Services
  4. {
  5. internal class WindowControlService : IWindowControlService
  6. {
  7. private readonly Form _form;
  8. public WindowControlService(Form 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.DragMove()
  32. {
  33. if (_form.InvokeRequired)
  34. {
  35. _form.Invoke(new Action(() =>
  36. {
  37. ReleaseCapture();
  38. SendMessage(_form.Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
  39. }));
  40. }
  41. else
  42. {
  43. ReleaseCapture();
  44. SendMessage(_form.Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
  45. }
  46. }
  47. public const int WM_NCLBUTTONDOWN = 0xA1;
  48. public const int HT_CAPTION = 0x2;
  49. [System.Runtime.InteropServices.DllImportAttribute("user32.dll")]
  50. public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
  51. [System.Runtime.InteropServices.DllImportAttribute("user32.dll")]
  52. public static extern bool ReleaseCapture();
  53. }
  54. }