代码
//声明最后的视口编号数值
//仅根据鼠标位置,获取视口编号,设置当前视口
int g_LastVp=0;
bool filterMouse(MSG *pMsg)
{
if( pMsg->message == WM_MOUSEMOVE )
{
CPoint cPnt (pMsg->lParam) ;
int nVp= acedGetWinNum(cPnt.x,cPnt.y);
if (nVp!= g_LastVp)
{
acedSetCurrentVPort(nVp);
g_LastVp=nVp;
}
}
return false;
}
//注册鼠标消息钩子
acedRegisterFilterWinMsg(filterMouse);
//移除鼠标消息钩子
acedRemoveFilterWinMsg(filterMouse);
C#例子cad2019测试
// For AutoCAD 2013 64 bit
// On previous versions, import from acad.exe (instead accore.dll)
//?acedRegisterFilterWinMsg@@YAHQ6AHPEAUtagMSG@@@Z@Z
// For AutoCAD 2019 64 bit
//?acedRegisterFilterWinMsg@@YA_NQ6A_NPEAUtagMSG@@@Z@Z
[DllImport("accore.dll",
CharSet = CharSet.Unicode,
CallingConvention = CallingConvention.Cdecl,
EntryPoint = "?acedRegisterFilterWinMsg@@YA_NQ6A_NPEAUtagMSG@@@Z@Z")]
private static extern int acedRegisterFilterWinMsg(
WindowHookProc callBackFunc);
[DllImport("accore.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl, EntryPoint = "?acedGetWinNum@@YAHHH@Z")]
private static extern int acedGetWinNum(int x,int y);
[DllImport("accore.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl, EntryPoint = "?acedSetCurrentVPort@@YA?AW4ErrorStatus@Acad@@H@Z")]
private static extern ErrorStatus acedSetCurrentVPort(int x);
// hook message filter callback function
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate int WindowHookProc(
ref System.Windows.Forms.Message msg);
private const int WM_MOUSEMOVE = 0x0200;
private static int WindowsHook(
ref System.Windows.Forms.Message msg)
{
// check the msg struct for whatever we want,
// like keys, paint messages etc
if (msg.Msg == WM_MOUSEMOVE)
{
// do something
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
int x = msg.LParam.ToInt32() & 0xffff;
int y = (msg.LParam.ToInt32() >> 16);
var nvp2 = acedGetWinNum(x, y);
acedSetCurrentVPort(nvp2);
//ed.WriteMessage("\n 视口编号 " + nvp2.ToString());
}
return 0;
}
private static WindowHookProc callBackFunc = null;
[CommandMethod("registerHook")]
public static void CmdRegisterHook()
{
callBackFunc = new WindowHookProc(WindowsHook);
acedRegisterFilterWinMsg(callBackFunc);
}
截图