{"id":1254,"date":"2012-10-30T18:09:26","date_gmt":"2012-10-30T10:09:26","guid":{"rendered":"https:\/\/www.pdbeta.com\/wpblog\/?p=1254"},"modified":"2017-08-28T21:43:22","modified_gmt":"2017-08-28T13:43:22","slug":"%e3%80%90%e8%bd%ac%e8%bd%bd%e3%80%91-net%e4%b8%8e%e5%85%b6%e4%bb%96%e7%a8%8b%e5%ba%8f%e4%ba%a4%e4%ba%92%ef%bc%88api","status":"publish","type":"post","link":"https:\/\/www.pdbeta.com\/wpblog\/?p=1254","title":{"rendered":"\u3010\u8f6c\u8f7d\u3011.net\u4e0e\u5176\u4ed6\u7a0b\u5e8f\u4ea4\u4e92\uff08API)"},"content":{"rendered":"<p>\u4e00\u3001\u672c\u4f8b\u5b50\uff0c\u7528 C# \u7684\u4e00\u4e2aWinform\u7a0b\u5e8f\uff0c\u521b\u5efa\u4e00\u4e2a\u6307\u5b9a\u8def\u5f84\u7684\u6587\u672c\u6587\u4ef6\uff0c\u5199\u5165\u67d0\u4e9b\u5185\u5bb9\u540e\uff0c\u5e76\u4fdd\u5b58\u3002<\/p>\n<p>\u4e3a\u4e86\u770b\u6e05\u6548\u679c\uff0c\u5c06 Form \u7684 TopMost \u8bbe\u4e3a true<\/p>\n<blockquote><p>Code<br \/>\nprivate void button1_Click( object sender, EventArgs e )<br \/>\n{<br \/>\n\/\/\u542f\u52a8notepad.exe \u8bb0\u4e8b\u672c\u7a0b\u5e8f\uff0c\u5e76\u5728d:\\\u4e0b\u521b\u5efa \u6216 \u6253\u5f00 text_test.txt\u6587\u4ef6<br \/>\nSystem.Diagnostics.Process txt = System.Diagnostics.Process.Start ( @&#8221;notepad.exe&#8221;, @&#8221;d:\\text_test.txt&#8221; );<br \/>\ntxt.StartInfo.WindowStyle = ProcessWindowStyle.Normal;<br \/>\n\/\/\u7b49\u5f85\u4e00\u79d2\uff0c\u4ee5\u4fbf\u76ee\u6807\u7a0b\u5e8fnotepad.exe\u8f93\u5165\u72b6\u6001\u5c31\u7eea<br \/>\ntxt.WaitForInputIdle ( 1000 );<br \/>\n\/\/\u5982\u679c\u76ee\u6807\u7a0b\u5e8f notepad.exe \u6ca1\u6709\u505c\u6b62\u54cd\u5e94\uff0c\u5219\u7ee7\u7eed<br \/>\nif ( txt.Responding )<br \/>\n{<br \/>\n\/\/\u5f00\u59cb\u5199\u5165\u5185\u5bb9<br \/>\nSendKeys.SendWait ( &#8220;&#8212;&#8211;\u4e0b\u9762\u7684\u5185\u5bb9\u662f\u5916\u90e8\u7a0b\u5e8f\u81ea\u52a8\u5199\u5165&#8212;&#8211;\\r\\n&#8221; );<br \/>\nSendKeys.SendWait ( this.textBox1.Text );\u00a0 \/\/\u5c06\u6587\u672c\u6846\u5185\u7684\u5185\u5bb9\u5199\u5165<br \/>\nSendKeys.SendWait ( &#8220;{Enter}{Enter}&#8221; );\u00a0 \/\/\u5199\u51652\u4e2a\u56de\u8f66<br \/>\nSendKeys.SendWait ( &#8220;\u6587\u6863\u521b\u5efa\u65f6\u95f4\uff1a&#8221; );<br \/>\nSendKeys.SendWait ( &#8220;{F5}&#8221; );\u00a0 \/\/\u53d1\u9001F5\u6309\u952e<br \/>\nSendKeys.SendWait (&#8220;{Enter}&#8221;);\u00a0 \/\/\u53d1\u9001\u56de\u8f66\u952e<br \/>\nSendKeys.SendWait ( &#8220;^s&#8221; );\u00a0 \/\/\u53d1\u9001 Ctrl + s \u952e<br \/>\nSendKeys.SendWait ( &#8220;%{F4}&#8221; );\u00a0 \/\/ \u53d1\u9001 Alt + F4 \u952e<br \/>\nMessageBox.Show (&#8220;\u6587\u4ef6\u5df2\u7ecf\u4fdd\u5b58\u6210\u529f\uff01&#8221;);<br \/>\n}<br \/>\n}<\/p><\/blockquote>\n<p><strong>\u6ce8\uff1a SendKeys \u53d1\u9001\u7684\u6309\u952e\u7684\u63a5\u6536\u7a97\u4f53\uff0c\u5fc5\u987b\u662f\u5f53\u524d\u7684\u6d3b\u52a8\u7a97\u4f53<\/strong><\/p>\n<p>\u4e8c\u3001<strong>C# \u5229\u7528 Windows API \u63a7\u5236 \u5916\u90e8\u7684 win32 \u7a0b\u5e8f<\/strong><\/p>\n<p><strong>\u6211\u4eec\u8fd9\u91cc\uff0c\u7528\u63a7\u5236 \u201c\u8ba1\u7b97\u5668\u201d\u7a0b\u5e8f\uff0c\u7b97\u4e00\u4e2a 3 + 2 = 5 \u7684\u7b97\u5f0f\u6765\u505a\u793a\u4f8b\u3002<\/strong><\/p>\n<p>API \u4e2d\u6211\u4eec\u8981\u7528\u5230\u7684\u51fd\u6570\u6709 <strong>FindWindow<\/strong>, <strong>FindWindowEx<\/strong>, <strong>SendMessage<\/strong>, <strong>SetForegroundWindow<\/strong><\/p>\n<p><strong>\u5bf9\u4e8eAPI\u7684\u5f15\u7528\u65b9\u6cd5\uff0c\u5927\u5bb6\u53bb\u770b API \u624b\u518c\u3002<\/strong>\u8981\u4f7f\u7528API\uff0c\u9700\u8981\u5f15\u5165\u547d\u540d\u7a7a\u95f4<\/p>\n<blockquote><p>Code<br \/>\nusing System.Runtime.InteropServices;<\/p>\n<p>\u4e0b\u9762\u7684API\u5f15\u7528\u90e8\u5206\u7684\u4ee3\u7801\uff0c\u653e\u5165 class \u5185\u90e8<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" title=\".net\u4e0e\u5176\u4ed6\u7a0b\u5e8f\u4ea4\u4e92\uff08API)\" alt=\".net\u4e0e\u5176\u4ed6\u7a0b\u5e8f\u4ea4\u4e92\uff08API)\" src=\"http:\/\/kb.cnblogs.com\/Images\/OutliningIndicators\/ExpandedBlockStart.gif\" width=\"11\" height=\"16\" align=\"top\" \/>Code<br \/>\n[DllImport ( &#8220;user32.dll&#8221;, EntryPoint = &#8220;FindWindow&#8221;, SetLastError = true )]<br \/>\nprivate static extern IntPtr FindWindow( string lpClassName, string lpWindowName );<br \/>\n[DllImport ( &#8220;user32.dll&#8221;, EntryPoint = &#8220;FindWindowEx&#8221;, SetLastError = true )]<br \/>\nprivate static extern IntPtr FindWindowEx( IntPtr hwndParent, uint hwndChildAfter, string lpszClass, string lpszWindow );<br \/>\n[DllImport ( &#8220;user32.dll&#8221;, EntryPoint = &#8220;SendMessage&#8221;, SetLastError = true, CharSet = CharSet.Auto )]<br \/>\nprivate static extern int SendMessage( IntPtr hwnd, uint wMsg, int wParam, int lParam );<br \/>\n[DllImport ( &#8220;user32.dll&#8221;, EntryPoint = &#8220;SetForegroundWindow&#8221;, SetLastError = true )]<br \/>\nprivate static extern void SetForegroundWindow( IntPtr hwnd );<\/p>\n<p>Code<br \/>\nprivate void button1_Click( object sender, EventArgs e )<br \/>\n{<br \/>\nconst uint BM_CLICK = 0xF5; \/\/\u9f20\u6807\u70b9\u51fb\u7684\u6d88\u606f\uff0c\u5bf9\u4e8e\u5404\u79cd\u6d88\u606f\u7684\u6570\u503c\uff0c\u5927\u5bb6\u8fd8\u662f\u5f97\u53bbAPI\u624b\u518c<br \/>\nIntPtr hwndCalc = FindWindow ( null, &#8220;\u8ba1\u7b97\u5668&#8221; ); \/\/\u67e5\u627e\u8ba1\u7b97\u5668\u7684\u53e5\u67c4<br \/>\nif ( hwndCalc != IntPtr.Zero )<br \/>\n{<br \/>\nIntPtr hwndThree = FindWindowEx ( hwndCalc, 0, null, &#8220;3&#8221; ); \/\/\u83b7\u53d6\u6309\u94ae3 \u7684\u53e5\u67c4<br \/>\nIntPtr hwndPlus = FindWindowEx ( hwndCalc, 0, null, &#8220;+&#8221; );\u00a0 \/\/\u83b7\u53d6\u6309\u94ae + \u7684\u53e5\u67c4<br \/>\nIntPtr hwndTwo = FindWindowEx ( hwndCalc, 0, null, &#8220;2&#8221; );\u00a0 \/\/\u83b7\u53d6\u6309\u94ae2 \u7684\u53e5\u67c4<br \/>\nIntPtr hwndEqual = FindWindowEx ( hwndCalc, 0, null, &#8220;=&#8221; ); \/\/\u83b7\u53d6\u6309\u94ae= \u7684\u53e5\u67c4<br \/>\nSetForegroundWindow ( hwndCalc );\u00a0 \/\/\u5c06\u8ba1\u7b97\u5668\u8bbe\u4e3a\u5f53\u524d\u6d3b\u52a8\u7a97\u53e3<br \/>\nSystem.Threading.Thread.Sleep ( 2000 );\u00a0 \/\/\u6682\u505c2\u79d2\u8ba9\u4f60\u770b\u5230\u6548\u679c<br \/>\nSendMessage ( hwndThree, BM_CLICK, 0, 0 );<br \/>\nSystem.Threading.Thread.Sleep ( 2000 );\u00a0 \/\/\u6682\u505c2\u79d2\u8ba9\u4f60\u770b\u5230\u6548\u679c<br \/>\nSendMessage ( hwndPlus, BM_CLICK, 0, 0 );<br \/>\nSystem.Threading.Thread.Sleep ( 2000 );\u00a0 \/\/\u6682\u505c2\u79d2\u8ba9\u4f60\u770b\u5230\u6548\u679c<br \/>\nSendMessage ( hwndTwo, BM_CLICK, 0, 0 );<br \/>\nSystem.Threading.Thread.Sleep ( 2000 );\u00a0 \/\/\u6682\u505c2\u79d2\u8ba9\u4f60\u770b\u5230\u6548\u679c<br \/>\nSendMessage ( hwndEqual, BM_CLICK, 0, 0 );<br \/>\nSystem.Threading.Thread.Sleep ( 2000 );<br \/>\nMessageBox.Show (&#8220;\u4f60\u770b\u5230\u7ed3\u679c\u4e86\u5417\uff1f&#8221;);<br \/>\n}<br \/>\nelse<br \/>\n{<br \/>\nMessageBox.Show (&#8220;\u6ca1\u6709\u542f\u52a8 [\u8ba1\u7b97\u5668]&#8221;);<br \/>\n}<br \/>\n}<\/p><\/blockquote>\n<p>\u4e09\u3001\u6700\u540e\u518d\u6765\u4e00\u4e2a\uff0c\u67e5\u627e\u5916\u90e8\u7a0b\u5e8f\u7684\u63a7\u4ef6<\/p>\n<blockquote><p>\u627e\u5230\u7a0b\u5e8f\u7684\u7a97\u53e3\u53e5\u67c4\u540e\uff0c\u53d1\u4e2a\u6d88\u606f\uff0c \u8fd9\u4e2a\u8d34\u5b50\u7684IE\u7a97\u53e3\u522b\u5173\uff0c\u628a\u4e0b\u9762\u7684\u4ee3\u7801\u590d\u5236\u5230\u7a97\u4f53\u4ee3\u7801\u4e2d\u70b9\u4e00\u4e0bCommand1\u5c31\u53ef\u4ee5\u4e86\u3002<br \/>\nOption\u00a0\u00a0 Explicit<br \/>\nPrivate\u00a0\u00a0 Declare\u00a0\u00a0 Function\u00a0\u00a0 SendMessage\u00a0\u00a0 Lib\u00a0\u00a0 &#8220;user32&#8221;\u00a0\u00a0 Alias\u00a0\u00a0 &#8220;SendMessageA&#8221;\u00a0\u00a0 (ByVal\u00a0 hwnd\u00a0\u00a0 As\u00a0\u00a0 Long,\u00a0\u00a0 ByVal\u00a0\u00a0 wMsg\u00a0\u00a0 As\u00a0\u00a0 Long,\u00a0\u00a0 ByVal\u00a0\u00a0 wParam\u00a0\u00a0 As\u00a0\u00a0 Long,\u00a0\u00a0 lParam\u00a0\u00a0 As\u00a0\u00a0 Any)\u00a0 As\u00a0\u00a0 Long<br \/>\nPrivate\u00a0\u00a0 Declare\u00a0\u00a0 Function\u00a0\u00a0 FindWindow\u00a0\u00a0 Lib\u00a0\u00a0 &#8220;user32&#8221;\u00a0\u00a0 Alias\u00a0\u00a0 &#8220;FindWindowA&#8221;\u00a0\u00a0 (ByVal\u00a0 lpClassName\u00a0\u00a0 As\u00a0\u00a0 String,\u00a0\u00a0 ByVal\u00a0\u00a0 lpWindowName\u00a0\u00a0 As\u00a0\u00a0 String)\u00a0\u00a0 As\u00a0\u00a0 Long<br \/>\nPrivate\u00a0\u00a0 Declare\u00a0\u00a0 Function\u00a0\u00a0 FindWindowEx\u00a0\u00a0 Lib\u00a0\u00a0 &#8220;user32&#8221;\u00a0\u00a0 Alias\u00a0\u00a0 &#8220;FindWindowExA&#8221;\u00a0\u00a0 (ByVal\u00a0\u00a0 hWnd1\u00a0\u00a0 As\u00a0\u00a0 Long,\u00a0\u00a0 ByVal\u00a0\u00a0 hWnd2\u00a0\u00a0 As\u00a0\u00a0 Long,\u00a0\u00a0 ByVal\u00a0\u00a0 lpsz1\u00a0\u00a0 As\u00a0\u00a0 String,\u00a0\u00a0 ByVal\u00a0\u00a0 lpsz2\u00a0 As\u00a0\u00a0 String)\u00a0\u00a0 As\u00a0\u00a0 Long<br \/>\nPrivate\u00a0\u00a0 Const\u00a0\u00a0 WM_GETTEXT\u00a0\u00a0 =\u00a0\u00a0 &amp;HD<br \/>\nPrivate\u00a0\u00a0 Sub\u00a0\u00a0 Command1_Click()<br \/>\nDim\u00a0\u00a0 Handle\u00a0\u00a0 As\u00a0\u00a0 Long<br \/>\nDim\u00a0\u00a0 wndText\u00a0\u00a0 As\u00a0\u00a0 String<br \/>\nwndText\u00a0\u00a0 =\u00a0\u00a0 Space(255)<br \/>\nHandle\u00a0\u00a0 =\u00a0\u00a0 FindWindow(&#8220;IEFrame&#8221;,\u00a0\u00a0 &#8220;\u8bf7\u95ee\u5982\u4f55\u83b7\u5f97\u4e00\u4e2a\u5916\u90e8\u7a0b\u5e8f\u7a97\u53e3\u4e2d\u6587\u672c\u6846\u7684\u5185\u5bb9\uff1f\u00a0\u00a0 &#8211;\u00a0 Microsoft\u00a0\u00a0 Internet\u00a0\u00a0 Explorer&#8221;)<br \/>\nHandle\u00a0\u00a0 =\u00a0\u00a0 FindWindowEx(Handle,\u00a0\u00a0 0&amp;,\u00a0\u00a0 &#8220;WorkerW&#8221;,\u00a0\u00a0 vbNullString)<br \/>\nHandle\u00a0\u00a0 =\u00a0\u00a0 FindWindowEx(Handle,\u00a0\u00a0 0&amp;,\u00a0\u00a0 &#8220;ReBarWindow32&#8221;,\u00a0\u00a0 vbNullString)<br \/>\nHandle\u00a0\u00a0 =\u00a0\u00a0 FindWindowEx(Handle,\u00a0\u00a0 0&amp;,\u00a0\u00a0 &#8220;ComboBoxEx32&#8221;,\u00a0\u00a0 vbNullString)<br \/>\nHandle\u00a0\u00a0 =\u00a0\u00a0 FindWindowEx(Handle,\u00a0\u00a0 0&amp;,\u00a0\u00a0 &#8220;ComboBox&#8221;,\u00a0\u00a0 vbNullString)\u00a0\u00a0 &#8216;\u00a0\u00a0 \u83b7\u5f97\u7a97\u53e3\u53e5\u67c4<br \/>\nHandle\u00a0\u00a0 =\u00a0\u00a0 FindWindowEx(Handle,\u00a0\u00a0 0&amp;,\u00a0\u00a0 &#8220;Edit&#8221;,\u00a0\u00a0 vbNullString)<br \/>\nSendMessage\u00a0\u00a0 Handle,\u00a0\u00a0 WM_GETTEXT,\u00a0\u00a0 256,\u00a0\u00a0 ByVal\u00a0\u00a0 wndText<br \/>\nwndText\u00a0\u00a0 =\u00a0\u00a0 Left(wndText,\u00a0\u00a0 InStr(wndText,\u00a0\u00a0 Chr(0)))<br \/>\nDebug.Print\u00a0\u00a0 wndText<br \/>\nEnd\u00a0\u00a0 Sub<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u672c\u4f8b\u5b50\uff0c\u7528 C# \u7684\u4e00\u4e2aWinform\u7a0b\u5e8f\uff0c\u521b\u5efa\u4e00\u4e2a\u6307\u5b9a\u8def\u5f84\u7684\u6587\u672c\u6587\u4ef6\uff0c\u5199\u5165\u67d0\u4e9b\u5185\u5bb9\u540e\uff0c\u5e76\u4fdd\u5b58\u3002 \u4e3a\u4e86\u770b\u6e05 &hellip; <a href=\"https:\/\/www.pdbeta.com\/wpblog\/?p=1254\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">\u3010\u8f6c\u8f7d\u3011.net\u4e0e\u5176\u4ed6\u7a0b\u5e8f\u4ea4\u4e92\uff08API)<\/span><\/a><\/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":"https:\/\/www.pdbeta.com\/wpblog\/index.php?rest_route=\/wp\/v2\/posts\/1254"}],"collection":[{"href":"https:\/\/www.pdbeta.com\/wpblog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.pdbeta.com\/wpblog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.pdbeta.com\/wpblog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pdbeta.com\/wpblog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1254"}],"version-history":[{"count":5,"href":"https:\/\/www.pdbeta.com\/wpblog\/index.php?rest_route=\/wp\/v2\/posts\/1254\/revisions"}],"predecessor-version":[{"id":2270,"href":"https:\/\/www.pdbeta.com\/wpblog\/index.php?rest_route=\/wp\/v2\/posts\/1254\/revisions\/2270"}],"wp:attachment":[{"href":"https:\/\/www.pdbeta.com\/wpblog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1254"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.pdbeta.com\/wpblog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1254"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.pdbeta.com\/wpblog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1254"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}