FloatingToolForm.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. using PaintDotNet.SystemLayer;
  2. using System;
  3. using System.Drawing;
  4. using System.Windows.Forms;
  5. namespace PaintDotNet
  6. {
  7. internal delegate bool CmdKeysEventHandler(object sender, ref Message msg, Keys keyData);
  8. internal class FloatingToolForm : PdnBaseForm, ISnapObstacleHost
  9. {
  10. private System.ComponentModel.IContainer components = null;
  11. private ControlEventHandler controlAddedDelegate;
  12. private ControlEventHandler controlRemovedDelegate;
  13. private KeyEventHandler keyUpDelegate;
  14. private SnapObstacleController snapObstacle;
  15. public SnapObstacle SnapObstacle
  16. {
  17. get
  18. {
  19. if (this.snapObstacle == null)
  20. {
  21. int distancePadding = UI.GetExtendedFrameBounds(this);
  22. int distance = SnapObstacle.DefaultSnapDistance + distancePadding;
  23. this.snapObstacle = new SnapObstacleController(this.Name, this.Bounds, SnapRegion.Exterior, false, SnapObstacle.DefaultSnapProximity, distance);
  24. this.snapObstacle.BoundsChangeRequested += SnapObstacle_BoundsChangeRequested;
  25. }
  26. return this.snapObstacle;
  27. }
  28. }
  29. private void SnapObstacle_BoundsChangeRequested(object sender, HandledEventArgs<Rectangle> e)
  30. {
  31. this.Bounds = e.Data;
  32. }
  33. /// <summary>
  34. /// Occurs when it is appropriate for the parent to steal focus.
  35. /// </summary>
  36. public event EventHandler RelinquishFocus;
  37. protected virtual void OnRelinquishFocus()
  38. {
  39. // Only relinquish focus if we have it in the first place
  40. if (MenuStripEx.IsAnyMenuActive)
  41. {
  42. return;
  43. }
  44. if (RelinquishFocus != null)
  45. {
  46. RelinquishFocus(this, EventArgs.Empty);
  47. }
  48. }
  49. public FloatingToolForm()
  50. {
  51. this.KeyPreview = true;
  52. controlAddedDelegate = new ControlEventHandler(ControlAddedHandler);
  53. controlRemovedDelegate = new ControlEventHandler(ControlRemovedHandler);
  54. keyUpDelegate = new KeyEventHandler(KeyUpHandler);
  55. this.ControlAdded += controlAddedDelegate; // we don't override OnControlAdded so we can re-use the method (see code below for ControlAdded)
  56. this.ControlRemoved += controlRemovedDelegate;
  57. //
  58. // Required for Windows Form Designer support
  59. //
  60. InitializeComponent();
  61. try
  62. {
  63. UserSessions.SessionChanged += new EventHandler(UserSessions_SessionChanged);
  64. Microsoft.Win32.SystemEvents.DisplaySettingsChanged += new EventHandler(SystemEvents_DisplaySettingsChanged);
  65. }
  66. catch (Exception ex)
  67. {
  68. Tracing.Ping("Exception while signing up for some system events: " + ex.ToString());
  69. }
  70. }
  71. private void SystemEvents_DisplaySettingsChanged(object sender, EventArgs e)
  72. {
  73. if (Visible && IsShown)
  74. {
  75. EnsureFormIsOnScreen();
  76. }
  77. }
  78. private void UserSessions_SessionChanged(object sender, EventArgs e)
  79. {
  80. if (Visible && IsShown)
  81. {
  82. EnsureFormIsOnScreen();
  83. }
  84. }
  85. protected override void OnClick(EventArgs e)
  86. {
  87. OnRelinquishFocus();
  88. base.OnClick(e);
  89. }
  90. public event CmdKeysEventHandler ProcessCmdKeyEvent;
  91. protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
  92. {
  93. bool result = false;
  94. if (Utility.IsArrowKey(keyData))
  95. {
  96. KeyEventArgs kea = new KeyEventArgs(keyData);
  97. switch (msg.Msg)
  98. {
  99. case 0x100: // WM_KEYDOWN:
  100. this.OnKeyDown(kea);
  101. return kea.Handled;
  102. /*
  103. case NativeMethods.WmConstants.WM_KEYUP:
  104. this.OnKeyUp(kea);
  105. return kea.Handled;
  106. */
  107. }
  108. }
  109. else
  110. {
  111. if (ProcessCmdKeyEvent != null)
  112. {
  113. result = ProcessCmdKeyEvent(this, ref msg, keyData);
  114. }
  115. }
  116. if (!result)
  117. {
  118. result = base.ProcessCmdKey(ref msg, keyData);
  119. }
  120. return result;
  121. }
  122. /// <summary>
  123. /// Clean up any resources being used.
  124. /// </summary>
  125. protected override void Dispose(bool disposing)
  126. {
  127. if (disposing)
  128. {
  129. if (components != null)
  130. {
  131. components.Dispose();
  132. components = null;
  133. }
  134. try
  135. {
  136. UserSessions.SessionChanged -= new EventHandler(UserSessions_SessionChanged);
  137. Microsoft.Win32.SystemEvents.DisplaySettingsChanged -= new EventHandler(SystemEvents_DisplaySettingsChanged);
  138. }
  139. catch (Exception)
  140. {
  141. // Ignore any errors
  142. }
  143. }
  144. base.Dispose(disposing);
  145. }
  146. #region Windows Form Designer generated code
  147. /// <summary>
  148. /// Required method for Designer support - do not modify
  149. /// the contents of this method with the code editor.
  150. /// </summary>
  151. private void InitializeComponent()
  152. {
  153. //
  154. // FloatingToolForm
  155. //
  156. this.AutoScaleDimensions = new SizeF(96F, 96F);
  157. this.AutoScaleMode = AutoScaleMode.Dpi;
  158. this.ClientSize = new Size(292, 271);
  159. this.FormBorderStyle = FormBorderStyle.SizableToolWindow;
  160. this.MaximizeBox = false;
  161. this.MinimizeBox = false;
  162. this.Name = "FloatingToolForm";
  163. this.ShowInTaskbar = false;
  164. this.SizeGripStyle = SizeGripStyle.Hide;
  165. this.ForceActiveTitleBar = true;
  166. }
  167. #endregion
  168. private void ControlAddedHandler(object sender, ControlEventArgs e)
  169. {
  170. e.Control.ControlAdded += controlAddedDelegate;
  171. e.Control.ControlRemoved += controlRemovedDelegate;
  172. e.Control.KeyUp += keyUpDelegate;
  173. }
  174. private void ControlRemovedHandler(object sender, ControlEventArgs e)
  175. {
  176. e.Control.ControlAdded -= controlAddedDelegate;
  177. e.Control.ControlRemoved -= controlRemovedDelegate;
  178. e.Control.KeyUp -= keyUpDelegate;
  179. }
  180. private void KeyUpHandler(object sender, KeyEventArgs e)
  181. {
  182. if (!e.Handled)
  183. {
  184. this.OnKeyUp(e);
  185. }
  186. }
  187. private void UpdateSnapObstacleBounds()
  188. {
  189. if (this.snapObstacle != null)
  190. {
  191. this.snapObstacle.SetBounds(this.Bounds);
  192. }
  193. }
  194. private void UpdateParking()
  195. {
  196. if (this.FormBorderStyle == FormBorderStyle.Fixed3D ||
  197. this.FormBorderStyle == FormBorderStyle.FixedDialog ||
  198. this.FormBorderStyle == FormBorderStyle.FixedSingle ||
  199. this.FormBorderStyle == FormBorderStyle.FixedToolWindow)
  200. {
  201. ISnapManagerHost ismh = this.Owner as ISnapManagerHost;
  202. if (ismh != null)
  203. {
  204. SnapManager mySM = ismh.SnapManager;
  205. mySM.ReparkObstacle(this);
  206. }
  207. }
  208. }
  209. protected override void OnVisibleChanged(EventArgs e)
  210. {
  211. if (Visible)
  212. {
  213. EnsureFormIsOnScreen();
  214. }
  215. base.OnVisibleChanged(e);
  216. }
  217. protected override void OnResizeBegin(EventArgs e)
  218. {
  219. UpdateSnapObstacleBounds();
  220. UpdateParking();
  221. base.OnResizeBegin(e);
  222. }
  223. protected override void OnResize(EventArgs e)
  224. {
  225. UpdateSnapObstacleBounds();
  226. base.OnResize(e);
  227. UpdateParking();
  228. }
  229. protected override void OnResizeEnd(EventArgs e)
  230. {
  231. this.moving = false;
  232. UpdateSnapObstacleBounds();
  233. UpdateParking();
  234. base.OnResizeEnd(e);
  235. OnRelinquishFocus();
  236. }
  237. protected override void OnSizeChanged(EventArgs e)
  238. {
  239. UpdateSnapObstacleBounds();
  240. UpdateParking();
  241. base.OnSizeChanged(e);
  242. }
  243. private Size movingCursorDelta = Size.Empty; // dx,dy from mousex,y to bounds.Location
  244. private bool moving = false;
  245. protected override void OnMoving(MovingEventArgs mea)
  246. {
  247. ISnapManagerHost snapHost = this.Owner as ISnapManagerHost;
  248. if (snapHost != null)
  249. {
  250. SnapManager sm = snapHost.SnapManager;
  251. // Make sure the window titlebar always follows a constant distance from the mouse cursor
  252. // Otherwise the window may "slip" as it snaps and unsnaps
  253. if (!this.moving)
  254. {
  255. this.movingCursorDelta = new Size(
  256. Cursor.Position.X - mea.Rectangle.X,
  257. Cursor.Position.Y - mea.Rectangle.Y);
  258. this.moving = true;
  259. }
  260. mea.Rectangle = new Rectangle(
  261. Cursor.Position.X - this.movingCursorDelta.Width,
  262. Cursor.Position.Y - this.movingCursorDelta.Height,
  263. mea.Rectangle.Width,
  264. mea.Rectangle.Height);
  265. this.snapObstacle.SetBounds(mea.Rectangle);
  266. Point pt = mea.Rectangle.Location;
  267. Point newPt = sm.AdjustObstacleDestination(this.SnapObstacle, pt);
  268. Rectangle newRect = new Rectangle(newPt, mea.Rectangle.Size);
  269. this.snapObstacle.SetBounds(newRect);
  270. mea.Rectangle = newRect;
  271. }
  272. base.OnMoving(mea);
  273. }
  274. protected override void OnMove(EventArgs e)
  275. {
  276. UpdateSnapObstacleBounds();
  277. base.OnMove(e);
  278. }
  279. protected override void OnEnabledChanged(EventArgs e)
  280. {
  281. if (this.snapObstacle != null)
  282. {
  283. this.snapObstacle.Enabled = this.Enabled;
  284. }
  285. base.OnEnabledChanged(e);
  286. }
  287. protected override void OnLoad(EventArgs e)
  288. {
  289. ISnapManagerHost smh = this.Owner as ISnapManagerHost;
  290. if (smh != null)
  291. {
  292. smh.SnapManager.AddSnapObstacle(this);
  293. }
  294. base.OnLoad(e);
  295. }
  296. }
  297. }