WPF多屏显示
目錄WPF多屏顯示實(shí)現(xiàn)代碼實(shí)現(xiàn)原理注意事項(xiàng)
WPF多屏顯示
實(shí)現(xiàn)代碼
需要在分屏顯示的窗體
<Window x:Class="WpfClient.Tools.FrmSubScreen"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WpfClient.Tools"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Title="FrmSubScreen"
Width="800"
Height="450"
AllowsTransparency="True"
Background="Transparent"
ResizeMode="NoResize"
ShowInTaskbar="False"
Topmost="True"
WindowStartupLocation="CenterScreen"
WindowStyle="None"
mc:Ignorable="d">
<Grid>
<Image Grid.Column="1" Source="https://img.alicdn.com/imgextra/i2/4179999320/O1CN01CIIi2C2IibyZZrMHw_!!0-item_pic.jpg_430x430q90.jpg" Stretch="Fill" />
</Grid>
</Window>
需要在分屏顯示的窗體后臺(tái)代碼
public partial class FrmSubScreen : Window
{
public FrmSubScreen()
{
InitializeComponent();
this.Loaded += (s, e) =>
{
foreach (Screen scr in Screen.AllScreens)
{
if (!scr.Primary)
{
//設(shè)置窗體位置
WindowStartupLocation = WindowStartupLocation.Manual;
Left = scr.WorkingArea.Left;
Top = scr.WorkingArea.Top;
WindowState = WindowState.Maximized;
break;
}
}
};
}
}
讓窗體在分屏顯示的代碼
public class SecondaryScreen
{
public static void Display()
{
var screens = Screen.AllScreens;
if (Screen.AllScreens.Count()>=2)
{
Dispatcher.CurrentDispatcher.Invoke(()=>
{
try
{
FrmSubScreen subScreen = new FrmSubScreen();
subScreen.Show();
}
catch (Exception e)
{
Logger.Default.Error(e.Message,e.StackTrace);
}
});
}
}
}
實(shí)現(xiàn)原理
假設(shè)屏幕分辨率是1920*1080,有兩屏幕,多屏的情況下主屏的左上角的坐標(biāo)為(0,0),第二個(gè)屏幕左上角坐標(biāo)是(1920,0)。雙屏顯示就是讓窗體自動(dòng)偏移到指定的屏幕中。
例如:
Left = scr.WorkingArea.Left;
Top = scr.WorkingArea.Top;
注意事項(xiàng)
需要特別注意:Window.WindowState屬性的設(shè)置,如果在Xaml代碼中直接設(shè)置最大化,那么無論后續(xù)如何設(shè)置分屏窗口都會(huì)直接顯示在主屏幕中。
解決方案:
在Loaded時(shí)設(shè)置WindowState = WindowState.Maximized
登峰造極的成就源于自律
總結(jié)
- 上一篇: 剑指offer之从上到下打印二叉树
- 下一篇: 《一天聊一个设计模式》 单例