{"id":437,"date":"2025-02-17T15:00:56","date_gmt":"2025-02-17T07:00:56","guid":{"rendered":"https:\/\/yuthk.com\/?p=437"},"modified":"2025-02-17T15:00:57","modified_gmt":"2025-02-17T07:00:57","slug":"c%e5%a4%84%e7%90%86%e9%94%ae%e7%9b%98%e4%b8%ba%e8%8b%b1%e6%96%87","status":"publish","type":"post","link":"https:\/\/yuthk.com\/?p=437","title":{"rendered":"C#\u5904\u7406\u952e\u76d8\u4e3a\u82f1\u6587"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\"> \u4ee3\u7801\u8bf4\u660e<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>ImmGetDefaultIMEWnd<\/code><\/strong>\uff1a\u83b7\u53d6\u9ed8\u8ba4\u7684 IME \u7a97\u53e3\u53e5\u67c4\u3002<\/li>\n\n\n\n<li><strong><code>ImmIsIME<\/code><\/strong>\uff1a\u68c0\u67e5\u5f53\u524d\u7684\u952e\u76d8\u5e03\u5c40\u662f\u5426\u662f IME\u3002<\/li>\n\n\n\n<li><strong><code>LoadKeyboardLayout<\/code><\/strong>\uff1a\u52a0\u8f7d\u6307\u5b9a\u7684\u952e\u76d8\u5e03\u5c40\uff08\u5982\u82f1\u8bed\uff09\u3002<\/li>\n\n\n\n<li><strong><code>GetForegroundWindow<\/code><\/strong>\uff1a\u83b7\u53d6\u5f53\u524d\u524d\u666f\u7a97\u53e3\u7684\u53e5\u67c4\u3002<\/li>\n\n\n\n<li><strong><code>GetKeyboardLayout<\/code><\/strong>\uff1a\u83b7\u53d6\u5f53\u524d\u7ebf\u7a0b\u7684\u952e\u76d8\u5e03\u5c40\u3002<\/li>\n\n\n\n<li><strong><code>KLF_ACTIVATE<\/code><\/strong>\uff1a\u6fc0\u6d3b\u952e\u76d8\u5e03\u5c40\u7684\u6807\u5fd7\u3002<\/li>\n\n\n\n<li><strong><code>ENGLISH_KEYBOARD_LAYOUT<\/code><\/strong>\uff1a\u82f1\u8bed\uff08\u7f8e\u56fd\uff09\u7684\u952e\u76d8\u5e03\u5c40 ID\uff08<code>00000409<\/code>\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>using System;\nusing System.Runtime.InteropServices;\nusing System.Text;\n\nclass Program\n{\n    \/\/ \u58f0\u660e Windows API \u51fd\u6570\n    &#91;DllImport(\"imm32.dll\", CharSet = CharSet.Auto)]\n    private static extern IntPtr ImmGetDefaultIMEWnd(IntPtr hWnd);\n\n    &#91;DllImport(\"imm32.dll\", CharSet = CharSet.Auto)]\n    private static extern bool ImmIsIME(IntPtr hKL);\n\n    &#91;DllImport(\"imm32.dll\", CharSet = CharSet.Auto)]\n    private static extern IntPtr ImmGetContext(IntPtr hWnd);\n\n    &#91;DllImport(\"imm32.dll\", CharSet = CharSet.Auto)]\n    private static extern bool ImmSetOpenStatus(IntPtr hIMC, bool bOpen);\n\n    &#91;DllImport(\"imm32.dll\", CharSet = CharSet.Auto)]\n    private static extern bool ImmReleaseContext(IntPtr hWnd, IntPtr hIMC);\n\n    &#91;DllImport(\"user32.dll\", CharSet = CharSet.Auto)]\n    private static extern IntPtr GetKeyboardLayout(uint idThread);\n\n    &#91;DllImport(\"user32.dll\", CharSet = CharSet.Auto)]\n    private static extern IntPtr LoadKeyboardLayout(string pwszKLID, uint Flags);\n\n    &#91;DllImport(\"user32.dll\", CharSet = CharSet.Auto)]\n    private static extern bool UnloadKeyboardLayout(IntPtr hkl);\n\n    &#91;DllImport(\"user32.dll\", CharSet = CharSet.Auto)]\n    private static extern IntPtr GetForegroundWindow();\n\n    &#91;DllImport(\"user32.dll\", CharSet = CharSet.Auto)]\n    private static extern uint GetWindowThreadProcessId(IntPtr hWnd, IntPtr lpdwProcessId);\n\n    \/\/ \u5b9a\u4e49\u952e\u76d8\u5e03\u5c40\u5e38\u91cf\n    private const uint KLF_ACTIVATE = 0x00000001;\n    private const string ENGLISH_KEYBOARD_LAYOUT = \"00000409\"; \/\/ \u82f1\u8bed\uff08\u7f8e\u56fd\uff09\u7684\u952e\u76d8\u5e03\u5c40 ID\n\n    static void Main(string&#91;] args)\n    {\n        \/\/ \u83b7\u53d6\u5f53\u524d\u524d\u666f\u7a97\u53e3\u7684\u53e5\u67c4\n        IntPtr hWnd = GetForegroundWindow();\n\n        \/\/ \u83b7\u53d6\u5f53\u524d\u7ebf\u7a0b\u7684\u952e\u76d8\u5e03\u5c40\n        uint threadId = GetWindowThreadProcessId(hWnd, IntPtr.Zero);\n        IntPtr hKL = GetKeyboardLayout(threadId);\n\n        \/\/ \u68c0\u67e5\u5f53\u524d\u952e\u76d8\u5e03\u5c40\u662f\u5426\u662f IME\n        if (ImmIsIME(hKL))\n        {\n            Console.WriteLine(\"\u5f53\u524d\u8f93\u5165\u6cd5\u662f IME\uff0c\u6b63\u5728\u5207\u6362\u5230\u82f1\u8bed\u8f93\u5165\u6cd5...\");\n\n            \/\/ \u52a0\u8f7d\u82f1\u8bed\u952e\u76d8\u5e03\u5c40\n            IntPtr englishLayout = LoadKeyboardLayout(ENGLISH_KEYBOARD_LAYOUT, KLF_ACTIVATE);\n\n            if (englishLayout != IntPtr.Zero)\n            {\n                Console.WriteLine(\"\u5df2\u5207\u6362\u5230\u82f1\u8bed\u8f93\u5165\u6cd5\u3002\");\n            }\n            else\n            {\n                Console.WriteLine(\"\u65e0\u6cd5\u52a0\u8f7d\u82f1\u8bed\u952e\u76d8\u5e03\u5c40\u3002\");\n            }\n        }\n        else\n        {\n            Console.WriteLine(\"\u5f53\u524d\u8f93\u5165\u6cd5\u5df2\u7ecf\u662f\u975e IME \u8f93\u5165\u6cd5\uff08\u5982\u82f1\u8bed\uff09\u3002\");\n        }\n    }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4ee3\u7801\u8bf4\u660e<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-437","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/yuthk.com\/index.php?rest_route=\/wp\/v2\/posts\/437","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/yuthk.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/yuthk.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/yuthk.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/yuthk.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=437"}],"version-history":[{"count":1,"href":"https:\/\/yuthk.com\/index.php?rest_route=\/wp\/v2\/posts\/437\/revisions"}],"predecessor-version":[{"id":438,"href":"https:\/\/yuthk.com\/index.php?rest_route=\/wp\/v2\/posts\/437\/revisions\/438"}],"wp:attachment":[{"href":"https:\/\/yuthk.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=437"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/yuthk.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=437"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/yuthk.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=437"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}