我有一台带1-4个显示器的电脑。每个显示器可能具有不同的分辨率。我使用System.Windows.Forms.Screen.AllScreens获取屏幕(监视器)及其分辨率。对于每个屏幕,我打开一个新的全屏窗口。 我有一个数据项列表(显示时可能会有不同的高度)。我想使用ItemsControl显示此数据项。当在第一台显示器上使用所有空间(高度)时,它必须转到第二个,依此类推......但是只有完整的数据项必须在屏幕上。
我该怎么做?或者我是否只需要使用一个延伸到多个屏幕的窗口?
答案 0 :(得分:2)
我是 noob ,但我会尽力帮助。我正在研究一个wpf全屏双显示器应用程序。 (我希望任务栏可见)。
<Window x:Class="SlideWpf.SecondWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="500" Width="800" BorderThickness="0" AllowsTransparency="True" WindowStyle="None">
<Window.Background>
<SolidColorBrush Opacity="0"></SolidColorBrush>
</Window.Background>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="55*" Name="GRowTop"/>
<RowDefinition Height="409*" Name="GRowContent"/>
<RowDefinition Height="36*" Name="GRowBottom"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="89*" Name="GColLeft"/>
<ColumnDefinition Width="659*" Name="GColMid"/>
<ColumnDefinition Width="52*" Name="GColRight"/>
</Grid.ColumnDefinitions>
<Border BorderThickness="2" BorderBrush="Red" Grid.Row="0" Grid.Column="1" Grid.RowSpan="2">
<Border BorderThickness="10" BorderBrush="Black">
<Grid Name="g1" Background="AntiqueWhite"></Grid>
</Border>
</Border>
<Border BorderThickness="2" BorderBrush="Red" Grid.Row="1" Grid.Column="0" Grid.RowSpan="2">
<Border BorderThickness="10" BorderBrush="Black">
<Grid Name="g2" Background="Beige"></Grid>
</Border>
</Border>
</Grid>
</Window>
<强>击穿强>
wpf http://i58.tinypic.com/9hj1i8.jpg
if (ScreensUtils.Bounds(1).Left < ScreensUtils.Bounds(0).Left)
{
Left = ScreensUtils.Location(1).X;
Top = Math.Min(ScreensUtils.Location(1).Y, ScreensUtils.Location(0).Y);
Width = ScreensUtils.Size(0).Width + ScreensUtils.Size(1).Width;
Height = Math.Max(ScreensUtils.Bounds(0).Bottom, ScreensUtils.Bounds(1).Top + ScreensUtils.Size(1).Height);
GRowTop.Height = new GridLength(ScreensUtils.Location(1).Y);
GColLeft.Width = new GridLength(ScreensUtils.Size(1).Width);
GColMid.Width = new GridLength(ScreensUtils.Size(0).Width);
GRowBottom.Height = new GridLength(ScreensUtils.Bounds(1).Top + ScreensUtils.Size(1).Height - ScreensUtils.Bounds(0).Bottom);
}
这处理我的具体情况:我有一个辅助显示器,左边的显示器,比第一个更小,略低。
desktop http://i61.tinypic.com/vetyyx.jpg
现在你可以对网格有所了解。
<强>击穿强>
ScreensUtils完全可以成为一种方法。我计划将它扩展一点,所以我把它变成了一个类。
class ScreensUtils
{
public static Size Size(int index)
{
return Screen.AllScreens[index].WorkingArea.Size;
}
public static Point Location(int index)
{
return Screen.AllScreens[index].WorkingArea.Location;
}
public static Rectangle Bounds(int index)
{
return new Rectangle( Screen.AllScreens[index].WorkingArea.Left, Screen.AllScreens[index].WorkingArea.Top, Screen.AllScreens[index].WorkingArea.Left + Screen.AllScreens[index].WorkingArea.Size.Width , Screen.AllScreens[index].WorkingArea.Top + Screen.AllScreens[index].WorkingArea.Size.Height);
}
}
这还需要一个标题栏。可以跨显示器延伸,也可以仅在主显示器上延任何人都应该没问题(一个简单的网格或一个网格+堆栈/停靠面板中已存在的内容网格。)
存在自动隐藏任务栏的问题。 (当任务栏隐藏时,该空格将显示为空。)
为此,有:
class TaskbarUtils
{
public enum TaskbarPosition
{
Unknown = -1,
Left,
Top,
Right,
Bottom,
}
public sealed class Taskbar
{
private const string ClassName = "Shell_TrayWnd";
public Rectangle Bounds
{
get;
private set;
}
public TaskbarPosition Position
{
get;
private set;
}
public Point Location
{
get
{
return this.Bounds.Location;
}
}
public Size Size
{
get
{
return this.Bounds.Size;
}
}
//Always returns false under Windows 7
public bool AlwaysOnTop
{
get;
private set;
}
public bool AutoHide
{
get;
private set;
}
public Taskbar()
{
IntPtr taskbarHandle = User32.FindWindow(Taskbar.ClassName, null);
APPBARDATA data = new APPBARDATA();
data.cbSize = (uint)Marshal.SizeOf(typeof(APPBARDATA));
data.hWnd = taskbarHandle;
IntPtr result = Shell32.SHAppBarMessage(ABM.GetTaskbarPos, ref data);
if (result == IntPtr.Zero)
throw new InvalidOperationException();
this.Position = (TaskbarPosition)data.uEdge;
this.Bounds = Rectangle.FromLTRB(data.rc.left, data.rc.top, data.rc.right, data.rc.bottom);
data.cbSize = (uint)Marshal.SizeOf(typeof(APPBARDATA));
result = Shell32.SHAppBarMessage(ABM.GetState, ref data);
int state = result.ToInt32();
this.AlwaysOnTop = (state & ABS.AlwaysOnTop) == ABS.AlwaysOnTop;
this.AutoHide = (state & ABS.Autohide) == ABS.Autohide;
}
}
public enum ABM : uint
{
New = 0x00000000,
Remove = 0x00000001,
QueryPos = 0x00000002,
SetPos = 0x00000003,
GetState = 0x00000004,
GetTaskbarPos = 0x00000005,
Activate = 0x00000006,
GetAutoHideBar = 0x00000007,
SetAutoHideBar = 0x00000008,
WindowPosChanged = 0x00000009,
SetState = 0x0000000A,
}
public enum ABE : uint
{
Left = 0,
Top = 1,
Right = 2,
Bottom = 3
}
public static class ABS
{
public const int Autohide = 0x0000001;
public const int AlwaysOnTop = 0x0000002;
}
public static class Shell32
{
[DllImport("shell32.dll", SetLastError = true)]
public static extern IntPtr SHAppBarMessage(ABM dwMessage, [In] ref APPBARDATA pData);
}
public static class User32
{
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
}
[StructLayout(LayoutKind.Sequential)]
public struct APPBARDATA
{
public uint cbSize;
public IntPtr hWnd;
public uint uCallbackMessage;
public ABE uEdge;
public RECT rc;
public int lParam;
}
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
}
}
你像
一样使用它var _taskbar = new TaskbarUtils;
if(_taskbar.AlwaysOnTop) {}
这是一个打印屏幕。
desktop http://i58.tinypic.com/neygk5.jpg
我发现了 Screen.AllScreens [指数] .WorkingArea.Bottom 报告混乱的价值。我用 Screen.AllScreens [index] .WorkingArea.Top + Screen.AllScreens [index] .WorkingArea.Size.Height
我提供的代码很糟糕。 ScreenUtils也是如此。它是在飞行中完成的,除了我自己以外没有考虑其他情况。但这是件好事。因为我实际上是DoWork(),我会更新我的答案。
你应该完全有能力适应你的需要。
如果您发现错误或有所改善,请发布您的发现。
祝你好运!
PS:任务栏类取自here