簡體   English   中英

如何居中MDI子窗體?

[英]How to center MDI child Form?

我有這段代碼,添加一個新的孩子:

  private void Menu1_Click(object sender, RoutedEventArgs e)
    {

        Point centerPoint = new Point((Container.ActualWidth  / 2) - (500/2), (Container.ActualHeight / 2) - (400 / 2));


        MdiChild newForm = new MdiChild();
        newForm.Title = "My Form";
        newForm.Content = new MyForm1();
        newForm.Width = 500;
        newForm.Height = 400;
        newForm.Resizable = false;
        newForm.MaximizeBox = false;
        newForm.Position = centerPoint;

        Container.Children.Add(newForm);
    }

此代碼用於視圖:

<Window x:Class="WpfApplication1.MainWindow"
    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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WpfApplication1"
    mc:Ignorable="d"
     xmlns:mdi="clr-namespace:WPF.MDI;assembly=WPF.MDI"
Title="WPF.MDI Example" Height="362" Width="684" WindowState="Maximized">
<DockPanel>
    <mdi:MdiContainer Name="Container" Background="#FF474040" >
        <mdi:MdiContainer.Menu>
            <Menu DockPanel.Dock="Top" Width="677">                  
                <MenuItem Header="Menu1" click="Menu1_Click">                        
        </mdi:MdiContainer.Menu>
    </mdi:MdiContainer>
</DockPanel>

如何將MDI子窗體居中?

它總是在左上方打開

我還沒有找到解決方案,

謝謝。

我還嘗試將這段代碼放在子窗體后面的代碼中,但是不起作用:

    public partial class MyForm1: UserControl
{
    BalanceEntities db = new BalanceEntities();
    public MyForm1()
    {
        InitializeComponent();
        this.HorizontalAlignment = HorizontalAlignment.Center;
        this.VerticalAlignment = VerticalAlignment.Center;

    }       
}

您應該像這樣使用Window的WindowStartupLocation屬性:

private void Menu1_Click(object sender, RoutedEventArgs e)
{

    Point centerPoint = new Point((Container.ActualWidth  / 2) - (500/2), (Container.ActualHeight / 2) - (400 / 2));


    MdiChild newForm = new MdiChild();
    newForm .Title = "My Form";
    //The code omitted for the brevity
    newForm.WindowStartupLocation = System.Windows.WindowStartupLocation.CenterScreen;

    Container.Children.Add(newForm);
}

要么:

this.WindowStartupLocation = System.Windows.WindowStartupLocation.CenterScreen;

感謝James Thorpe ,我以這種方式解決了我的問題:

  private void Menu1_Click(object sender, RoutedEventArgs e)
    {      
        MdiChild newForm = new MdiChild();
    newForm.Title = "My Form";
    newForm.Content = new MyForm1();
    newForm.Width = 500;
    newForm.Height = 400;
    newForm.Resizable = false;
    newForm.MaximizeBox = false;

    Container.Children.Add(newForm);

    // And then I add the position:
   Point centerPoint = new Point((Container.ActualWidth / 2) - (newForm.Width / 2), (Container.ActualHeight / 2) - (newForm.Height / 2));
   newForm.Position = centerPoint;
    }

謝謝您的幫助。

執行以下操作對我有用

newForm.StartPosition = FormStartPosition.CenterScreen;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM