WPF:取得目前螢幕的尺寸
使用SystemParameters 擷取主畫面的尺寸很簡單,但確定目前螢幕的尺寸畫面可能更複雜,尤其是在多螢幕配置中。為了應對這項挑戰,請考慮以下方法:
利用WinForms 螢幕包裝器
圍繞System.Windows.Forms.Screen 物件建立與WPF 相容的包裝器存取螢幕相關資訊的便捷方式。以下是一個範例實作:
public class WpfScreen { public static IEnumerable<wpfscreen> AllScreens() { foreach (Screen screen in System.Windows.Forms.Screen.AllScreens) { yield return new WpfScreen(screen); } } public static WpfScreen GetScreenFrom(Window window) { WindowInteropHelper windowInteropHelper = new WindowInteropHelper(window); Screen screen = System.Windows.Forms.Screen.FromHandle(windowInteropHelper.Handle); WpfScreen wpfScreen = new WpfScreen(screen); return wpfScreen; } public static WpfScreen GetScreenFrom(Point point) { int x = (int)Math.Round(point.X); int y = (int)Math.Round(point.Y); System.Drawing.Point drawingPoint = new System.Drawing.Point(x, y); Screen screen = System.Windows.Forms.Screen.FromPoint(drawingPoint); WpfScreen wpfScreen = new WpfScreen(screen); return wpfScreen; } public static WpfScreen Primary { get { return new WpfScreen(System.Windows.Forms.Screen.PrimaryScreen); } } private readonly Screen screen; internal WpfScreen(System.Windows.Forms.Screen screen) { this.screen = screen; } public Rect DeviceBounds { get { return this.GetRect(this.screen.Bounds); } } public Rect WorkingArea { get { return this.GetRect(this.screen.WorkingArea); } } private Rect GetRect(Rectangle value) { return new Rect { X = value.X, Y = value.Y, Width = value.Width, Height = value.Height }; } public bool IsPrimary { get { return this.screen.Primary; } } public string DeviceName { get { return this.screen.DeviceName; } } }</wpfscreen>
在XAML 中存取螢幕尺寸
雖然無法直接從XAML 存取螢幕尺寸,但您可以組合使用附加屬性與包裝類:
<usercontrol> <usercontrol.resources> <wpfscreenextension x:key="ScreenExtension"></wpfscreenextension> </usercontrol.resources> <stackpanel> <textblock text="Device Bounds Width: {Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=Width, Converter={StaticResource ScreenExtension}}" verticalalignment="Center"></textblock> <textblock text="Working Area Height: {Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=Height, Converter={StaticResource ScreenExtension}}" verticalalignment="Center"></textblock> </stackpanel> </usercontrol>
public class WpfScreenExtension : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { UserControl userControl = value as UserControl; if (userControl == null) return null; Rect bounds = WpfScreen.GetScreenFrom(userControl).DeviceBounds; if (bounds.IsEmpty) return String.Empty; if (parameter?.ToString() == "Width") return bounds.Width; else if (parameter?.ToString() == "Height") return bounds.Height; else return bounds; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { return null; } }
以上是WPF中如何高效率取得目前螢幕尺寸?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

C 和XML的未來發展趨勢分別為:1)C 將通過C 20和C 23標準引入模塊、概念和協程等新特性,提升編程效率和安全性;2)XML將繼續在數據交換和配置文件中佔據重要地位,但會面臨JSON和YAML的挑戰,並朝著更簡潔和易解析的方向發展,如XMLSchema1.1和XPath3.1的改進。

現代C 設計模式利用C 11及以後的新特性實現,幫助構建更靈活、高效的軟件。 1)使用lambda表達式和std::function簡化觀察者模式。 2)通過移動語義和完美轉發優化性能。 3)智能指針確保類型安全和資源管理。

C 多線程和並發編程的核心概念包括線程的創建與管理、同步與互斥、條件變量、線程池、異步編程、常見錯誤與調試技巧以及性能優化與最佳實踐。 1)創建線程使用std::thread類,示例展示瞭如何創建並等待線程完成。 2)同步與互斥使用std::mutex和std::lock_guard保護共享資源,避免數據競爭。 3)條件變量通過std::condition_variable實現線程間的通信和同步。 4)線程池示例展示瞭如何使用ThreadPool類並行處理任務,提高效率。 5)異步編程使用std::as

C 的內存管理、指針和模板是核心特性。 1.內存管理通過new和delete手動分配和釋放內存,需注意堆和棧的區別。 2.指針允許直接操作內存地址,使用需謹慎,智能指針可簡化管理。 3.模板實現泛型編程,提高代碼重用性和靈活性,需理解類型推導和特化。

C 適合系統編程和硬件交互,因為它提供了接近硬件的控制能力和麵向對象編程的強大特性。 1)C 通過指針、內存管理和位操作等低級特性,實現高效的系統級操作。 2)硬件交互通過設備驅動程序實現,C 可以編寫這些驅動程序,處理與硬件設備的通信。

C 適合構建高性能遊戲和仿真係統,因為它提供接近硬件的控制和高效性能。 1)內存管理:手動控制減少碎片,提高性能。 2)編譯時優化:內聯函數和循環展開提昇運行速度。 3)低級操作:直接訪問硬件,優化圖形和物理計算。

文件操作難題的真相:文件打開失敗:權限不足、路徑錯誤、文件被佔用。數據寫入失敗:緩衝區已滿、文件不可寫、磁盤空間不足。其他常見問題:文件遍歷緩慢、文本文件編碼不正確、二進製文件讀取錯誤。

深入解析C語言文件操作難題前言文件操作是C語言編程中一項重要的功能。然而,它也可能是一個有挑戰性的領域,尤其是在處理複雜文件結構時。本文將深入解析C語言文件操作的常見難題,並提供實戰案例來闡明解決方法。打開和關閉文件打開文件時,有兩種主要的模式:r(只讀)和w(寫只)。要打開文件,可以使用fopen()函數:FILE*fp=fopen("file.txt","r");打開文件後,必須在使用完後將其關閉,以釋放資源:fclose(fp);讀取和寫入數據可以使


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

Atom編輯器mac版下載
最受歡迎的的開源編輯器

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

禪工作室 13.0.1
強大的PHP整合開發環境

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

SublimeText3漢化版
中文版,非常好用