WinForm 窗体显示位置

          窗体显示的位置首先由窗体的StartPosition决定,FormStartPosition这个枚举值由如下几种情况
// 摘要:
// 窗体的位置由 System.Windows.Forms.Control.Location 属性确定。
Manual = ,
//
// 摘要:
// 窗体在当前显示窗口中居中,其尺寸在窗体大小中指定。
CenterScreen = ,
//
// 摘要:
// 窗体定位在 Windows 默认位置,其尺寸在窗体大小中指定。
WindowsDefaultLocation = ,
//
// 摘要:
// 窗体定位在 Windows 默认位置,其边界也由 Windows 默认决定。
WindowsDefaultBounds = ,
//
// 摘要:
// 窗体在其父窗体中居中。
CenterParent = ,

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/, yHeight/);//这里需要再减去窗体本身的宽度和高度的一半
mainForm.Show();

Wpf 窗体是显示位置和winform类似

、在屏幕中间显示,设置window.WindowStartupLocation = WindowStartupLocation.CenterScreen;
private void button1_Click(object sender, RoutedEventArgs e)
{
TestWindow window = new TestWindow();
window.WindowStartupLocation = WindowStartupLocation.CenterScreen;
window.ShowDialog();
}
、在父窗口中间显示,设置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();
}
、在任意位置显示,设置window.WindowStartupLocation = WindowStartupLocation.Manual;并制定窗口的Left和Top坐标。
private void button1_Click(object sender, RoutedEventArgs e)
{
TestWindow window = new TestWindow();
window.WindowStartupLocation = WindowStartupLocation.Manual;
window.Left = ;
window.Top = ;
window.ShowDialog();
}

Winform WPF 窗体显示位置的更多相关文章

  1. WinForm 设置窗体启动位置在活动屏幕右下角

    WinForm 设置窗体启动位置在活动屏幕右下角 在多屏幕环境下, 默认使用鼠标所在的屏幕 1. 设置窗体的 StartPosition 为 FormStartPosition.Manual. 2. ...

  2. WinForm开发,窗体显示和窗体传值相关知识总结

    主窗体中代码: public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void b ...

  3. WPF 窗体显示最前端

    原文:WPF 窗体显示最前端 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/jjx0224/article/details/8782845 如何做一 ...

  4. Winform将一个窗体显示在另一个窗体中

    private void ShowForm(Form Indexform) { Form1 form1 = new Form1(); form1 .TopLevel = false; form1 .P ...

  5. WPF C# 多屏情况下,实现窗体显示到指定的屏幕内

    原文:WPF C# 多屏情况下,实现窗体显示到指定的屏幕内 针对于一个程序,需要在两个显示屏上显示不同的窗体,(亦或N个显示屏N个窗体),可以使用如下的方式实现. 主要涉及到的:System.Wind ...

  6. 【转】WinForm窗体显示和窗体间传值

    以前对WinForm窗体显示和窗体间传值了解不是很清楚 最近做了一些WinForm项目,把用到的相关知识整理如下 A.WinForm中窗体显示 显示窗体可以有以下2种方法: Form.ShowDial ...

  7. C#:控制WinForm界面的显示

    控制WinForm界面在屏幕的四个角落显示,具体代码中有说明: using System; using System.Collections.Generic; using System.Drawing ...

  8. Winform窗口弹出位置控制

    窗体的弹出位置可以由属性StartPosition来指定,默认值有: Manural 自定义,由属性Location指定: CenterScreen 屏幕中央: WindowsDefaultBound ...

  9. WPF用ShowDialog()弹出窗体时控制该窗体的显示位置,并传值回父窗体

    原文:http://blog.csdn.net/kiss0622/article/details/5852153 方法一: 1.父窗口代码 Window1.xaml.cs private void B ...

随机推荐

  1. springmvc中freemarker的搭建

    在springmvc框架已经搭好的前提下,导入freemarker.jar,此处版本是2.3. 主要的配置工作都在spring-servlet.xml中,请看代码: <!--配置试图解析器 -- ...

  2. bootstrap3的 progress 进度条

    : 2.3版               3.0版 .bar .progress-bar .bar-* .progress-bar-* 2.代码: <!DOCTYPE html PUBLIC & ...

  3. hust 1570 Lazy. Lazy. Laaaaaaaaaaaazy!

    链接 1570 - Lazy. Lazy. Laaaaaaaaaaaazy! 题意 给出三种按键,caplock,shift,nomal(像正常键盘操作一样) ,输入三串字符串,s1,s2,txt, ...

  4. mysql 读写分离 ,mysql_proxy实现

    下载安装mysql_proxy: 解压后, 该目录包含已经编译好的二进制文件 . 1: 利用mysql_proxy实现负载均衡 执行mysql_proxy ./mysql-proxy-path/bin ...

  5. noip 2010 引水入城 贪心 + 搜索

    不难分析出如果有解则每个蓄水厂所能覆盖到的干旱城市一定是连续的.否则,中间那些没被覆盖的部分永远都不能被覆盖到. 当然,每个蓄水厂所覆盖的城市有可能不连续,不过既然有解,则一定都是连续的.我们可以开一 ...

  6. sh脚本文件

    echo `date +%F`  打印当前日期  用于写备份脚本按照名字命名

  7. P1892 团伙

    题目描述 1920年的芝加哥,出现了一群强盗.如果两个强盗遇上了,那么他们要么是朋友,要么是敌人.而且有一点是肯定的,就是: 我朋友的朋友是我的朋友: 我敌人的敌人也是我的朋友. 两个强盗是同一团伙的 ...

  8. nginx启动、关闭与重启命令

    nginx启动 1.启动nginx:./nginx  2.关闭nginx:./nginx  -s  stop 3.重启nginx:./nginx -s reload

  9. 01.Python基础-4.字符串

    1 字符串初识 字符串编码 字符串定义:多个字符(双引号或单引号中的数据)组成 字符串下标 类似列表list 格式化 就是占位符 最常用%s %d %f等等 转义字符 转义字符 描述 \ (在行尾时) ...

  10. [Beginning SharePoint Designer 2010]Chapter4 发布页面

    本章概要: 1.SharePoint中的Web内容管理 2.SharePoint发布系统的特性 3.SharePoint发布页面的组成 4.母板页 5.如何构建页面布局和他们潜在的内容类型