{"id":104,"date":"2016-05-12T11:17:55","date_gmt":"2016-05-12T03:17:55","guid":{"rendered":"http:\/\/www.lnzdl.com\/blog\/?p=104"},"modified":"2016-05-12T11:17:55","modified_gmt":"2016-05-12T03:17:55","slug":"c%e5%ae%9e%e7%8e%b0%e5%bf%ab%e6%8d%b7%e9%94%ae%e7%b3%bb%e7%bb%9f%e7%83%ad%e9%94%ae%e5%93%8d%e5%ba%94","status":"publish","type":"post","link":"http:\/\/www.lnzdl.com\/blog\/c%e5%ae%9e%e7%8e%b0%e5%bf%ab%e6%8d%b7%e9%94%ae%e7%b3%bb%e7%bb%9f%e7%83%ad%e9%94%ae%e5%93%8d%e5%ba%94\/","title":{"rendered":"C#\u5b9e\u73b0\u5feb\u6377\u952e(\u7cfb\u7edf\u70ed\u952e)\u54cd\u5e94"},"content":{"rendered":"<p>\u5728\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u53ef\u80fd\u4f1a\u9700\u8981\u5b9e\u73b0\u50cfCtrl+C\u590d\u5236\u3001Ctrl+V\u7c98\u8d34\u8fd9\u6837\u7684\u5feb\u6377\u952e\uff0c\u672c\u6587\u7b80\u5355\u4ecb\u7ecd\u4e86\u5b83\u7684\u5b9e\u73b0\uff0c\u5e76\u7ed9\u51fa\u4e86\u4e00\u4e2a\u5b9e\u73b0\u7c7b\u3002<br \/>\uff081\uff09\u5efa\u7acb\u4e00\u4e2a\u7c7b\u6587\u4ef6\uff0c\u547d\u540d\u4e3aHotKey.cs\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<br \/>using System;<br \/>using System.Collections.Generic;<br \/>using System.Runtime.InteropServices;<br \/>using System.Windows.Forms;<br \/>namespace KoalaStudio.BookshopManager<br \/>{<br \/>class HotKey<br \/>&nbsp;&nbsp;&nbsp; {<br \/>\/\/\u5982\u679c\u51fd\u6570\u6267\u884c\u6210\u529f\uff0c\u8fd4\u56de\u503c\u4e0d\u4e3a0\u3002<br \/>\/\/\u5982\u679c\u51fd\u6570\u6267\u884c\u5931\u8d25\uff0c\u8fd4\u56de\u503c\u4e3a0\u3002\u8981\u5f97\u5230\u6269\u5c55\u9519\u8bef\u4fe1\u606f\uff0c\u8c03\u7528GetLastError\u3002<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [DllImport(&#8220;user32.dll&#8221;, SetLastError = true)]<br \/>public static extern bool RegisterHotKey(<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; IntPtr hWnd,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \/\/\u8981\u5b9a\u4e49\u70ed\u952e\u7684\u7a97\u53e3\u7684\u53e5\u67c4<br \/>int id,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \/\/\u5b9a\u4e49\u70ed\u952eID\uff08\u4e0d\u80fd\u4e0e\u5176\u5b83ID\u91cd\u590d\uff09&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; KeyModifiers fsModifiers,&nbsp;&nbsp; \/\/\u6807\u8bc6\u70ed\u952e\u662f\u5426\u5728\u6309Alt\u3001Ctrl\u3001Shift\u3001Windows\u7b49\u952e\u65f6\u624d\u4f1a\u751f\u6548<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Keys vk&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \/\/\u5b9a\u4e49\u70ed\u952e\u7684\u5185\u5bb9<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; );<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [DllImport(&#8220;user32.dll&#8221;, SetLastError = true)]<br \/>public static extern bool UnregisterHotKey(<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; IntPtr hWnd,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \/\/\u8981\u53d6\u6d88\u70ed\u952e\u7684\u7a97\u53e3\u7684\u53e5\u67c4<br \/>int id&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \/\/\u8981\u53d6\u6d88\u70ed\u952e\u7684ID<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; );<br \/>\/\/\u5b9a\u4e49\u4e86\u8f85\u52a9\u952e\u7684\u540d\u79f0\uff08\u5c06\u6570\u5b57\u8f6c\u53d8\u4e3a\u5b57\u7b26\u4ee5\u4fbf\u4e8e\u8bb0\u5fc6\uff0c\u4e5f\u53ef\u53bb\u9664\u6b64\u679a\u4e3e\u800c\u76f4\u63a5\u4f7f\u7528\u6570\u503c\uff09<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [Flags()]<br \/>public enum KeyModifiers<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; None = 0,<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Alt = 1,<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Ctrl = 2,<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Shift = 4,<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; WindowsKey = 8<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br \/>&nbsp;&nbsp;&nbsp; }<br \/>}<br \/>\u7b80\u5355\u8bf4\u660e\u4e00\u4e0b\uff1a<br \/>\u201cpublic static extern bool RegisterHotKey()\u201d\u8fd9\u4e2a\u51fd\u6570\u7528\u4e8e\u6ce8\u518c\u70ed\u952e\u3002\u7531\u4e8e\u8fd9\u4e2a\u51fd\u6570\u9700\u8981\u5f15\u7528user32.dll\u52a8\u6001\u94fe\u63a5\u5e93\u540e\u624d\u80fd\u4f7f\u7528\uff0c\u5e76\u4e14<br \/>user32.dll\u662f\u975e\u6258\u7ba1\u4ee3\u7801\uff0c\u4e0d\u80fd\u7528\u547d\u540d\u7a7a\u95f4\u7684\u65b9\u5f0f\u76f4\u63a5\u5f15\u7528\uff0c\u6240\u4ee5\u9700\u8981\u7528\u201cDllImport\u201d\u8fdb\u884c\u5f15\u5165\u540e\u624d\u80fd\u4f7f\u7528\u3002\u4e8e\u662f\u5728\u51fd\u6570\u524d\u9762\u9700\u8981\u52a0\u4e0a<br \/>\u201c[DllImport(&#8220;user32.dll&#8221;, SetLastError = true)]\u201d\u8fd9\u884c\u8bed\u53e5\u3002<br \/>\u201cpublic static extern bool UnregisterHotKey()\u201d\u8fd9\u4e2a\u51fd\u6570\u7528\u4e8e\u6ce8\u9500\u70ed\u952e\uff0c\u540c\u7406\u4e5f\u9700\u8981\u7528DllImport\u5f15\u7528user32.dll\u540e\u624d\u80fd\u4f7f\u7528\u3002<br \/>\u201cpublic enum KeyModifiers{}\u201d\u5b9a\u4e49\u4e86\u4e00\u7ec4\u679a\u4e3e\uff0c\u5c06\u8f85\u52a9\u952e\u7684\u6570\u5b57\u4ee3\u7801\u76f4\u63a5\u8868\u793a\u4e3a\u6587\u5b57\uff0c\u4ee5\u65b9\u4fbf\u4f7f\u7528\u3002\u8fd9\u6837\u5728\u8c03\u7528\u65f6\u6211\u4eec\u4e0d\u5fc5\u8bb0\u4f4f\u6bcf\u4e00\u4e2a\u8f85<br \/>\u52a9\u952e\u7684\u4ee3\u7801\u800c\u53ea\u9700\u76f4\u63a5\u9009\u62e9\u5176\u540d\u79f0\u5373\u53ef\u3002<br \/>\uff082\uff09\u4ee5\u7a97\u4f53FormA\u4e3a\u4f8b\uff0c\u4ecb\u7ecdHotKey\u7c7b\u7684\u4f7f\u7528<br \/>\u5728FormA\u7684Activate\u4e8b\u4ef6\u4e2d\u6ce8\u518c\u70ed\u952e\uff0c\u672c\u4f8b\u4e2d\u6ce8\u518cShift+S\uff0cCtrl+Z\uff0cAlt+D\u8fd9\u4e09\u4e2a\u70ed\u952e\u3002\u8fd9\u91cc\u7684Id\u53f7\u53ef\u4efb\u610f\u8bbe\u7f6e\uff0c\u4f46\u8981\u4fdd\u8bc1\u4e0d\u88ab\u91cd\u590d\u3002<br \/>private void Form_Activated(object sender, EventArgs e)<br \/>{<br \/>\/\/\u6ce8\u518c\u70ed\u952eShift+S\uff0cId\u53f7\u4e3a100\u3002HotKey.KeyModifiers.Shift\u4e5f\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u6570\u5b574\u6765\u8868\u793a\u3002<br \/>&nbsp;&nbsp;&nbsp; HotKey.RegisterHotKey(Handle, 100, HotKey.KeyModifiers.Shift, Keys.S); <br \/>\/\/\u6ce8\u518c\u70ed\u952eCtrl+B\uff0cId\u53f7\u4e3a101\u3002HotKey.KeyModifiers.Ctrl\u4e5f\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u6570\u5b572\u6765\u8868\u793a\u3002<br \/>&nbsp;&nbsp;&nbsp; HotKey.RegisterHotKey(Handle, 101, HotKey.KeyModifiers.Ctrl, Keys.B);<br \/>\/\/\u6ce8\u518c\u70ed\u952eAlt+D\uff0cId\u53f7\u4e3a102\u3002HotKey.KeyModifiers.Alt\u4e5f\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u6570\u5b571\u6765\u8868\u793a\u3002<br \/>&nbsp;&nbsp;&nbsp; HotKey.RegisterHotKey(Handle, 102, HotKey.KeyModifiers.Alt, Keys.D);<br \/>}<br \/>\u5728FormA\u7684Leave\u4e8b\u4ef6\u4e2d\u6ce8\u9500\u70ed\u952e\u3002<br \/>private void FrmSale_Leave(object sender, EventArgs e)<br \/>{<br \/>\/\/\u6ce8\u9500Id\u53f7\u4e3a100\u7684\u70ed\u952e\u8bbe\u5b9a<br \/>&nbsp;&nbsp;&nbsp; HotKey.UnregisterHotKey(Handle, 100);<br \/>\/\/\u6ce8\u9500Id\u53f7\u4e3a101\u7684\u70ed\u952e\u8bbe\u5b9a<br \/>&nbsp;&nbsp;&nbsp; HotKey.UnregisterHotKey(Handle, 101);\/\/ <a href=\"http:\/\/ike.126.com\/\">http:\/\/ike.126.com<\/a><br \/>\/\/\u6ce8\u9500Id\u53f7\u4e3a102\u7684\u70ed\u952e\u8bbe\u5b9a<br \/>&nbsp;&nbsp;&nbsp; HotKey.UnregisterHotKey(Handle, 102);<br \/>}<br \/>\u91cd\u8f7dFromA\u4e2d\u7684WndProc\u51fd\u6570<br \/>\/\/\/<br \/>\/\/\/ \u76d1\u89c6Windows\u6d88\u606f<br \/>\/\/\/ \u91cd\u8f7dWndProc\u65b9\u6cd5\uff0c\u7528\u4e8e\u5b9e\u73b0\u70ed\u952e\u54cd\u5e94<br \/>\/\/\/<br \/>\/\/\/<br \/>protected override void WndProc(ref Message m)<br \/>{<br \/>const int WM_HOTKEY = 0x0312;\/\/\u5982\u679cm.Msg\u7684\u503c\u4e3a0x0312\u90a3\u4e48\u8868\u793a\u7528\u6237\u6309\u4e0b\u4e86\u70ed\u952e<br \/>\/\/\u6309\u5feb\u6377\u952e <br \/>switch (m.Msg)<br \/>&nbsp;&nbsp;&nbsp; {<br \/>case WM_HOTKEY:<br \/>switch (m.WParam.ToInt32())<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br \/>case 100:&nbsp;&nbsp;&nbsp; \/\/\u6309\u4e0b\u7684\u662fShift+S<br \/>\/\/\u6b64\u5904\u586b\u5199\u5feb\u6377\u952e\u54cd\u5e94\u4ee3\u7801&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br \/>break;<br \/>case 101:&nbsp;&nbsp;&nbsp; \/\/\u6309\u4e0b\u7684\u662fCtrl+B<br \/>\/\/\u6b64\u5904\u586b\u5199\u5feb\u6377\u952e\u54cd\u5e94\u4ee3\u7801<br \/>break;<br \/>case 102:&nbsp;&nbsp;&nbsp; \/\/\u6309\u4e0b\u7684\u662fAlt+D<br \/>\/\/\u6b64\u5904\u586b\u5199\u5feb\u6377\u952e\u54cd\u5e94\u4ee3\u7801<br \/>break;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br \/>break;<br \/>&nbsp;&nbsp;&nbsp; }<br \/>base.WndProc(ref m);<br \/>}<br \/>\u5b8c\u6210\u4ee3\u7801\u540e\uff0c\u6211\u4eec\u5728\u7a97\u4f53\u4e2d\u6309\u4e0bShift+S\u3001Ctrl+B\u3001Alt+D\u8fd9\u4e09\u7ec4\u5feb\u6377\u952e\u4e2d\u7684\u4efb\u610f\u4e00\u7ec4\u65f6\uff0c\u7a0b\u5e8f\u90fd\u4f1a\u505a\u51fa\u54cd\u5e94\u7684\u53cd\u5e94\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u53ef\u80fd\u4f1a\u9700\u8981\u5b9e\u73b0\u50cfCtrl+C\u590d\u5236\u3001Ctrl+V\u7c98\u8d34\u8fd9\u6837\u7684\u5feb\u6377\u952e\uff0c\u672c\u6587\u7b80\u5355\u4ecb\u7ecd\u4e86\u5b83\u7684\u5b9e\u73b0\uff0c\u5e76\u7ed9\u51fa\u4e86\u4e00\u4e2a\u5b9e\u73b0\u7c7b\u3002\uff081\uff09\u5efa\u7acb\u4e00\u4e2a\u7c7b\u6587\u4ef6\uff0c\u547d\u540d\u4e3aHotKey.cs\uff0c\u4ee3\u7801\u5982\u4e0b\uff1ausing System;using System.Collections.Generic;using System.Runtime.InteropServices;using System.Windows.Forms; [&hellip;]<\/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":[],"_links":{"self":[{"href":"http:\/\/www.lnzdl.com\/blog\/wp-json\/wp\/v2\/posts\/104"}],"collection":[{"href":"http:\/\/www.lnzdl.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.lnzdl.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.lnzdl.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.lnzdl.com\/blog\/wp-json\/wp\/v2\/comments?post=104"}],"version-history":[{"count":1,"href":"http:\/\/www.lnzdl.com\/blog\/wp-json\/wp\/v2\/posts\/104\/revisions"}],"predecessor-version":[{"id":105,"href":"http:\/\/www.lnzdl.com\/blog\/wp-json\/wp\/v2\/posts\/104\/revisions\/105"}],"wp:attachment":[{"href":"http:\/\/www.lnzdl.com\/blog\/wp-json\/wp\/v2\/media?parent=104"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.lnzdl.com\/blog\/wp-json\/wp\/v2\/categories?post=104"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.lnzdl.com\/blog\/wp-json\/wp\/v2\/tags?post=104"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}