Vir*_*rld 51 wpf location window
我想TaskBar在窗户启动时将窗口显示在时钟顶部.
如何找到桌面右下角的位置?
我使用此代码在Windows窗体应用程序中运行良好,但在WPF中无法正常工作:
var desktopWorkingArea = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea;
this.Left = desktopWorkingArea.Right - this.Width;
this.Top = desktopWorkingArea.Bottom - this.Height;
Run Code Online (Sandbox Code Playgroud)
Kla*_*s78 104
此代码适用于WPF,显示100%和125%
private void Window_Loaded(object sender, RoutedEventArgs e)
{
var desktopWorkingArea = System.Windows.SystemParameters.WorkArea;
this.Left = desktopWorkingArea.Right - this.Width;
this.Top = desktopWorkingArea.Bottom - this.Height;
}
Run Code Online (Sandbox Code Playgroud)
简而言之,我使用
System.Windows.SystemParameters.WorkArea
代替
System.Windows.Forms.Screen.PrimaryScreen.WorkingArea
您可以使用窗口的SizeChanged事件,而不是Loaded希望窗口在大小更改时保持在角落中。如果窗口已Window.SizeToContent设置为 ; 以外的某个值SizeToContent.Manual,则这尤其方便。在这种情况下,它将在保持在角落的同时进行调整以适应内容。
public MyWindow()
{
SizeChanged += (o, e) =>
{
var r = SystemParameters.WorkArea;
Left = r.Right - ActualWidth;
Top = r.Bottom - ActualHeight;
};
InitializeComponent();
}
Run Code Online (Sandbox Code Playgroud)
另请注意,您应该减去ActualWidth和ActualHeight(而不是其他回复中所示的Width和Height)来处理更多可能的情况,例如SizeToContent在运行时在模式之间切换。
| 归档时间: |
|
| 查看次数: |
52016 次 |
| 最近记录: |