using System;using System.Collections.Generic;using System.Drawing;using System.Linq;using System.Reflection;using System.Runtime.InteropServices;using System.Text;using System.Text.RegularExpressions;using System.Windows.Forms;using ZwSoft.ZwCAD.Runtime;namespace test{ public static partial class HookLib { private static bool isNormal = true; private static Size size; private static Form Frm = null; private static WindowHookProc callBackFunc = null; // For AutoCAD // On previous versions, import from acad.exe (instead accore.dll) //[DllImport("accore.dll", // CharSet = CharSet.Unicode, // CallingConvention = CallingConvention.Cdecl, // EntryPoint = "?acedRegisterFilterWinMsg@@YA_NQ6A_NPEAUtagMSG@@@Z@Z")] //private static extern int acedRegisterFilterWinMsg(WindowHookProc callBackFunc); // For AutoCAD // On previous versions, import from acad.exe (instead accore.dll) //[DllImport("accore.dll", // CharSet = CharSet.Unicode, // CallingConvention = CallingConvention.Cdecl, // EntryPoint = "?acedRemoveFilterWinMsg@@YA_NQ6A_NPEAUtagMSG@@@Z@Z")] //private static extern int acedRemoveFilterWinMsg(WindowHookProc callBackFunc); // For ZWCAD 2014 // On previous versions, import from ZWCAD.exe [DllImport("ZWCAD.exe", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl, EntryPoint = "?zcedRegisterFilterWinMsg@@YAHQ6AHPAUtagMSG@@@Z@Z")] private static extern int acedRegisterFilterWinMsg(WindowHookProc callBackFunc); // For ZWCAD 2014 // On previous versions, import from ZWCAD.exe [DllImport("ZWCAD.exe", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl, EntryPoint = "?zcedRemoveFilterWinMsg@@YAHQ6AHPAUtagMSG@@@Z@Z")] private static extern int acedRemoveFilterWinMsg(WindowHookProc callBackFunc); // hook message filter callback function [UnmanagedFunctionPointer(CallingConvention.Cdecl)] public delegate int WindowHookProc(ref Message msg); private static int WindowsHook(ref Message msg) { // check the msg struct for whatever we want, // like keys, paint messages etc if (msg.Msg == 0x200) { if (Frm != null && !Frm.IsDisposed) { // do something Frm.Text = "移动" + Cursor.Position.X.ToString() + "," + Cursor.Position.Y.ToString(); 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) { if (isNormal) { Frm.ClientSize = new Size(size.Width, 20); isNormal = false; } } else if (!isNormal) { Frm.ClientSize = size; isNormal = true; } } } return 0; } //[CommandMethod("RegisterHook")] public static void CmdRegisterHook() { if (callBackFunc == null) { callBackFunc = new WindowHookProc(WindowsHook); acedRegisterFilterWinMsg(callBackFunc); } } //[CommandMethod("RmoveHook")] public static void CmdRmoveHook() { if (callBackFunc != null) { acedRemoveFilterWinMsg(callBackFunc); callBackFunc = null; } } public static void ShowForm(string str) { if (Frm == null || Frm.IsDisposed) { Assembly assembly = Assembly.GetExecutingAssembly(); Frm = assembly.CreateInstance($"{MethodBase.GetCurrentMethod().DeclaringType.Namespace}.{str}") as Form; size = new Size(400, 400); ZwSoft.ZwCAD.ApplicationServices.Application.ShowModelessDialog(Frm); CmdRegisterHook(); } else Frm.Show(); } //public static void ShowForm(string str) //{ // if (Frm == null || Frm.IsDisposed) // { // Assembly tempAssembly = Assembly.GetExecutingAssembly(); // Type t = tempAssembly.GetType(str); // object[] args = null; // Frm = Activator.CreateInstance(t, args) as Form; // //size = new Size(400, 400); // size = Frm.Size; // ZwSoft.ZwCAD.ApplicationServices.Application.ShowModelessDialog(Frm); // CmdRegisterHook(); // } // else Frm.Show(); //} }}