1. using System;
    2. using System.Collections.Generic;
    3. using System.Drawing;
    4. using System.Linq;
    5. using System.Reflection;
    6. using System.Runtime.InteropServices;
    7. using System.Text;
    8. using System.Text.RegularExpressions;
    9. using System.Windows.Forms;
    10. using ZwSoft.ZwCAD.Runtime;
    11. namespace test
    12. {
    13. public static partial class HookLib
    14. {
    15. private static bool isNormal = true;
    16. private static Size size;
    17. private static Form Frm = null;
    18. private static WindowHookProc callBackFunc = null;
    19. // For AutoCAD
    20. // On previous versions, import from acad.exe (instead accore.dll)
    21. //[DllImport("accore.dll",
    22. // CharSet = CharSet.Unicode,
    23. // CallingConvention = CallingConvention.Cdecl,
    24. // EntryPoint = "?acedRegisterFilterWinMsg@@YA_NQ6A_NPEAUtagMSG@@@Z@Z")]
    25. //private static extern int acedRegisterFilterWinMsg(WindowHookProc callBackFunc);
    26. // For AutoCAD
    27. // On previous versions, import from acad.exe (instead accore.dll)
    28. //[DllImport("accore.dll",
    29. // CharSet = CharSet.Unicode,
    30. // CallingConvention = CallingConvention.Cdecl,
    31. // EntryPoint = "?acedRemoveFilterWinMsg@@YA_NQ6A_NPEAUtagMSG@@@Z@Z")]
    32. //private static extern int acedRemoveFilterWinMsg(WindowHookProc callBackFunc);
    33. // For ZWCAD 2014
    34. // On previous versions, import from ZWCAD.exe
    35. [DllImport("ZWCAD.exe",
    36. CharSet = CharSet.Unicode,
    37. CallingConvention = CallingConvention.Cdecl,
    38. EntryPoint = "?zcedRegisterFilterWinMsg@@YAHQ6AHPAUtagMSG@@@Z@Z")]
    39. private static extern int acedRegisterFilterWinMsg(WindowHookProc callBackFunc);
    40. // For ZWCAD 2014
    41. // On previous versions, import from ZWCAD.exe
    42. [DllImport("ZWCAD.exe",
    43. CharSet = CharSet.Unicode,
    44. CallingConvention = CallingConvention.Cdecl,
    45. EntryPoint = "?zcedRemoveFilterWinMsg@@YAHQ6AHPAUtagMSG@@@Z@Z")]
    46. private static extern int acedRemoveFilterWinMsg(WindowHookProc callBackFunc);
    47. // hook message filter callback function
    48. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
    49. public delegate int WindowHookProc(ref Message msg);
    50. private static int WindowsHook(ref Message msg)
    51. {
    52. // check the msg struct for whatever we want,
    53. // like keys, paint messages etc
    54. if (msg.Msg == 0x200)
    55. {
    56. if (Frm != null && !Frm.IsDisposed)
    57. { // do something
    58. Frm.Text = "移动" + Cursor.Position.X.ToString() + "," + Cursor.Position.Y.ToString();
    59. if (Cursor.Position.X < Frm.Location.X || Cursor.Position.X > Frm.Location.X + Frm.Size.Width || Cursor.Position.Y < Frm.Location.Y || Cursor.Position.Y > Frm.Location.Y + Frm.Size.Height)
    60. {
    61. if (isNormal)
    62. {
    63. Frm.ClientSize = new Size(size.Width, 20);
    64. isNormal = false;
    65. }
    66. }
    67. else if (!isNormal)
    68. {
    69. Frm.ClientSize = size;
    70. isNormal = true;
    71. }
    72. }
    73. }
    74. return 0;
    75. }
    76. //[CommandMethod("RegisterHook")]
    77. public static void CmdRegisterHook()
    78. {
    79. if (callBackFunc == null)
    80. {
    81. callBackFunc = new WindowHookProc(WindowsHook);
    82. acedRegisterFilterWinMsg(callBackFunc);
    83. }
    84. }
    85. //[CommandMethod("RmoveHook")]
    86. public static void CmdRmoveHook()
    87. {
    88. if (callBackFunc != null)
    89. {
    90. acedRemoveFilterWinMsg(callBackFunc);
    91. callBackFunc = null;
    92. }
    93. }
    94. public static void ShowForm(string str)
    95. {
    96. if (Frm == null || Frm.IsDisposed)
    97. {
    98. Assembly assembly = Assembly.GetExecutingAssembly();
    99. Frm = assembly.CreateInstance($"{MethodBase.GetCurrentMethod().DeclaringType.Namespace}.{str}") as Form;
    100. size = new Size(400, 400);
    101. ZwSoft.ZwCAD.ApplicationServices.Application.ShowModelessDialog(Frm);
    102. CmdRegisterHook();
    103. }
    104. else Frm.Show();
    105. }
    106. //public static void ShowForm(string str)
    107. //{
    108. // if (Frm == null || Frm.IsDisposed)
    109. // {
    110. // Assembly tempAssembly = Assembly.GetExecutingAssembly();
    111. // Type t = tempAssembly.GetType(str);
    112. // object[] args = null;
    113. // Frm = Activator.CreateInstance(t, args) as Form;
    114. // //size = new Size(400, 400);
    115. // size = Frm.Size;
    116. // ZwSoft.ZwCAD.ApplicationServices.Application.ShowModelessDialog(Frm);
    117. // CmdRegisterHook();
    118. // }
    119. // else Frm.Show();
    120. //}
    121. }
    122. }