using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
[assembly: CommandClass(typeof(HelpUtils.MyCommands))]
namespace HelpUtils
{
public class Helper : IDisposable
{
static int ver = Application.Version.Major;
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate int WindowHookProc(ref System.Windows.Forms.Message msg);
private WindowHookProc callBackFunc = null;
#region AutoCAD >= 2013 // Версии от 2013 и ...
// x64
[DllImport("accore.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl,
EntryPoint = "?acedRegisterFilterWinMsg@@YAHQ6AHPEAUtagMSG@@@Z@Z")]
private static extern int acedRegisterFilterWinMsg_64_R19(WindowHookProc callBackFunc);
[DllImport("accore.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl,
EntryPoint = "?acedRemoveFilterWinMsg@@YAHQ6AHPEAUtagMSG@@@Z@Z")]
private static extern int acedRemoveFilterWinMsg_64_R19(WindowHookProc callBackFunc);
// x86
[DllImport("accore.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl,
EntryPoint = "?acedRegisterFilterWinMsg@@YAHQ6AHPAUtagMSG@@@Z@Z")]
private static extern int acedRegisterFilterWinMsg_32_R19(WindowHookProc callBackFunc);
[DllImport("accore.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl,
EntryPoint = "?acedRemoveFilterWinMsg@@YAHQ6AHPAUtagMSG@@@Z@Z")]
private static extern int acedRemoveFilterWinMsg_32_R19(WindowHookProc callBackFunc);
#endregion
#region AutoCAD <= 2012 // Версии от 2007 до 2012
// x64
[DllImport("acad.exe", CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl,
EntryPoint = "?acedRegisterFilterWinMsg@@YAHQ6AHPEAUtagMSG@@@Z@Z")]
private static extern int acedRegisterFilterWinMsg_64_R18(WindowHookProc callBackFunc);
[DllImport("acad.exe", CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl,
EntryPoint = "?acedRemoveFilterWinMsg@@YAHQ6AHPEAUtagMSG@@@Z@Z")]
private static extern int acedRemoveFilterWinMsg_64_R18(WindowHookProc callBackFunc);
// x86
[DllImport("acad.exe", CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl,
EntryPoint = "?acedRegisterFilterWinMsg@@YAHQ6AHPAUtagMSG@@@Z@Z")]
private static extern int acedRegisterFilterWinMsg_32_R18(WindowHookProc callBackFunc);
[DllImport("acad.exe", CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl,
EntryPoint = "?acedRemoveFilterWinMsg@@YAHQ6AHPAUtagMSG@@@Z@Z")]
private static extern int acedRemoveFilterWinMsg_32_R18(WindowHookProc callBackFunc);
#endregion
private static int acedRegisterFilterWinMsg(WindowHookProc callBackFunc)
{
if (ver < 19)
{
if (IntPtr.Size == 4)
return acedRegisterFilterWinMsg_32_R18(callBackFunc);
else
return acedRegisterFilterWinMsg_64_R18(callBackFunc);
}
else
{
if (IntPtr.Size == 4)
return acedRegisterFilterWinMsg_32_R19(callBackFunc);
else
return acedRegisterFilterWinMsg_64_R19(callBackFunc);
}
}
private static int acedRemoveFilterWinMsg(WindowHookProc callBackFunc)
{
if (ver < 19)
{
if (IntPtr.Size == 4)
return acedRemoveFilterWinMsg_32_R18(callBackFunc);
else
return acedRemoveFilterWinMsg_64_R18(callBackFunc);
}
else
{
if (IntPtr.Size == 4)
return acedRemoveFilterWinMsg_32_R19(callBackFunc);
else
return acedRemoveFilterWinMsg_64_R19(callBackFunc);
}
}
const int WM_KEYDOWN = 0x100;
const int WM_KEYUP = 0x101;
const int F1 = 0x70;
private int WindowsHook(ref System.Windows.Forms.Message msg)
{
if (msg.Msg == WM_KEYDOWN && (int)msg.WParam == F1)
{
// Вместо запуска процесса используем стандартный класс Help
System.Windows.Forms.Help.ShowHelp(null, helpPath, helpTopic);
// Process process = new Process();
// string parameter = string.Format("mk:@MSITStore:{0}::/{1}",
// helpPath.Replace(" ", "%20"), helpTopic);
// ProcessStartInfo info = new ProcessStartInfo("hh", parameter);
// process.StartInfo = info;
// process.Start();
return 1;
}
// Блокируем запуск стандартной справки по отпусканию F1
if (msg.Msg == WM_KEYUP && (int)msg.WParam == F1)
return 1;
return 0;
}
public string helpPath = null;
// Если есть .htm на конце, то незабываем
public string helpTopic = null;
public Helper(string path, string topic)
{
helpPath = path; helpTopic = topic;
callBackFunc = new WindowHookProc(this.WindowsHook);
acedRegisterFilterWinMsg(callBackFunc);
}
public void Dispose()
{
acedRemoveFilterWinMsg(callBackFunc);
}
}
public class MyCommands
{
[CommandMethod("HelpUtils", "TestF1", CommandFlags.Modal)]
public void testHelp()
{
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
using (Helper hlp = new Helper("C:\\MyHelp.chm", "ThroughAttribute.htm"))
{
ed.GetPoint("\nНажмите F1 для ThroughAttribute: ");
// Можем динамически менять разделы справки, да и путь к файлу справки
hlp.helpTopic = "ThroughAcedSetFunHelp.htm";
ed.GetDouble("\nНажмите F1 для ThroughAcedSetFunHelp: ");
hlp.helpTopic = "ThroughProcess.htm";
ed.GetString("\nНажмите F1 для ThroughProcess: ");
}
}
}
}