diff --git a/.gitignore b/.gitignore index dfcfd56..6b44058 100644 --- a/.gitignore +++ b/.gitignore @@ -348,3 +348,5 @@ MigrationBackup/ # Ionide (cross platform F# VS Code tools) working folder .ionide/ +/AutoIME安装使用说明.pdf +/AutoIME.zip diff --git a/AutoIME/AutoIME/Program.cs b/AutoIME/AutoIME/Program.cs index e250f32..4d0554d 100644 --- a/AutoIME/AutoIME/Program.cs +++ b/AutoIME/AutoIME/Program.cs @@ -1,6 +1,9 @@ -using Autodesk.AutoCAD.EditorInput; +using Autodesk.AutoCAD.DatabaseServices; +using Autodesk.AutoCAD.EditorInput; using Autodesk.AutoCAD.Runtime; using System; +using System.Runtime.InteropServices; +using System.Text; using System.Windows.Forms; using AcadApplication = Autodesk.AutoCAD.ApplicationServices.Application; @@ -9,8 +12,33 @@ namespace AutoIME { public class Program : IExtensionApplication { + //delegate void WinEventDelegate(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime); + + //[DllImport("user32.dll")] + //static extern IntPtr SetWinEventHook(uint eventMin, uint eventMax, IntPtr hmodWinEventProc, WinEventDelegate lpfnWinEventProc, uint idProcess, uint idThread, uint dwFlags); + + //private const uint WINEVENT_OUTOFCONTEXT = 0; + //private const uint EVENT_SYSTEM_FOREGROUND = 3; + //[DllImport("user32.dll")] + //static extern IntPtr GetForegroundWindow(); + + //[DllImport("user32.dll")] + //static extern IntPtr GetActiveWindow(); + + //[DllImport("user32.dll")] + //static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count); + + //[DllImport("user32.dll", SetLastError = true)] + //static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId); + + //[DllImport("user32.dll", ExactSpelling = true, CharSet = CharSet.Auto)] + //public static extern IntPtr GetParent(IntPtr hWnd); + + //static WinEventDelegate dele = null; + + public const string AppName = "AutoIME"; - public static readonly Editor Editor = AcadApplication.DocumentManager.MdiActiveDocument.Editor; + public static Editor Editor => AcadApplication.DocumentManager.MdiActiveDocument.Editor; private readonly Config _config = Config.GetConfigInstance(); [CommandMethod("iAutoIME")] @@ -31,7 +59,7 @@ public static void Uninstall() { Editor.WriteMessage("================<<卸载开始>>================\n"); UnregisterApp(); - // unbind command + Setup.UnbindCommandToDoc(AcadApplication.DocumentManager.MdiActiveDocument); Editor.WriteMessage("================<<卸载完成>>================\n"); } @@ -66,6 +94,12 @@ public static void UnregisterApp() Editor.WriteMessage("================<<注销成功>>================\n"); } + internal static void TearDown() + { + Config.GetConfigInstance().Switch2DefaultIME(); + //dele = null; + } + [CommandMethod("SetIME")] public static void SetIME() { @@ -92,16 +126,51 @@ public static void SetIME() public void Initialize() { + //dele = new WinEventDelegate(WinEventProc); + //IntPtr m_hhook = SetWinEventHook(EVENT_SYSTEM_FOREGROUND, EVENT_SYSTEM_FOREGROUND, IntPtr.Zero, dele, 0, 0, WINEVENT_OUTOFCONTEXT); _config.Initialize(); _config.Switch2CommandIME(); Setup.InitialBinding(); + AppDomain currentDomain = AppDomain.CurrentDomain; + currentDomain.UnhandledException += CurrentDomain_UnhandledException; + + Editor.WriteMessage("================<>================\n"); + } + private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs args) + { + System.Exception e = (System.Exception)args.ExceptionObject; + System.IO.File.AppendAllText(@"autoimeerror.txt", e.Message); + System.IO.File.AppendAllText(@"autoimeerror.txt", e.StackTrace); + System.IO.File.AppendAllText(@"autoimeerror.txt", "\n"); } + //private string GetActiveWindowTitle() + //{ + // const int nChars = 256; + // IntPtr handle = IntPtr.Zero; + // StringBuilder Buff = new StringBuilder(nChars); + // handle = GetForegroundWindow(); + // if (GetWindowText(handle, Buff, nChars) > 0) + // { + // return Buff.ToString(); + // } + // return null; + //} + + //private void WinEventProc(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime) + //{ + // var threadID = GetWindowThreadProcessId(GetForegroundWindow(), out uint processID); + // var title = GetActiveWindowTitle(); + // var lines = $"PID: {processID} TID: {threadID} Title: {title}\n"; + + // System.IO.File.AppendAllText(@"gettitle.txt", lines); + //} + public void Terminate() { - _config.Switch2DefaultIME(); + TearDown(); } } diff --git a/AutoIME/AutoIME/Setup.cs b/AutoIME/AutoIME/Setup.cs index a7e19c9..8844c7b 100644 --- a/AutoIME/AutoIME/Setup.cs +++ b/AutoIME/AutoIME/Setup.cs @@ -80,7 +80,7 @@ private static void DocumentManager_DocumentDestroyed(object sender, DocumentDes private static void QuitWillStart(object sender, EventArgs e) { - _config.Switch2DefaultIME(); + Program.TearDown(); } public static void UnbindCommandToDoc(AcadDocument doc) diff --git a/README.md b/README.md index 3204796..4334150 100644 --- a/README.md +++ b/README.md @@ -63,6 +63,6 @@ 2. `rhfa` -- 此为 “卸载” 的五笔编码 3. `xzAutoIME` -- `xz`为 ”卸载“ 拼音首字母 -2. 显示以下信息即为安装成功: +2. 显示以下信息即为卸载成功: ![image-20200504231359788](README/image-20200504231359788.png) \ No newline at end of file