{"id":508,"date":"2024-12-25T00:51:01","date_gmt":"2024-12-24T16:51:01","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=508"},"modified":"2024-12-25T00:51:01","modified_gmt":"2024-12-24T16:51:01","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8-wpf-%e5%ba%94%e7%94%a8%e4%b8%ad%e8%8e%b7%e5%8f%96%e6%b4%bb%e5%8a%a8%e5%b1%8f%e5%b9%95%e7%9a%84%e5%b0%ba%e5%af%b8%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2024\/12\/25\/%e5%a6%82%e4%bd%95%e5%9c%a8-wpf-%e5%ba%94%e7%94%a8%e4%b8%ad%e8%8e%b7%e5%8f%96%e6%b4%bb%e5%8a%a8%e5%b1%8f%e5%b9%95%e7%9a%84%e5%b0%ba%e5%af%b8%ef%bc%9f\/","title":{"rendered":"\u5982\u4f55\u5728 WPF \u5e94\u7528\u4e2d\u83b7\u53d6\u6d3b\u52a8\u5c4f\u5e55\u7684\u5c3a\u5bf8\uff1f"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">\u5728 WPF \u5e94\u7528\u7a0b\u5e8f\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7 <code>System.Windows.Forms.Screen<\/code> \u7c7b\u6216\u8005 <code>System.Windows.SystemParameters<\/code> \u6765\u83b7\u53d6\u6d3b\u52a8\u5c4f\u5e55\u7684\u5c3a\u5bf8\u3002<\/p>\n<\/blockquote>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u65b9\u6cd5 1\uff1a\u4f7f\u7528 <code>System.Windows.Forms.Screen<\/code> \u7c7b<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>System.Windows.Forms.Screen<\/code> \u7c7b\u63d0\u4f9b\u4e86\u8be6\u7ec6\u7684\u5c4f\u5e55\u4fe1\u606f\uff0c\u5305\u62ec\u5f53\u524d\u6d3b\u52a8\u5c4f\u5e55\u7684\u5de5\u4f5c\u533a\u548c\u5206\u8fa8\u7387\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u6b65\u9aa4<\/strong><\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u6dfb\u52a0\u5bf9 <code>System.Windows.Forms<\/code> \u7684\u5f15\u7528\u3002<\/li>\n\n\n\n<li>\u4f7f\u7528 <code>Screen.FromHandle<\/code> \u6216 <code>Screen.FromPoint<\/code> \u83b7\u53d6\u7279\u5b9a\u7a97\u53e3\u6216\u70b9\u6240\u5728\u7684\u5c4f\u5e55\u3002<\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u793a\u4f8b\u4ee3\u7801<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>using System;\nusing System.Windows;\nusing System.Windows.Forms; \/\/ \u5f15\u7528\u6b64\u547d\u540d\u7a7a\u95f4\nusing System.Runtime.InteropServices;\n\npublic partial class MainWindow : Window\n{\n    public MainWindow()\n    {\n        InitializeComponent();\n\n        \/\/ \u83b7\u53d6\u5f53\u524d\u7a97\u53e3\u53e5\u67c4\n        var helper = new System.Windows.Interop.WindowInteropHelper(this);\n        IntPtr hwnd = helper.Handle;\n\n        \/\/ \u83b7\u53d6\u7a97\u53e3\u6240\u5728\u7684\u5c4f\u5e55\n        Screen currentScreen = Screen.FromHandle(hwnd);\n\n        \/\/ \u5c4f\u5e55\u5206\u8fa8\u7387\n        int screenWidth = currentScreen.Bounds.Width;\n        int screenHeight = currentScreen.Bounds.Height;\n\n        \/\/ \u5de5\u4f5c\u533a\u5c3a\u5bf8\uff08\u6392\u9664\u4efb\u52a1\u680f\u7b49\uff09\n        int workAreaWidth = currentScreen.WorkingArea.Width;\n        int workAreaHeight = currentScreen.WorkingArea.Height;\n\n        \/\/ \u663e\u793a\u7ed3\u679c\n        MessageBox.Show($\"\u5c4f\u5e55\u5206\u8fa8\u7387: {screenWidth}x{screenHeight}\\n\u5de5\u4f5c\u533a\u5c3a\u5bf8: {workAreaWidth}x{workAreaHeight}\");\n    }\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u65b9\u6cd5 2\uff1a\u4f7f\u7528 <code>System.Windows.SystemParameters<\/code><\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u53ea\u9700\u8981\u83b7\u53d6\u4e3b\u5c4f\u5e55\u7684\u5c3a\u5bf8\uff0c\u53ef\u4ee5\u4f7f\u7528 WPF \u7684\u5185\u7f6e <code>SystemParameters<\/code>\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u793a\u4f8b\u4ee3\u7801<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>using System.Windows;\n\npublic partial class MainWindow : Window\n{\n    public MainWindow()\n    {\n        InitializeComponent();\n\n        \/\/ \u4e3b\u5c4f\u5e55\u7684\u5168\u5c4f\u5206\u8fa8\u7387\n        double screenWidth = SystemParameters.PrimaryScreenWidth;\n        double screenHeight = SystemParameters.PrimaryScreenHeight;\n\n        \/\/ \u4e3b\u5c4f\u5e55\u7684\u5de5\u4f5c\u533a\u5c3a\u5bf8\uff08\u6392\u9664\u4efb\u52a1\u680f\u7b49\uff09\n        double workAreaWidth = SystemParameters.WorkArea.Width;\n        double workAreaHeight = SystemParameters.WorkArea.Height;\n\n        \/\/ \u663e\u793a\u7ed3\u679c\n        MessageBox.Show($\"\u5c4f\u5e55\u5206\u8fa8\u7387: {screenWidth}x{screenHeight}\\n\u5de5\u4f5c\u533a\u5c3a\u5bf8: {workAreaWidth}x{workAreaHeight}\");\n    }\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u65b9\u6cd5\u5bf9\u6bd4<\/strong><\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u65b9\u6cd5<\/th><th>\u4f7f\u7528\u573a\u666f<\/th><th>\u4f18\u70b9<\/th><th>\u7f3a\u70b9<\/th><\/tr><\/thead><tbody><tr><td><code>System.Windows.Forms.Screen<\/code><\/td><td>\u83b7\u53d6\u591a\u5c4f\u5e55\u6216\u6d3b\u52a8\u5c4f\u5e55\u7684\u5c3a\u5bf8<\/td><td>\u652f\u6301\u591a\u5c4f\u5e55\uff0c\u529f\u80fd\u5f3a\u5927<\/td><td>\u9700\u8981\u6dfb\u52a0\u989d\u5916\u5f15\u7528<\/td><\/tr><tr><td><code>System.Windows.SystemParameters<\/code><\/td><td>\u83b7\u53d6\u4e3b\u5c4f\u5e55\u5c3a\u5bf8<\/td><td>\u7b80\u5355\u6613\u7528\uff0c\u65e0\u9700\u5916\u90e8\u5f15\u7528<\/td><td>\u53ea\u652f\u6301\u4e3b\u5c4f\u5e55\uff0c\u4e0d\u652f\u6301\u591a\u5c4f\u5e55<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u603b\u7ed3<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5982\u679c\u5e94\u7528\u7a0b\u5e8f\u9700\u8981\u652f\u6301\u591a\u5c4f\u5e55\u573a\u666f\uff0c\u5efa\u8bae\u4f7f\u7528 <code>System.Windows.Forms.Screen<\/code>\u3002<\/li>\n\n\n\n<li>\u5982\u679c\u53ea\u9700\u8981\u4e3b\u5c4f\u5e55\u7684\u4fe1\u606f\u4e14\u4e0d\u60f3\u6dfb\u52a0\u989d\u5916\u5f15\u7528\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>System.Windows.SystemParameters<\/code>\u3002<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 WPF \u5e94\u7528\u7a0b\u5e8f\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7 System.Windows.Forms.Screen \u7c7b\u6216\u8005 System [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[59],"tags":[],"class_list":["post-508","post","type-post","status-publish","format-standard","hentry","category-c"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/508","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=508"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/508\/revisions"}],"predecessor-version":[{"id":509,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/508\/revisions\/509"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=508"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=508"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=508"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}