Winform WPF 窗體顯示位置


   WinForm 窗體顯示位置       

          窗體顯示的位置首先由窗體的StartPosition決定,FormStartPosition這個枚舉值由如下幾種情況
         // 摘要:
        //     窗體的位置由 System.Windows.Forms.Control.Location 屬性確定。
        Manual = 0,
        //
        // 摘要:
        //     窗體在當前顯示窗口中居中,其尺寸在窗體大小中指定。
        CenterScreen = 1,
        //
        // 摘要:
        //     窗體定位在 Windows 默認位置,其尺寸在窗體大小中指定。
        WindowsDefaultLocation = 2,
        //
        // 摘要:
        //     窗體定位在 Windows 默認位置,其邊界也由 Windows 默認決定。
        WindowsDefaultBounds = 3,
        //
        // 摘要:
        //     窗體在其父窗體中居中。
        CenterParent = 4,
View Code

        1、顯示的窗體位於屏幕的中心

MainForm mainForm = new MainForm();
mainForm.StartPosition = FormStartPosition.CenterScreen;//顯示位於當前屏幕的中心位置 mainForm.Show();

       2、顯示位於主窗體的中心位置

dlg.StartPosition = FormStartPosition.CenterParent;//能夠這樣做的前提是主窗體必須先定義和顯示。否則登錄窗體可能無法找到父窗體。
dlg.ShowDialog();

       3、手動設置顯示的具體位置(常用)

 要想顯示在自己設置的任意位置,首先必須設置ForStartPosion的枚舉值為Manual,
dlg.StartPosition=FormStartPosition.Manual;
隨后獲取屏幕的分辨率,也就是顯示器屏幕的大小。
int xWidth = SystemInformation.PrimaryMonitorSize.Width;//獲取顯示器屏幕寬度
int yHeight = SystemInformation.PrimaryMonitorSize.Height;//高度
然后定義窗口位置,以主窗體為例
mainForm.Location = new Point(xWidth/2, yHeight/2);//這里需要再減去窗體本身的寬度和高度的一半
mainForm.Show();

    Wpf 窗體是顯示位置和winform類似

1、在屏幕中間顯示,設置window.WindowStartupLocation = WindowStartupLocation.CenterScreen;
private void button1_Click(object sender, RoutedEventArgs e)
{
TestWindow window = new TestWindow();
window.WindowStartupLocation = WindowStartupLocation.CenterScreen;
window.ShowDialog();
}
2、在父窗口中間顯示,設置window.WindowStartupLocation = WindowStartupLocation.CenterOwner;,並指定Owner。
private void button1_Click(object sender, RoutedEventArgs e)
{
TestWindow window = new TestWindow();
window.WindowStartupLocation = WindowStartupLocation.CenterOwner;
window.Owner = this;
window.ShowDialog();
}
3、在任意位置顯示,設置window.WindowStartupLocation = WindowStartupLocation.Manual;並制定窗口的Left和Top坐標。
private void button1_Click(object sender, RoutedEventArgs e)
{
TestWindow window = new TestWindow();
window.WindowStartupLocation = WindowStartupLocation.Manual;
window.Left = 0;
window.Top = 0;
window.ShowDialog();
}
View Code

 

      

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM