Sample that demonstrate common source code for different AutoCAD version and Platform (32 or 64 bit):
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using AcAp = Autodesk.AutoCAD.ApplicationServices;
[assembly: CommandClass(typeof(Rivilis.HookESC))]
namespace Rivilis
{
public static class HookESC
{
static int AcadVer = AcAp.Application.Version.Major;
// For AutoCAD 2007 - 2012 64 bit
[DllImport("acad.exe", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl,
EntryPoint = "?acedRegisterFilterWinMsg@@YAHQ6AHPEAUtagMSG@@@Z@Z")]
private static extern int acedRegisterFilterWinMsg2012x64(WindowHookProc callBackFunc);
[DllImport("acad.exe", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl,
EntryPoint = "?acedRemoveFilterWinMsg@@YAHQ6AHPEAUtagMSG@@@Z@Z")]
private static extern int acedRemoveFilterWinMsg2012x64(WindowHookProc callBackFunc);
// For AutoCAD 2007 - 2012 32 bit
[DllImport("acad.exe", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl,
EntryPoint = "?acedRegisterFilterWinMsg@@YAHQ6AHPAUtagMSG@@@Z@Z")]
private static extern int acedRegisterFilterWinMsg2012x32(WindowHookProc callBackFunc);
[DllImport("acad.exe", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl,
EntryPoint = "?acedRemoveFilterWinMsg@@YAHQ6AHPAUtagMSG@@@Z@Z")]
private static extern int acedRemoveFilterWinMsg2012x32(WindowHookProc callBackFunc);
// For AutoCAD 2013 64 bit
[DllImport("accore.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl,
EntryPoint = "?acedRegisterFilterWinMsg@@YAHQ6AHPEAUtagMSG@@@Z@Z")]
private static extern int acedRegisterFilterWinMsg2013x64(WindowHookProc callBackFunc);
[DllImport("accore.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl,
EntryPoint = "?acedRemoveFilterWinMsg@@YAHQ6AHPEAUtagMSG@@@Z@Z")]
private static extern int acedRemoveFilterWinMsg2013x64(WindowHookProc callBackFunc);
// For AutoCAD 2013 32 bit
[DllImport("accore.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl,
EntryPoint = "?acedRegisterFilterWinMsg@@YAHQ6AHPAUtagMSG@@@Z@Z")]
private static extern int acedRegisterFilterWinMsg2013x32(WindowHookProc callBackFunc);
[DllImport("accore.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl,
EntryPoint = "?acedRemoveFilterWinMsg@@YAHQ6AHPAUtagMSG@@@Z@Z")]
private static extern int acedRemoveFilterWinMsg2013x32(WindowHookProc callBackFunc);
private static int acedRegisterFilterWinMsg(WindowHookProc callBackFunc)
{
if (AcadVer >= 19) {
if (IntPtr.Size == 4) return acedRegisterFilterWinMsg2013x32(callBackFunc);
else return acedRegisterFilterWinMsg2013x64(callBackFunc);
} else {
if (IntPtr.Size == 4) return acedRegisterFilterWinMsg2012x32(callBackFunc);
else return acedRegisterFilterWinMsg2012x64(callBackFunc);
}
}
private static int acedRemoveFilterWinMsg(WindowHookProc callBackFunc)
{
if (AcadVer >= 19) {
if (IntPtr.Size == 4) return acedRemoveFilterWinMsg2013x32(callBackFunc);
else return acedRemoveFilterWinMsg2013x64(callBackFunc);
}
else {
if (IntPtr.Size == 4) return acedRemoveFilterWinMsg2012x32(callBackFunc);
else return acedRemoveFilterWinMsg2012x64(callBackFunc);
}
}
// hook message filter callback function
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate int WindowHookProc(ref Message msg);
private static WindowHookProc callBackFunc = null;
private static int WindowsHook(ref Message msg)
{
if (msg.Msg == 0x0100 && msg.WParam == (IntPtr)0x1b) {
// MessageBox.Show("ESC pressed"); // <- your's action
// acedRemoveFilterWinMsg(callBackFunc); // <- unregister callback
}
return 0;
}
[CommandMethod("SetHookEsc")]
public static void SetHookEsc()
{
callBackFunc = new WindowHookProc(WindowsHook);
acedRegisterFilterWinMsg(callBackFunc);
}
[CommandMethod("UnsetHookEsc")]
public static void UnsetHookEsc()
{
if (callBackFunc != null) acedRemoveFilterWinMsg(callBackFunc);
}
}
}
Відповідь корисна? Клікніть на "ВПОДОБАЙКУ" цім повідомленням! | Do you find the posts helpful? "LIKE" these posts!
Находите сообщения полезными? Поставьте "НРАВИТСЯ" этим сообщениям!
На ваше запитання відповіли? Натисніть кнопку "ПРИЙНЯТИ РІШЕННЯ" | Have your question been answered successfully? Click "ACCEPT SOLUTION" button.
На ваш вопрос успешно ответили? Нажмите кнопку "УТВЕРДИТЬ РЕШЕНИЕ"
Alexander Rivilis / Александр Ривилис / Олександр Рівіліс
Programmer & Teacher & Helper / Программист - Учитель - Помощник / Програміст - вчитель - помічник
Facebook | Twitter | LinkedIn
