FloatingToolForm.cs 11 KB

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