DragForm.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*******************************************************************************
  2. * Copyright(c) 2014 DongkeSoft All rights reserved. / Confidential
  3. * 类的信息:
  4. * 1.程序名称:DragForm.cs
  5. * 2.功能描述:类文件
  6. * 编辑履历:
  7. * 作者 日期 版本 修改内容
  8. * 陈晓野 2014/09/01 1.00 新建
  9. *******************************************************************************/
  10. using System;
  11. using System.Windows.Forms;
  12. namespace Dongke.IBOSS.PRD.Basics.DockPanel
  13. {
  14. // Inspired by Chris Sano's article:
  15. // http://msdn.microsoft.com/smartclient/default.aspx?pull=/library/en-us/dnwinforms/html/colorpicker.asp
  16. // In Sano's article, the DragForm needs to meet the following criteria:
  17. // (1) it was not to show up in the task bar;
  18. // ShowInTaskBar = false
  19. // (2) it needed to be the top-most window;
  20. // TopMost = true (not necessary here)
  21. // (3) its icon could not show up in the ALT+TAB window if the user pressed ALT+TAB during a drag-and-drop;
  22. // FormBorderStyle = FormBorderStyle.None;
  23. // Create with WS_EX_TOOLWINDOW window style.
  24. // Compares with the solution in the artile by setting FormBorderStyle as FixedToolWindow,
  25. // and then clip the window caption and border, this way is much simplier.
  26. // (4) it was not to steal focus from the application when displayed.
  27. // User Win32 ShowWindow API with SW_SHOWNOACTIVATE
  28. // In addition, this form should only for display and therefore should act as transparent, otherwise
  29. // WindowFromPoint will return this form, instead of the control beneath. Need BOTH of the following to
  30. // achieve this (don't know why, spent hours to try it out :( ):
  31. // 1. Enabled = false;
  32. // 2. WM_NCHITTEST returns HTTRANSPARENT
  33. internal class DragForm : Form
  34. {
  35. public DragForm()
  36. {
  37. FormBorderStyle = FormBorderStyle.None;
  38. ShowInTaskbar = false;
  39. SetStyle(ControlStyles.Selectable, false);
  40. Enabled = false;
  41. }
  42. protected override CreateParams CreateParams
  43. {
  44. get
  45. {
  46. CreateParams createParams = base.CreateParams;
  47. createParams.ExStyle |= (int)(Win32.WindowExStyles.WS_EX_NOACTIVATE | Win32.WindowExStyles.WS_EX_TOOLWINDOW);
  48. return createParams;
  49. }
  50. }
  51. protected override void WndProc(ref Message m)
  52. {
  53. if (m.Msg == (int)Win32.Msgs.WM_NCHITTEST)
  54. {
  55. m.Result = (IntPtr)Win32.HitTest.HTTRANSPARENT;
  56. return;
  57. }
  58. base.WndProc(ref m);
  59. }
  60. //The form can be still activated by explicity calling Activate
  61. protected override bool ShowWithoutActivation
  62. {
  63. get
  64. {
  65. return true;
  66. }
  67. }
  68. public virtual void Show(bool bActivate)
  69. {
  70. Show();
  71. if (bActivate)
  72. Activate();
  73. }
  74. }
  75. }