{"id":294453,"date":"2024-05-20T15:10:35","date_gmt":"2024-05-20T07:10:35","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/294453.html"},"modified":"2024-05-20T15:10:53","modified_gmt":"2024-05-20T07:10:53","slug":"c-%e5%a6%82%e4%bd%95%e5%ae%9e%e7%8e%b0%e5%87%a0%e4%bd%95%e6%b5%8b%e9%87%8f%e5%b7%a5%e5%85%b7","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/294453.html","title":{"rendered":"C# \u5982\u4f55\u5b9e\u73b0\u51e0\u4f55\u6d4b\u91cf\u5de5\u5177"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27141455\/db6252c9-d411-4dc3-a0fe-98be3878dfd1.webp\" alt=\"C# \u5982\u4f55\u5b9e\u73b0\u51e0\u4f55\u6d4b\u91cf\u5de5\u5177\" \/><\/p>\n<p><p>\u51e0\u4f55\u6d4b\u91cf\u5de5\u5177\u5728C#\u4e2d\u7684\u5b9e\u73b0\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528<strong>\u56fe\u5f62\u7528\u6237\u754c\u9762\uff08GUI\uff09\u6846\u67b6<\/strong>\u3001<strong>\u7ed8\u56feAPI<\/strong>\u3001\u4ee5\u53ca<strong>\u6570\u5b66\u8fd0\u7b97<\/strong>\u6765\u5b8c\u6210\u3002\u57fa\u672c\u5b9e\u73b0\u65b9\u5f0f\u6d89\u53ca\u7684\u6280\u672f\u70b9\u5305\u62ec\uff1a<strong>\u521b\u5efa\u56fe\u5f62\u754c\u9762\u3001\u6355\u83b7\u7528\u6237\u8f93\u5165\u3001\u56fe\u5f62\u6e32\u67d3<\/strong>\u3001\u4ee5\u53ca<strong>\u51e0\u4f55\u7b97\u6cd5<\/strong>\u7684\u5e94\u7528\u3002\u5728\u8be6\u7ec6\u63cf\u8ff0\u521b\u5efa\u56fe\u5f62\u754c\u9762\u65f6\uff0c\u6211\u4eec\u9700\u8981\u4f7f\u7528\u5982Windows Presentation Foundation\uff08WPF\uff09\u6216Windows Forms\u7b49\u6280\u672f\u6765\u6784\u5efa\u5e94\u7528\u7a0b\u5e8f\u7a97\u53e3\uff0c\u5e76\u8bbe\u8ba1\u7528\u6237\u4ea4\u4e92\u7684\u903b\u8f91\u6765\u6355\u83b7\u6d4b\u91cf\u547d\u4ee4\u548c\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5efa\u7acb\u9879\u76ee\u57fa\u7840<\/h3>\n<\/p>\n<p><p>C#\u4e2d\u6784\u5efa\u4e00\u4e2a\u51e0\u4f55\u6d4b\u91cf\u5de5\u5177\u7684\u7b2c\u4e00\u6b65\u662f\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u9879\u76ee\uff0c\u5e76\u9009\u62e9\u5408\u9002\u7684GUI\u6846\u67b6\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528Windows Forms\u6216WPF\u4f5c\u4e3a\u7528\u6237\u754c\u9762\u5c42\uff0c\u4e24\u8005\u90fd\u652f\u6301\u7ed8\u5236\u81ea\u5b9a\u4e49\u56fe\u5f62\u548c\u5904\u7406\u7528\u6237\u4e8b\u4ef6\u3002<\/p>\n<\/p>\n<p><h4>\u8bbe\u7f6e\u5f00\u53d1\u73af\u5883<\/h4>\n<\/p>\n<p><p>\u5728Visual Studio\u4e2d\u521b\u5efa\u4e00\u4e2a\u65b0\u7684WPF\u6216Windows Forms\u5e94\u7528\u7a0b\u5e8f\u9879\u76ee\u3002\u9009\u62e9.NET\u6846\u67b6\u6216.NET Core\u4f5c\u4e3a\u8fd0\u884c\u65f6\u73af\u5883\u3002<\/p>\n<\/p>\n<p><h4>\u8bbe\u8ba1\u7528\u6237\u754c\u9762<\/h4>\n<\/p>\n<p><p>\u8bbe\u8ba1\u4e00\u4e2a\u7528\u6237\u53cb\u597d\u7684\u754c\u9762\uff0c\u5305\u62ec\u5de5\u5177\u6761\u3001\u83dc\u5355\u6216\u6309\u94ae\u6765\u9009\u62e9\u4e0d\u540c\u7684\u6d4b\u91cf\u5de5\u5177\uff08\u5982\u76f4\u7ebf\u3001\u77e9\u5f62\u3001\u5706\u7b49\uff09\u3002\u4f7f\u7528\u63a7\u4ef6\uff0c\u5982Canvas\u6216PictureBox\uff0c\u4f5c\u4e3a\u7ed8\u56fe\u533a\u57df\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u7528\u6237\u8f93\u5165\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u6bcf\u4e00\u79cd\u51e0\u4f55\u6d4b\u91cf\u5de5\u5177\u9700\u8981\u4e0d\u540c\u7684\u7528\u6237\u8f93\u5165\u3002<strong>\u7f16\u5199\u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f\u6765\u6355\u83b7\u7528\u6237\u52a8\u4f5c<\/strong>\uff0c\u5982\u70b9\u51fb\u3001\u62d6\u52a8\u548c\u677e\u5f00\u9f20\u6807\uff0c\u4ee5\u786e\u5b9a\u56fe\u5f62\u7684\u8d77\u59cb\u70b9\u548c\u7ec8\u70b9\u3002<\/p>\n<\/p>\n<p><h4>\u6355\u83b7\u548c\u54cd\u5e94\u4e8b\u4ef6<\/h4>\n<\/p>\n<p><p>\u4e3a\u7528\u6237\u754c\u9762\u4e2d\u7684\u63a7\u4ef6\u6dfb\u52a0\u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f\uff0c\u4f8b\u5982MouseDown\u3001MouseMove\u548cMouseUp\u4e8b\u4ef6\u3002\u5728\u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f\u4e2d\u83b7\u53d6\u9f20\u6807\u7684\u4f4d\u7f6e\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><h4>\u786e\u5b9a\u5750\u6807\u548c\u53c2\u6570<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7\u7528\u6237\u7684\u9f20\u6807\u52a8\u4f5c\u83b7\u5f97\u56fe\u5f62\u7684\u57fa\u672c\u53c2\u6570\uff0c\u5982\u957f\u5ea6\u3001\u5bbd\u5ea6\u6216\u534a\u5f84\u3002\u4fdd\u5b58\u8fd9\u4e9b\u53c2\u6570\uff0c\u4ee5\u4fbf\u540e\u7eed\u7684\u51e0\u4f55\u8fd0\u7b97\u548c\u56fe\u5f62\u6e32\u67d3\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u56fe\u5f62\u6e32\u67d3<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528C#\u5185\u7f6e\u7684\u7ed8\u56feAPI\uff0c\u5982System.Drawing\u6216WPF\u7684DrawingContext\uff0c\u6765\u5728\u754c\u9762\u4e0a\u7ed8\u5236\u7528\u6237\u5b9a\u4e49\u7684\u56fe\u5f62\u3002<\/p>\n<\/p>\n<p><h4>\u7ed8\u5236\u57fa\u672c\u56fe\u5f62<\/h4>\n<\/p>\n<p><p>\u5f00\u53d1\u7ed8\u5236\u76f4\u7ebf\u3001\u77e9\u5f62\u3001\u5706\u7b49\u57fa\u672c\u56fe\u5f62\u7684\u51fd\u6570\u3002\u4f7f\u7528Graphics\u5bf9\u8c61\u6216DrawingContext\u6765\u64cd\u4f5c\u7ed8\u56fe\u52a8\u4f5c\u3002<\/p>\n<\/p>\n<p><h4>\u5b9e\u65f6\u53cd\u9988<\/h4>\n<\/p>\n<p><p>\u5728\u7528\u6237\u8fdb\u884c\u6d4b\u91cf\u52a8\u4f5c\u65f6\uff0c\u5373\u65f6\u7ed8\u5236\u56fe\u5f62\uff0c\u63d0\u4f9b\u5b9e\u65f6\u53cd\u9988\u3002\u8fd9\u8981\u6c42\u5728MouseMove\u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f\u4e2d\u5b9e\u73b0\u4e34\u65f6\u7ed8\u56fe\u903b\u8f91\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u51e0\u4f55\u7b97\u6cd5\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u6bcf\u79cd\u51e0\u4f55\u6d4b\u91cf\u5de5\u5177\uff0c\u5b9e\u73b0\u6240\u9700\u7684\u8ba1\u7b97\u903b\u8f91\u4ee5\u63d0\u4f9b\u51c6\u786e\u7684\u6d4b\u91cf\u7ed3\u679c\uff0c\u5982\u957f\u5ea6\u3001\u9762\u79ef\u548c\u5468\u957f\u7b49\u3002<\/p>\n<\/p>\n<p><h4>\u8ddd\u79bb\u548c\u957f\u5ea6\u8ba1\u7b97<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u7ebf\u6bb5\uff0c\u5b9e\u73b0\u4e24\u70b9\u95f4\u7684\u8ddd\u79bb\u8ba1\u7b97\u3002\u91c7\u7528\u52fe\u80a1\u5b9a\u7406\u6765\u8ba1\u7b97\u76f4\u7ebf\u957f\u5ea6\uff1a\u957f\u5ea6 =\u221a[(x2-x1)\u00b2 + (y2-y1)\u00b2]\u3002<\/p>\n<\/p>\n<p><h4>\u9762\u79ef\u548c\u5468\u957f\u8ba1\u7b97<\/h4>\n<\/p>\n<p><p>\u4e3a\u77e9\u5f62\u548c\u5706\u5f62\u5de5\u5177\u5b9e\u73b0\u9762\u79ef\u548c\u5468\u957f\u7684\u8ba1\u7b97\u3002\u4f8b\u5982\uff0c\u77e9\u5f62\u7684\u5468\u957f\u662f(2 * \u957f\u5ea6) + (2 * \u5bbd\u5ea6)\uff0c\u9762\u79ef\u662f\u957f\u5ea6 * \u5bbd\u5ea6\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u7528\u6237\u4ea4\u4e92\u548c\u6570\u636e\u5c55\u793a<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u56fe\u5f62\u7684\u7ed8\u5236\uff0c\u8fd8\u8981\u901a\u8fc7\u6587\u672c\u6846\u3001\u6807\u7b7e\u6216\u5f39\u7a97\u7b49\u5f62\u5f0f\u5c55\u793a\u6d4b\u91cf\u7ed3\u679c\uff0c\u4ee5\u589e\u8fdb\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n<\/p>\n<p><h4>\u7ed3\u679c\u8f93\u51fa<\/h4>\n<\/p>\n<p><p>\u7f16\u5199\u51fd\u6570\uff0c\u5c06\u6d4b\u91cf\u7ed3\u679c\u683c\u5f0f\u5316\u540e\u8f93\u51fa\u81f3\u754c\u9762\u6307\u5b9a\u4f4d\u7f6e\u3002\u6ce8\u610f\u5728\u7528\u6237\u8fdb\u884c\u65b0\u7684\u6d4b\u91cf\u524d\u6e05\u7a7a\u65e7\u7684\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><h4>\u4ea4\u4e92\u5f0f\u6307\u5bfc<\/h4>\n<\/p>\n<p><p>\u4e3a\u7528\u6237\u63d0\u4f9b\u6307\u5bfc\u6027\u7684\u4fe1\u606f\uff0c\u6bd4\u5982\u5f53\u524d\u9009\u62e9\u7684\u5de5\u5177\u3001\u5982\u4f55\u64cd\u4f5c\u4ee5\u53ca\u7ed3\u679c\u7684\u89e3\u8bfb\u7b49\uff0c\u8fd9\u53ef\u4ee5\u901a\u8fc7\u72b6\u6001\u680f\u6216\u60ac\u6d6e\u63d0\u793a\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u6d4b\u8bd5\u548c\u8c03\u8bd5<\/h3>\n<\/p>\n<p><p>\u5bf9\u51e0\u4f55\u6d4b\u91cf\u5de5\u5177\u8fdb\u884c\u5168\u9762\u7684\u6d4b\u8bd5\uff0c\u4ee5\u786e\u4fdd\u529f\u80fd\u7684\u51c6\u786e\u6027\u548c\u7528\u6237\u754c\u9762\u7684\u54cd\u5e94\u6027\u3002\u4fee\u590d\u53ef\u80fd\u51fa\u73b0\u7684\u9519\u8bef\u5e76\u4f18\u5316\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n<\/p>\n<p><h4>\u5355\u5143\u6d4b\u8bd5<\/h4>\n<\/p>\n<p><p>\u7f16\u5199\u5355\u5143\u6d4b\u8bd5\u6765\u9a8c\u8bc1\u5404\u51e0\u4f55\u8ba1\u7b97\u51fd\u6570\u7684\u6b63\u786e\u6027\u3002\u4fdd\u8bc1\u5728\u5404\u79cd\u6761\u4ef6\u4e0b\u90fd\u80fd\u5f97\u51fa\u6b63\u786e\u7684\u6d4b\u91cf\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><h4>\u8c03\u8bd5\u548c\u4f18\u5316<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7\u8c03\u8bd5\u5de5\u5177\u6392\u67e5\u53ef\u80fd\u7684\u7a0b\u5e8f\u9519\u8bef\u3002\u5bf9\u7528\u6237\u754c\u9762\u548c\u51e0\u4f55\u7b97\u6cd5\u8fdb\u884c\u4f18\u5316\uff0c\u4ee5\u63d0\u9ad8\u5de5\u5177\u7684\u6027\u80fd\u548c\u51c6\u786e\u6027\u3002<\/p>\n<\/p>\n<p><p>\u7efc\u4e0a\u6240\u8ff0\uff0cC#\u5b9e\u73b0\u51e0\u4f55\u6d4b\u91cf\u5de5\u5177\u9700\u8981\u7ed3\u5408\u56fe\u5f62\u7528\u6237\u754c\u9762\u8bbe\u8ba1\u3001\u4e8b\u4ef6\u5904\u7406\u3001\u56fe\u5f62\u7ed8\u5236\u548c\u51e0\u4f55\u7b97\u6cd5\u7b49\u591a\u65b9\u9762\u7684\u6280\u80fd\u3002\u901a\u8fc7\u8fd9\u4e9b\u6b65\u9aa4\u7684\u534f\u8c03\uff0c\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u65e2\u7f8e\u89c2\u53c8\u529f\u80fd\u4e30\u5bcc\u7684\u51e0\u4f55\u6d4b\u91cf\u5de5\u5177\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>Q1\uff1aC# \u4e2d\u6709\u54ea\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u7528\u6765\u5b9e\u73b0\u51e0\u4f55\u6d4b\u91cf\u5de5\u5177\uff1f<\/strong><\/p>\n<p>A1\uff1aC# \u6709\u8bb8\u591a\u65b9\u6cd5\u53ef\u4ee5\u5b9e\u73b0\u51e0\u4f55\u6d4b\u91cf\u5de5\u5177\uff0c\u4f8b\u5982\u4f7f\u7528\u6570\u5b66\u5e93\u6216\u51e0\u4f55\u8ba1\u7b97\u5e93\u6765\u8fdb\u884c\u51e0\u4f55\u8ba1\u7b97\uff0c\u4f7f\u7528\u56fe\u5f62\u5e93\u6765\u7ed8\u5236\u548c\u663e\u793a\u51e0\u4f55\u56fe\u5f62\uff0c\u6216\u8005\u4f7f\u7528\u81ea\u5b9a\u4e49\u7b97\u6cd5\u8fdb\u884c\u6d4b\u91cf\u548c\u8ba1\u7b97\u7b49\u3002<\/p>\n<p><strong>Q2\uff1aC# \u4e2d\u53ef\u4ee5\u4f7f\u7528\u54ea\u4e9b\u6570\u5b66\u5e93\u6216\u51e0\u4f55\u8ba1\u7b97\u5e93\u5b9e\u73b0\u51e0\u4f55\u6d4b\u91cf\u5de5\u5177\uff1f<\/strong><\/p>\n<p>A2\uff1aC# \u4e2d\u6709\u591a\u4e2a\u6570\u5b66\u5e93\u6216\u51e0\u4f55\u8ba1\u7b97\u5e93\u53ef\u4f9b\u9009\u62e9\uff0c\u6bd4\u5982 Math.NET Numerics\u3001Accord.NET\u3001OpenTk\u3001Unity3D \u7b49\u3002\u8fd9\u4e9b\u5e93\u63d0\u4f9b\u4e86\u5404\u79cd\u6570\u5b66\u51fd\u6570\u548c\u51e0\u4f55\u8ba1\u7b97\u529f\u80fd\uff0c\u53ef\u4ee5\u7528\u6765\u5b9e\u73b0\u51e0\u4f55\u6d4b\u91cf\u5de5\u5177\u7684\u5404\u79cd\u529f\u80fd\uff0c\u5982\u8ba1\u7b97\u4e24\u4e2a\u70b9\u4e4b\u95f4\u7684\u8ddd\u79bb\u3001\u8ba1\u7b97\u7ebf\u6bb5\u548c\u591a\u8fb9\u5f62\u7684\u4ea4\u70b9\u7b49\u3002<\/p>\n<p><strong>Q3\uff1aC# \u4e2d\u5982\u4f55\u5b9e\u73b0\u51e0\u4f55\u6d4b\u91cf\u5de5\u5177\u7684\u56fe\u5f62\u663e\u793a\u548c\u7528\u6237\u4ea4\u4e92\uff1f<\/strong><\/p>\n<p>A3\uff1a\u8981\u5b9e\u73b0\u51e0\u4f55\u6d4b\u91cf\u5de5\u5177\u7684\u56fe\u5f62\u663e\u793a\u548c\u7528\u6237\u4ea4\u4e92\uff0c\u53ef\u4ee5\u4f7f\u7528 C# \u7684\u56fe\u5f62\u5e93\uff0c\u5982 Windows Forms\u3001WPF \u6216\u8005 Unity3D\u3002\u8fd9\u4e9b\u5e93\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u56fe\u5f62\u7ed8\u5236\u548c\u7528\u6237\u4ea4\u4e92\u529f\u80fd\uff0c\u53ef\u4ee5\u901a\u8fc7\u7ed8\u5236\u51e0\u4f55\u56fe\u5f62\u3001\u5904\u7406\u9f20\u6807\u548c\u952e\u76d8\u4e8b\u4ef6\u7b49\u65b9\u5f0f\uff0c\u5b9e\u73b0\u51e0\u4f55\u6d4b\u91cf\u5de5\u5177\u7684\u56fe\u5f62\u663e\u793a\u548c\u7528\u6237\u4ea4\u4e92\u529f\u80fd\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u51e0\u4f55\u6d4b\u91cf\u5de5\u5177\u5728C#\u4e2d\u7684\u5b9e\u73b0\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u56fe\u5f62\u7528\u6237\u754c\u9762\uff08GUI\uff09\u6846\u67b6\u3001\u7ed8\u56feAPI\u3001\u4ee5\u53ca\u6570\u5b66\u8fd0\u7b97\u6765\u5b8c\u6210\u3002\u57fa\u672c\u5b9e\u73b0\u65b9\u5f0f [&hellip;]","protected":false},"author":3,"featured_media":294476,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/294453"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=294453"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/294453\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/294476"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=294453"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=294453"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=294453"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}