{"id":410,"date":"2015-04-25T10:05:08","date_gmt":"2015-04-25T02:05:08","guid":{"rendered":"http:\/\/yangwang.hk\/?p=410"},"modified":"2015-04-25T16:33:11","modified_gmt":"2015-04-25T08:33:11","slug":"%e6%8e%a7%e5%88%b6%e5%8f%b0%e7%aa%97%e5%8f%a3%e7%9a%84%e7%bb%98%e5%9b%be","status":"publish","type":"post","link":"https:\/\/yangwang.hk\/?p=410","title":{"rendered":"\u63a7\u5236\u53f0\u7a97\u53e3\u7684\u7ed8\u56fe"},"content":{"rendered":"<p>\u6700\u8fd1\u548c\u522b\u4eba\u804a\u5929\uff0c\u804a\u5230\u4e86\u63a7\u5236\u53f0\u7ed8\u56fe\uff0c\u66fe\u7ecf\u5b66\u4e86\u4e00\u4e9bwindows\u7684API\u7f16\u7a0b\uff0c\u5374\u6ca1\u60f3\u5230\u76f4\u63a5\u5728\u63a7\u5236\u53f0\u7a97\u53e3\u4e0a\u8fdb\u884c\u7ed8\u56fe<\/p>\n<p>\u4eca\u5929\u6709\u65f6\u95f4\uff0c\u4fbf\u628a\u4ee3\u7801\u5199\u4e86\u51fa\u6765<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n#include &lt;Windows.h&gt;\r\n#include &lt;stdio.h&gt;\r\n\r\n\/************************************************************************\/\r\n\/* \u51fd\u6570 DrawCircle\r\n\/* \u53c2\u6570 HDC hdc \u7ed8\u56fe\u7684\u53e5\u67c4\r\n\/*      int x\r\n\/*\t\tint y\t\u5706\u5fc3\u7684x\uff0cy\u5750\u6807\r\n\/*\t\tint r\t\u5706\u7684\u534a\u5f84\r\n\/*\t\tCOLORREF color\t\t\u5706\u7684\u586b\u5145\u989c\u8272\r\n\/* \u529f\u80fd \u5728(x, y)\u5904\u753b\u4e00\u4e2a\u534a\u5f84\u4e3ar\u7684\u5706\uff0c\u7528color\u7684\u989c\u8272\u586b\u5145\r\n\/************************************************************************\/\r\nvoid DrawCircle(HDC hdc, int x, int y, int r, COLORREF color)\r\n{\r\n\tHBRUSH brush = (HBRUSH)CreateSolidBrush (color);\t\t\/\/\u65b0\u5efa\u4e00\u4e2a\u753b\u5237\r\n\tSelectObject(hdc, (HGDIOBJ)brush);\t\t\t\t\t\t\/\/\u9009\u62e9\u753b\u5237\r\n\tEllipse(hdc, x-r, y-r, x+r, y+r);\t\t\t\t\t\t\/\/\u753b\u4e00\u4e2a\u5706\r\n\tDeleteObject(brush);\t\t\t\t\t\t\t\t\t\/\/\u5220\u9664\u753b\u5237\r\n}\r\n\r\nint main()\r\n{\r\n\tHWND console = GetConsoleWindow();\t\t\t\t\t\t\/\/\u83b7\u53d6\u63a7\u5236\u53f0\u7a97\u53e3\u53e5\u67c4\r\n\tHDC  console_hdc = GetDC(console);\t\t\t\t\t\t\/\/\u83b7\u53d6\u7ed8\u56fedc\r\n\tRECT rect;\t\t\t\t\t\t\t\t\t\t\t\t\/\/\u4fdd\u5b58\u7ed8\u56fe\u533a\u57df\u5927\u5c0f\u7684\u7ed3\u6784\u4f53\r\n\tint x,\r\n\t\ty,\t\t\t\t\t\t\t\/\/\u5c0f\u7403\u7684xy\u5750\u6807\r\n\t\tdx = 3,\t\t\t\t\t\t\/\/x\u7684\u589e\u91cf\r\n\t\tdy = 3;\t\t\t\t\t\t\/\/y\u7684\u589e\u91cf\r\n\tint r = 0, g = 0, b = 0;\t\t\/\/\u5c0f\u7403\u989c\u8272\r\n\tint speed;\t\t\t\t\t\t\/\/\u5c0f\u7403\u901f\u5ea6\r\n\tCOORD pos={0,4};\t\t\t\t\/\/\u5149\u6807\u4f4d\u7f6e\r\n\r\n\tHANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);\r\n\r\n\tGetClientRect(console, &amp;rect);\t\t\t\t\t\t\t\/\/\u9996\u5148\u83b7\u53d6\u7ed8\u56fe\u533a\u57df\u7684\u5927\u5c0f\r\n\r\n\tx = 10 + rand() % (rect.right);\r\n\ty = 10 + rand() % (rect.bottom);\r\n\r\n\tprintf(&quot;\u5f53\u524d\u7a97\u53e3\u5bbd\u5ea6 %d\\n\u5f53\u524d\u7a97\u53e3\u9ad8\u5ea6 %d\\n\u8bf7\u8f93\u5165\u5c0f\u7403\u989c\u8272(r g b):&quot;, rect.right, rect.bottom);\r\n\tscanf(&quot;%d %d %d&quot;, &amp;r, &amp;g, &amp;b);\r\n\tprintf(&quot;\u8bf7\u8f93\u5165\u5c0f\u7403\u8fd0\u52a8\u901f\u5ea6\uff081~10\uff09&quot;);\r\n\tscanf(&quot;%d&quot;, &amp;speed);\r\n\t\/\/rgb \u7684\u8303\u56f4\u5206\u522b\u662f0~255\r\n\t\/\/255 255 255\u4e3a\u767d\u8272\r\n\r\n\tif (speed &lt;1 || speed &gt; 10 || r &gt;255 || r &lt; 0 || g &gt;255 || g &lt; 0 || b &gt;255 || g &lt;0)\r\n\t{\r\n\t\tprintf(&quot;\u65e0\u6548\u7684\u8f93\u5165&quot;);\r\n\t\treturn 0;\r\n\t}\r\n\r\n\twhile (1)\r\n\t{\r\n\t\tSetConsoleCursorPosition(hOut,pos);\t\t\t\t\t\t\/\/\u8bbe\u7f6e\u5149\u6807\u4f4d\u7f6e\r\n\t\tprintf(&quot;\u5f53\u524d\u5c0f\u7403\u4f4d\u7f6e x= %d  \\ty=%d  &quot;, x, y);\r\n\t\tDrawCircle(console_hdc, x, y, 10, RGB(r, g, b));\t\t\/\/\u753b\u4e00\u4e2a\u767d\u8272\u7684\u5706\r\n\t\tSleep(200\/speed);\r\n\t\tDrawCircle(console_hdc, x, y, 10, RGB(0, 0, 0));\t\t\/\/\u753b\u4e00\u4e2a\u9ed1\u8272\u7684\u5706\uff08\u64e6\u9664\uff09\r\n\r\n\t\tif (x &gt; rect.right - 10 || x &lt; 10)\r\n\t\t\tdx = -dx;\r\n\r\n\t\tif (y &gt; rect.bottom - 10 || y &lt; 10)\r\n\t\t\tdy = -dy;\r\n\r\n\t\tx += dx;\r\n\t\ty += dy;\r\n\t}\r\n\r\n\tReleaseDC(console, console_hdc);\r\n\treturn 0;\r\n}\r\n\r\n<\/pre>\n<p>\u8981\u5b9e\u73b0\u7ed8\u56fe\u5c31\u9700\u8981\u5148\u83b7\u53d6\u63a7\u5236\u53f0\u7a97\u53e3\u7684\u53e5\u67c4<\/p>\n<p>\u53e5\u67c4\u8bf4\u7684\u9ad8\u5927\u4e0a\uff0c\u5176\u5b9e\u5b83\u5c31\u662f\u4e00\u4e2a\u6570\u5b57\u800c\u5df2\uff0c\u5b83\u6807\u8bb0\u4e86\u4e00\u79cd\u8d44\u6e90\uff0c\u6839\u636e\u8fd9\u4e2a\u6570\u5b57\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u627e\u5230\u8fd9\u4e2a\u7a97\u53e3\u3002\u4e0d\u7136\u5728\u7ed8\u56fe\u51fd\u6570\u7ed8\u56fe\u7684\u65f6\u5019\uff0c\u5c31\u53ef\u80fd\u5199\u5230\u522b\u7684\u7a97\u53e3\u4e0a\u4e86<\/p>\n<p>\u83b7\u53d6\u4e86\u63a7\u5236\u53f0\u53e5\u67c4\u4e4b\u540e\uff0c\u6211\u4eec\u9700\u8981\u83b7\u53d6HDC\uff0cHDC\u662f\u8bbe\u5907\u4e0a\u4e0b\u6587\uff0c\u540d\u5b57\u53d8\u5f97\u66f4\u52a0\u7384\u4e4e\u4e86\uff0c\u5176\u5b9e\u5b83\u5c31\u662f\u7528\u6765\u7ed8\u56fe\u7684\u3002<\/p>\n<p>GetStdHandle\u8fd9\u4e2aAPI\u53c8\u662f\u83b7\u53d6\u53e5\u67c4\u7684\uff0c\u4e0d\u8fc7\u8fd9\u4e00\u6b21\u4e0d\u662f\u7a97\u53e3\u4e86\uff0c\u800c\u662f\u6807\u51c6\u8f93\u51fa\uff0c\u56e0\u4e3a\u6211\u4eec\u7a0b\u5e8f\u4e2d\u9700\u8981\u76f4\u63a5\u5b9a\u4f4d\u5149\u6807\u7684\u4f4d\u7f6e\uff0c\u56e0\u6b64\u9700\u8981\u4f7f\u7528\u5230\u6807\u51c6\u8f93\u51fa\u7684\u53e5\u67c4<\/p>\n<p>GetClientRect\u80fd\u83b7\u53d6\u7ed8\u56fe\u7684\u8303\u56f4\uff0c\u77e5\u9053\u4e86\u7ed8\u56fe\u7684\u8303\u56f4\u540e\uff0c\u6211\u4eec\u80fd\u8ba9\u753b\u51fa\u6765\u7684\u5706\u201c\u78b0\u5230\u201d\u8fb9\u6846\u540e\u81ea\u52a8\u7684\u201c\u5f39\u56de\u201d<\/p>\n<p>\u56e0\u4e3aprintf\u4f1a\u5728\u6bcf\u6b21\u753b\u5706\u4e4b\u540e\u8f93\u51fa\u5706\u7684\u5750\u6807\uff0c\u56e0\u6b64\u6211\u4eec\u9700\u8981\u5728\u6bcf\u6b21\u8f93\u51fa\u524d\u5c06\u5149\u6807\u653e\u5230\u6307\u5b9a\u7684\u4f4d\u7f6e<\/p>\n<p>SetConsoleCursorPosition\u4fbf\u53d1\u6325\u4e86\u8fd9\u4e2a\u4f5c\u7528\uff0c\u5b83\u5c06\u5149\u6807\u5b9a\u4f4d\u5230\u7b2c\u4e09\u884c\u7b2c\u4e00\u4e2a\u5b57\u7b26\u7684\u4f4d\u7f6e<\/p>\n<p>DrawCircle\u662f\u6211\u4eec\u81ea\u5df1\u5c01\u88c5\u7684\u4e00\u4e2a\u51fd\u6570\uff0c\u5b83\u7684\u5177\u4f53\u529f\u80fd\u6ce8\u91ca\u4e2d\u5df2\u7ecf\u660e\u786e\u7684\u5199\u4e86\u51fa\u6765<\/p>\n<p>DrawCircle\u4e2d\u6211\u4eec\u5148\u65b0\u5efa\u4e86\u4e00\u4e2a\u753b\u5237\uff0c\u56e0\u4e3a\u6211\u4eec\u9700\u8981\u7528\u6307\u5b9a\u7684\u989c\u8272\u53bb\u753b\u5706<\/p>\n<p>SelectObject\u9009\u62e9\u753b\u5237\uff0c\u8fd9\u6837\u4e0b\u9762\u7684\u4f5c\u56fe\u5c31\u4f1a\u53d8\u6210\u6211\u4eec\u6240\u6307\u5b9a\u7684\u989c\u8272<\/p>\n<p>Ellipse\u662f\u4e00\u4e2a\u753b\u692d\u5706\u7684\u51fd\u6570\uff0c\u5b83\u7684\u540e\u56db\u4e2a\u53c2\u6570\u5206\u522b\u662f\u692d\u5706\u7684\u5916\u754c\u77e9\u5f62\u7684\u5750\u6807\uff0c\u5982\u679c\u6211\u4eec\u4f7f\u77e9\u5f62\u7684\u8fb9\u957f\u76f8\u7b49\uff0c\u90a3\u4e48\u5b83\u753b\u51fa\u6765\u7684\u81ea\u7136\u5c31\u662f\u5706\u5f62\u4e86<\/p>\n<p>\u6700\u540e\u522b\u5fd8\u4e86\u628a\u753b\u5237\u7528DeleteObject\u5220\u9664\uff0c\u56e0\u4e3a\u753b\u5237\u4e5f\u662f\u9700\u8981\u5360\u7528\u8d44\u6e90\u7684<\/p>\n<p>\u4e0b\u9762\u5c31\u662f\u5b9e\u73b0\u5c0f\u7403\u7684\u8fd0\u52a8\u8fc7\u7a0b\u4e86\uff0c\u5176\u5b9e\u5b9e\u73b0\u8d77\u6765\u5f88\u7b80\u5355\u3002\u7531\u4e8e\u201c\u89c6\u89c9\u6682\u7559\u201d\u6548\u5e94\uff0c\u6211\u4eec\u53ea\u9700\u8981\u5148\u753b\u4e00\u4e2a\u5706\uff0c\u518d\u5220\u9664\uff0c\u63a5\u7740\u7acb\u5373\u5728\u4ed6\u7684\u65c1\u8fb9\u753b\u4e00\u4e2a\u5706\uff0c\u4eba\u773c\u4fbf\u4f1a\u5f62\u6210\u4e00\u79cd\u9519\u89c9\uff0c\u5c0f\u7403\u5728\u8fd0\u52a8\uff01<\/p>\n<p>\u90a3\u4e48\u600e\u4e48\u64e6\u9664\u5462\uff0c\u5f88\u7b80\u5355\u5566\uff0c\u5728\u6211\u4eec\u753b\u51fa\u7684\u5706\u4e0a\u518d\u753b\u4e00\u4e2a\u5706\uff0c\u5b83\u7684\u989c\u8272\u548c\u80cc\u666f\u989c\u8272\u76f8\u540c\uff0c\u8fd9\u6837\u4e0d\u5c31\u628a\u521a\u624d\u7684\u5706\u7ed9\u8986\u76d6\u4e86\u5417\uff1f<\/p>\n<p>\u5c0f\u7403\u7684\u8fd0\u52a8\u901f\u5ea6\u7531\u6bcf\u6b21\u753b\u5706\u5230\u64e6\u9664\u4e4b\u524d\u7684\u5ef6\u65f6\u65f6\u95f4\u51b3\u5b9a\uff0c\u6839\u636e\u4f60\u7684\u8f93\u5165\uff0c\u6bcf\u6b21\u5ef6\u65f6\u7684\u503c\u5c06\u5728200ms &#8211; 20ms\u4e4b\u95f4\u53d8\u5316<\/p>\n<p>\u8fd9\u6837\uff0c\u6211\u4eec\u4fbf\u5b9e\u73b0\u4e86\u63a7\u5236\u53f0\u7248\u672c\u7684\u8fd0\u52a8\u7684\u5c0f\u7403\u7a0b\u5e8f\u4e86<\/p>\n<div id=\"attachment_418\" style=\"width: 658px\" class=\"wp-caption alignnone\"><a href=\"http:\/\/yangwang.hk\/wp-content\/uploads\/2015\/04\/1.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-418\" class=\"size-full wp-image-418\" src=\"http:\/\/yangwang.hk\/wp-content\/uploads\/2015\/04\/1.png\" alt=\"\u8fd0\u884c\u6548\u679c\" width=\"648\" height=\"425\" srcset=\"https:\/\/yangwang.hk\/wp-content\/uploads\/2015\/04\/1.png 648w, https:\/\/yangwang.hk\/wp-content\/uploads\/2015\/04\/1-300x196.png 300w\" sizes=\"auto, (max-width: 648px) 100vw, 648px\" \/><\/a><p id=\"caption-attachment-418\" class=\"wp-caption-text\">\u8fd0\u884c\u6548\u679c<\/p><\/div>\n<div id=\"attachment_419\" style=\"width: 490px\" class=\"wp-caption alignnone\"><a href=\"http:\/\/yangwang.hk\/wp-content\/uploads\/2015\/04\/2.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-419\" class=\"size-full wp-image-419\" src=\"http:\/\/yangwang.hk\/wp-content\/uploads\/2015\/04\/2.png\" alt=\"\u8fd0\u884c\u6548\u679c\" width=\"480\" height=\"185\" srcset=\"https:\/\/yangwang.hk\/wp-content\/uploads\/2015\/04\/2.png 480w, https:\/\/yangwang.hk\/wp-content\/uploads\/2015\/04\/2-300x115.png 300w\" sizes=\"auto, (max-width: 480px) 100vw, 480px\" \/><\/a><p id=\"caption-attachment-419\" class=\"wp-caption-text\">\u8fd0\u884c\u6548\u679c<\/p><\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u6700\u8fd1\u548c\u522b\u4eba\u804a\u5929\uff0c\u804a\u5230\u4e86\u63a7\u5236\u53f0\u7ed8\u56fe\uff0c\u66fe\u7ecf\u5b66\u4e86\u4e00\u4e9bwindows\u7684API\u7f16\u7a0b\uff0c\u5374\u6ca1\u60f3\u5230\u76f4\u63a5\u5728\u63a7\u5236\u53f0\u7a97\u53e3\u4e0a\u8fdb\u884c\u7ed8\u56fe \u2026 <span class=\"read-more\"><a href=\"https:\/\/yangwang.hk\/?p=410\">Read More: \u63a7\u5236\u53f0\u7a97\u53e3\u7684\u7ed8\u56fe &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-410","post","type-post","status-publish","format-standard","hentry","category-c"],"views":4676,"_links":{"self":[{"href":"https:\/\/yangwang.hk\/index.php?rest_route=\/wp\/v2\/posts\/410","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/yangwang.hk\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/yangwang.hk\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/yangwang.hk\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/yangwang.hk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=410"}],"version-history":[{"count":6,"href":"https:\/\/yangwang.hk\/index.php?rest_route=\/wp\/v2\/posts\/410\/revisions"}],"predecessor-version":[{"id":420,"href":"https:\/\/yangwang.hk\/index.php?rest_route=\/wp\/v2\/posts\/410\/revisions\/420"}],"wp:attachment":[{"href":"https:\/\/yangwang.hk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=410"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/yangwang.hk\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=410"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/yangwang.hk\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=410"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}