gpt4 book ai didi

c# - wpf窗口定位问题

转载 作者:可可西里 更新时间:2023-11-01 10:06:53 26 4
gpt4 key购买 nike

我有一个在第二个屏幕上运行的应用程序,当用户在第一个屏幕上运行该应用程序时,该应用程序会检测到第二个显示器并将其位置更改为第二个屏幕。

这有一个问题,主窗口的子窗口出现在第一个监视器中。如果正确建立所有者属性,则不应发生这种情况。

Window1 w = new Window1();
win.Owner = Application.Current.MainWindow;

我的应用程序很复杂,由调用子窗口的组件组成,但我附上了一段代码来说明问题。在第一个监视器中执行代码,手动将窗口移动到辅助监视器,然后按下按钮调用出现在第一个监视器中的子窗口:(。

注意:我知道我可以编写一个代码来检测每个子窗口中的辅助监视器并移动到那时,但如果可能的话,我想要一个更简单和正确的解决方案。

注意 2:在 visual studio 之外运行应用程序,直接从“.exe”运行。在 Visual Studio 中工作正常。

<Window x:Class="Test.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Control="clr-namespace:Borrarrrrr"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Control:UserControl1 x:Name="ctr" />
</Grid>
</Window>

public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}

private void Button_Click(object sender, RoutedEventArgs e)
{
Window1 w = new Window1();
//w.Owner = this;
w.Owner = Application.Current.MainWindow;
w.Show();
}
}


<UserControl x:Class="Test.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<Button Click="Button_Click"></Button>
</Grid>
</UserControl>

public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}

private void Button_Click(object sender, RoutedEventArgs e)
{
Window1 w = new Window1();
w.Owner = Application.Current.MainWindow;
w.Show();
}
}


<Window x:Class="Test.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300"
WindowStartupLocation="CenterOwner" WindowStyle="None" AllowsTransparency="True"
WindowState="Maximized">
<Grid Background="Aqua">

</Grid>
</Window>

最佳答案

尝试设置 Window.WindowStartupLocation :

private void Button_Click(object sender, RoutedEventArgs e)
{
Window1 w = new Window1();
w.Owner = Application.Current.MainWindow;
w.WindowStartupLocation = System.Windows.WindowStartupLocation.CenterOwner;
w.Show();
}

关于c# - wpf窗口定位问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17196023/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com