{"id":159,"date":"2012-02-02T18:24:06","date_gmt":"2012-02-02T10:24:06","guid":{"rendered":"http:\/\/www.withonly.com\/?p=159"},"modified":"2018-08-13T10:27:29","modified_gmt":"2018-08-13T02:27:29","slug":"cpu%e6%b8%a9%e5%ba%a6%e5%b0%8f%e5%b7%a5%e5%85%b7%e4%b9%8b%e6%95%b0%e6%8d%ae%e5%91%88%e7%8e%b0","status":"publish","type":"post","link":"https:\/\/blog.5danyuan.com\/?p=159","title":{"rendered":"CPU\u6e29\u5ea6\u5c0f\u5de5\u5177\u4e4b\u6570\u636e\u5448\u73b0"},"content":{"rendered":"<p>\u5728\u4e0a\u4e00\u7bc7\u6587\u5b57\u91cc\uff0c\u5df2\u7ecf\u5199\u4e86\u5982\u4f55\u83b7\u53d6\u6570\u636e\u3002\u5176\u5b9e\uff0c\u5728\u519c\u5386\u65b0\u5e74\u524d\uff0c\u90a3\u90e8\u5206\u4ee3\u7801\u8863\u670d\u81ea\u5df1\u6d17\u5c31\u5df2\u7ecf\u5199\u597d\u4e86\uff0c\u4e22\u5728\u90a3\u91cc\u4e86\u3002<\/p>\n<p>\u6211\u4eca\u5929\u8981\u8bf4\u7684\uff0c\u5728\u4efb\u52a1\u680f\u663e\u793a\u6570\u636e\uff0c\u53ef\u4e0d\u662f\u5728\u7cfb\u7edf\u6258\u76d8\u533a\u57df\u663e\u793a\u56fe\u6807\uff0c\u90a3\u4e2a\u5728 NET \u91cc\u7528 NotifyIcon \u63a7\u4ef6\u5c31\u53ef\u4ee5\u505a\u5230\u7684\u3002\u6309\u60ef\u4f8b\uff0c\u5148\u4e0a\u56fe\u3002<\/p>\n<p><a href=\"\/wp-content\/uploads\/2012\/02\/\u4efb\u52a1\u680f\u5e95\u90e8.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-160\" title=\"\u4efb\u52a1\u680f\u5e95\u90e8\" src=\"\/wp-content\/uploads\/2012\/02\/\u4efb\u52a1\u680f\u5e95\u90e8.png\" alt=\"\" width=\"192\" height=\"40\" \/><\/a><\/p>\n<p style=\"text-align: center;\">\u00a0\u8fd9\u4e2a\u662f\u4efb\u52a1\u680f\u5728\u5e95\u90e8\u7684\u65f6\u5019<\/p>\n<p><a href=\"\/wp-content\/uploads\/2012\/02\/\u4efb\u52a1\u680f\u53f3\u90e8.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-161\" title=\"\u4efb\u52a1\u680f\u53f3\u90e8\" src=\"\/wp-content\/uploads\/2012\/02\/\u4efb\u52a1\u680f\u53f3\u90e8.png\" alt=\"\" width=\"61\" height=\"100\" \/><\/a><\/p>\n<p style=\"text-align: center;\">\u8fd9\u4e2a\u662f\u4efb\u52a1\u680f\u5728\u53f3\u90e8\u7684\u65f6\u5019<\/p>\n<p style=\"text-align: left;\">\u81f3\u4e8e\u4efb\u52a1\u680f\u5728\u5de6\u8fb9\u548c\u5728\u4e0a\u8fb9\uff0c\u539f\u7406\u90fd\u662f\u4e00\u6837\u7684\u3002<\/p>\n<p style=\"text-align: left;\">\u00a0\u6211\u5728\u7f51\u4e0a\u4e5f\u786e\u5b9e\u641c\u7d22\u4e86\u4e00\u628a\uff0c\u5173\u4e8e NET \u6765\u5b9e\u73b0\u8fd9\u6837\u6548\u679c\u7684\u8d44\u6599\u597d\u5c11\uff0c\u5e74\u5c11\u4e0d\u5b66 C++\uff0c\u8001\u6765\u5c31\u7a7a\u53f9\u6c14\u3002<\/p>\n<p style=\"text-align: left;\">\u518d\u8bf4\u539f\u7406\u524d\uff0c\u63d0\u4e00\u54c8\u53e5\u67c4\u8fd9\u4e2a\u6982\u5ff5\uff0c\u5728 C++ \u91cc\uff0c\u51e0\u4e4e\u6240\u6709\u7684\u754c\u9762\u3001\u63a7\u4ef6\u90fd\u6709\u53e5\u67c4\u3002\u4f8b\u5982\u4e00\u4e2a\u7a97\u4f53\uff0c\u4e00\u4e2a\u6309\u94ae\uff0c\u684c\u9762\uff0c\u548c\u4eca\u5929\u7684\u4e3b\u4f53\u201c\u4efb\u52a1\u680f\u201d\u90fd\u6709\u53e5\u67c4\u3002\u8fd9\u91cc\u8bf4\u7684\u662f\u51e0\u4e4e\uff0c\u56e0\u4e3a\u8fd8\u6709\u7528 DirectUI \u5b9e\u73b0\u7684\u754c\u9762\u662f\u6ca1\u6709\u53e5\u67c4\u7684\uff0c\u4f8b\u5982 QQ\u3001MSN\u3001\u767e\u5ea6Hi\u3001YY \u7b49\uff0cDirectUI \uff0c\u8fd9\u662f\u9898\u5916\u8bdd\u4e86\u3002<\/p>\n<p style=\"text-align: left;\">\u6211\u4eec\u5bf9\u53e5\u67c4\u64cd\u4f5c\u5b9e\u9645\u4e0a\u5c31\u662f\u5bf9\u63a7\u4ef6\u6216\u8005\u8bf4\u6240\u4ee3\u8868\u7684\u5185\u5b58\u8fdb\u884c\u64cd\u4f5c\u3002\u4ece\u5e7f\u6cdb\u610f\u4e49\u4e0a\u6765\u8bf4\uff0cvar obj=document.createElement(&#8220;div&#8221;); \u8fd9\u91cc\u7684 obj \u53d8\u91cf\u5c31\u53ef\u4ee5\u7406\u89e3\u4e3a\u4e00\u4e2a\u53e5\u67c4\u3002<\/p>\n<p style=\"text-align: left;\">\u5b9e\u73b0\u539f\u7406\u662f\u9171\u7d2b\u7684\uff0c\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u63a7\u4ef6\u6bd4\u5982\u8bf4 Label \uff0c\u7136\u540e\u8bbe\u7f6e\u5b83\u7684\u7236\u5bb9\u5668\u4e3a\u4efb\u52a1\u680f\uff0c\u6700\u540e\u518d\u8bbe\u7f6e\u5b83\u7684\u4f4d\u7f6e\u3002\u5c31\u53ef\u4ee5\u8fbe\u5230\u8981\u6c42\u4e86\u3002<\/p>\n<p style=\"text-align: left;\">\u4efb\u52a1\u680f\u4e3b\u8981\u6709\u4e24\u5757\uff0c\u4e00\u662f\u663e\u793a\u5f53\u524d\u8fd0\u884c\u7684\u7a0b\u5e8f\uff0c\u6211\u8fd9\u91cc\u81ea\u5df1\u53d6\u540d\u5b57\u53eb\u5de5\u5177\u6761\uff0c\u4e8c\u662f\u663e\u793a\u7a0b\u5e8f\u7684\u6258\u76d8\u56fe\u6807\uff0c\u6211\u53d6\u540d\u5b57\u53eb\u6258\u76d8\u680f\u3002<\/p>\n<p style=\"text-align: left;\">\u7b2c\u4e00\u6b65\uff0c\u5f15\u5165\u76f8\u5173\u7684 C++ API\u3002\u4ec0\u4e48 FindWindow \u3001GetWindowRect \u4ec0\u4e48\u7684\u3002<\/p>\n<p>[DllImport(&#8220;user32.dll&#8221;, EntryPoint = &#8220;FindWindowEx&#8221;, SetLastError = true)]<br \/>\nstatic extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);<\/p>\n<p>[DllImport(&#8220;user32.dll&#8221;)]<br \/>\nstatic extern IntPtr FindWindow(string lpClassName, string lpWindowName);<\/p>\n<p>[DllImport(&#8220;user32.dll&#8221;)]<br \/>\nstatic extern int GetWindowRect(IntPtr hwnd, out Rect lpRect);<\/p>\n<p>[DllImport(&#8220;user32.dll&#8221;, EntryPoint = &#8220;MoveWindow&#8221;)]<br \/>\nstatic extern int MoveWindow(IntPtr hwnd, int x, int y, int nWidth, int nHeight, int bRepaint=1);<\/p>\n<p>[DllImport(&#8220;user32.dll&#8221;)]<br \/>\nstatic extern int SetParent(IntPtr wnd, IntPtr newParentWnd);<\/p>\n<p style=\"text-align: left;\">\u7b2c\u4e8c\u6b65\uff0c\u62ff\u5230\u6211\u4eec\u9700\u8981\u64cd\u4f5c\u7684\u5bf9\u8c61\u7684\u53e5\u67c4\u3002<\/p>\n<p>IntPtr trayHwnd = FindWindow(&#8220;Shell_TrayWnd&#8221;, null); \/\/\u83b7\u53d6\u4efb\u52a1\u680f\u6574\u4f53\u53e5\u67c4<br \/>\nIntPtr notifyHwnd = FindWindowEx(trayHwnd, IntPtr.Zero, &#8220;TrayNotifyWnd&#8221;, null);\/\/\u83b7\u53d6\u4efb\u52a1\u680f\u6258\u76d8\u533a\u57df\u53e5\u67c4<br \/>\nIntPtr toolbarHwnd = FindWindowEx(trayHwnd, IntPtr.Zero, &#8220;ReBarWindow32&#8221;, null);\/\/\u83b7\u53d6\u4efb\u52a1\u680f\u5de5\u5177\u6761\u533a\u57df\u53e5\u67c4<br \/>\nIntPtr imeHwnd = FindWindowEx(toolbarHwnd, IntPtr.Zero, &#8220;CiceroUIWndFrame&#8221;, null);\/\/\u83b7\u53d6\u8bed\u8a00\u680f\u533a\u57df\u53e5\u67c4<\/p>\n<p>\u7b2c\u4e09\u6b65\uff0c\u5f00\u59cb\u83b7\u53d6\u76f8\u5173\u5c3a\u5bf8\u3002<\/p>\n<p>Rect box = new Rect();<br \/>\nGetWindowRect(toolbarHwnd, out box);<\/p>\n<p>\/\/\u83b7\u53d6\u4efb\u52a1\u680f\u5c3a\u5bf8<br \/>\nint boxWidth = box.Right &#8211; box.Left;<br \/>\nint boxHeight = box.Bottom &#8211; box.Top;<br \/>\n\/\/\u5224\u65ad\u4efb\u52a1\u680f\u662f\u6c34\u5e73\u8fd8\u662f\u5782\u76f4\u3002true \u4e3a\u6c34\u5e73\uff0cfalse \u4e3a\u5782\u76f4<br \/>\nbool position = boxWidth &gt; boxHeight ? true : false;<\/p>\n<p>Rect ime = new Rect();<br \/>\nint d = GetWindowRect(imeHwnd, out ime);<\/p>\n<p>\/\/\u83b7\u53d6\u8bed\u8a00\u680f\u5c3a\u5bf8<br \/>\nint imeWidth = 0;<br \/>\nint imeHeight = 0;<br \/>\nif (d != 0)<br \/>\n{<br \/>\nimeWidth = ime.Right &#8211; ime.Left;<br \/>\nimeHeight = ime.Bottom &#8211; ime.Top;<br \/>\n}<\/p>\n<p>frmHwnd = FindWindow(null, &#8220;SysMonitor&#8221;); \/\/\u67e5\u627e\u7a0b\u5e8f\u7a97\u4f53<\/p>\n<p>\u7b2c\u56db\u6b65\uff0c\u5f00\u59cb\u8ba1\u7b97\u76ee\u7684\u5750\u6807\u3002<\/p>\n<p>if (frmHwnd != IntPtr.Zero)<br \/>\n{<br \/>\nint x1, y1; \/\/\u7a97\u4f53\u76ee\u6807\u5750\u6807<br \/>\nint x2, y2;\/\/\u8bed\u8a00\u680f\u76ee\u6807\u5750\u6807<br \/>\nif (position)<br \/>\n{<br \/>\nx1 = boxWidth &#8211; w;<br \/>\ny1 = (boxHeight &#8211; h) \/ 2;<\/p>\n<p>x2 = x1 &#8211; imeWidth-10; \/\/\u8fd9\u91cc\u591a\u51cf\u7684 10 \u662f\u4e3a\u4e86\u589e\u52a0\u95f4\u9699\uff0c\u6f02\u4eae\u70b9<br \/>\ny2 = 0;<br \/>\n}<br \/>\nelse<br \/>\n{<br \/>\nint diff = 2; \/\/\u5782\u76f4\u65b9\u5411\uff0c\u6263\u51cf\u5bbd\u5ea6\u66f4\u7f8e\u89c2<br \/>\n\/\/\u5982\u679c\u4e3a\u5782\u76f4\u65b9\u5411\uff0c\u4fee\u6539\u5bbd\u5ea6\u4ee5\u9002\u5e94\u4efb\u52a1\u680f\u5bbd\u5ea6<br \/>\nw = boxWidth &#8211; diff;<\/p>\n<p>x1 = box.Left==0?0:diff;<br \/>\ny1 = boxHeight &#8211; h;<\/p>\n<p>x2 = x1;<br \/>\ny2 = boxHeight &#8211; imeHeight &#8211; h;<br \/>\n}<\/p>\n<p>this.Width = w;<br \/>\nthis.Height = h;<br \/>\nlblInfo.Width = w;<br \/>\nlblInfo.Height = h;<\/p>\n<p>int c = SetParent(frmHwnd, toolbarHwnd);\/\/\u8bbe\u7f6e\u4efb\u52a1\u680f\u4e3a\u7a97\u4f53\u7684\u7236\u7a97\u4f53<\/p>\n<p>\u7b2c\u4e94\u6b65\uff0c\u79fb\u52a8 CPU \u6e29\u5ea6\u7a97\u4f53\u548c\u8bed\u8a00\u680f\u5230\u76ee\u7684\u4f4d\u7f6e\u3002<br \/>\nMoveWindow(frmHwnd, x1, y1, w, h);\/\/\u79fb\u52a8\u663e\u793aCPU\u6e29\u5ea6\u7684\u7a97\u4f53<br \/>\nMoveWindow(imeHwnd, x2, y2, imeWidth, imeHeight);\/\/\u79fb\u52a8\u8bed\u8a00\u680f\u7a97\u4f53<\/p>\n<p>\u5199\u5230\u8fd9\u91cc\uff0c\u6574\u4f53\u7ed3\u6784\u5c31\u7b97\u5b8c\u4e86\u3002\u4e0d\u8981\u5fd8\u8bb0\u4e86\u6dfb\u52a0\u4e00\u4e2a Timer \u5594\uff0c\u5b9a\u65f6\u83b7\u53d6\u6700\u65b0\u7684 CPU \u6e29\u5ea6\u5e76\u663e\u793a\u51fa\u6765\u3002<\/p>\n<p>\u6700\u540e\u751f\u6210\u7684 exe \u6211\u5c31\u4e0d\u653e\u4e0a\u6765\u4e86\u3002\u5982\u679c\u5927\u5bb6\u6709\u9700\u8981\u7684\u8bdd\uff0c\u7559\u8a00\u8863\u670d\u81ea\u5df1\u6d17\u4f1a\u53d1\u7ed9\u4f60\u4eec\u3002\u4ee3\u7801\u4f18\u5316\u7684\u4e8b\u60c5\u5c31\u4ea4\u7ed9\u4f60\u4eec\u53bb\u505a\u62c9\u3002<\/p>\n<p>CPU \u6e29\u5ea6\u5de5\u5177\u5230\u8fd9\u91cc\u5c31\u7b97\u5b8c\u62c9\u3002\u5269\u4e0b\u7684\u4e00\u4e2a\u672a\u5b8c\u6210\u7684\u662f\u5728\u76f4\u63a5\u5728\u684c\u9762\u4e0a\u7ed8\u5236\u5185\u5bb9\uff0c\u76f8\u4fe1\u6211\uff0c\u8fd9\u5bf9\u6211\u6765\u8bf4\u5f88\u6709\u7528\u7684\u3002<\/p>\n<p>\u6700\u540e\u7684\u6700\u540e\uff0c\u6211\u90a3\u4e9b\u4eb2\u7231\u7684\u670b\u53cb\u4eec\uff0c\u6211\u60f3\u4f60\u4eec\u4e86\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u4e0a\u4e00\u7bc7\u6587\u5b57\u91cc\uff0c\u5df2\u7ecf\u5199\u4e86\u5982\u4f55\u83b7\u53d6\u6570\u636e\u3002\u5176\u5b9e\uff0c\u5728\u519c\u5386\u65b0\u5e74\u524d\uff0c\u90a3\u90e8\u5206\u4ee3\u7801\u8863\u670d\u81ea\u5df1\u6d17\u5c31\u5df2\u7ecf\u5199\u597d\u4e86\uff0c\u4e22\u5728\u90a3\u91cc\u4e86\u3002 \u6211\u4eca [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[50,53,51],"class_list":["post-159","post","type-post","status-publish","format-standard","hentry","category-daily","tag-cpu","tag-53","tag-51"],"_links":{"self":[{"href":"https:\/\/blog.5danyuan.com\/index.php?rest_route=\/wp\/v2\/posts\/159","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.5danyuan.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.5danyuan.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.5danyuan.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.5danyuan.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=159"}],"version-history":[{"count":0,"href":"https:\/\/blog.5danyuan.com\/index.php?rest_route=\/wp\/v2\/posts\/159\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.5danyuan.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=159"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.5danyuan.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=159"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.5danyuan.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=159"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}