• Winform窗体启动在屏幕中间位置


    C# winform窗口打开特效及窗口位置居中
    在启动一个程序时,我们希望窗口显示的位置处于屏幕的正中心,可以如下设置:
     MainForm mainForm = new MainForm();
     mainForm.StartPosition = FormStartPosition.CenterScreen;
     mainForm.Show();
    如果在允许操作主窗口之前,必须先登录,则弹出登录窗口。此时主窗口出现在登录窗口后面,无法进行操作。
     MainForm mainForm = new MainForm();
     LoginForm dlg=new LoginForm();
     dlg.ShowDialog();
    这里ShowDialog方法表示你必须先操作完dlg窗口,才能操作后面的主窗体。
    如果要登录窗口显示在主窗口的中心,则在显示之前设置如下
     dlg.StartPosition = FormStartPosition.CenterParent;
     dlg.ShowDialog();
    能够这样做的前提是主窗体必须先定义和显示。否则登录窗体可能无法找到父窗体。
    除此之外,也可以手动设置窗口显示的位置,即窗口坐标。
    首先必须把窗体的显示位置设置为手动。
    dlg.StartPosition=FormStartPosition.Manual;
    随后获取屏幕的分辨率,也就是显示器屏幕的大小。
     int xWidth = SystemInformation.PrimaryMonitorSize.Width;//获取显示器屏幕宽度
     int yHeight = SystemInformation.PrimaryMonitorSize.Height;//高度
    然后定义窗口位置,以主窗体为例
     mainForm.Location = new Point(xWidth/2, yHeight/2);//这里需要再减去窗体本身的宽度和高度的一半
     mainForm.Show();
    这样三步之后,一个准确定位在屏幕位置上的窗体就显示出来了。
    用Point类时,必须先把它包含进来,在程序最前面写上:
     using System.Drawing;
    通过上面的一些简单介绍,您应该明白在C#中怎样设置窗体位置了吧
     
    
    出处:http://blog.csdn.net/qshpeng/article/details/1672359
    
    ====================================================================
    
    再看另外一个设置窗口打开的特效已经居中的方法,大家可以参考使用。
    
    复制代码
     using System.Runtime.InteropServices;
     public class Win32
     {
      public const Int32 AW_HOR_POSITIVE = 0x00000001; // 从左到右打开窗口
      public const Int32 AW_HOR_NEGATIVE = 0x00000002; // 从右到左打开窗口
      public const Int32 AW_VER_POSITIVE = 0x00000004; // 从上到下打开窗口
      public const Int32 AW_VER_NEGATIVE = 0x00000008; // 从下到上打开窗口
      public const Int32 AW_CENTER = 0x00000010; //若使用了AW_HIDE标志,则使窗口向内重叠;若未使用AW_HIDE标志,则使窗口向外扩展。
      public const Int32 AW_HIDE = 0x00010000; //隐藏窗口,缺省则显示窗口。
      public const Int32 AW_ACTIVATE = 0x00020000; //激活窗口。在使用了AW_HIDE标志后不要使用这个标志。
      public const Int32 AW_SLIDE = 0x00040000; //使用滑动类型。缺省则为滚动动画类型。当使用AW_CENTER标志时,这个标志就被忽略。
      public const Int32 AW_BLEND = 0x00080000; //使用淡出效果。只有当hWnd为顶层窗口的时候才可以使用此标志。
      [DllImport("user32.dll", CharSet = CharSet.Auto)]
      public static extern bool AnimateWindow(
        IntPtr hwnd, // handle to window
        int dwTime, // duration of animation
        int dwFlags // animation type
        );
     }
     /**//*淡入窗体*/
     private void Form_Load(object sender, EventArgs e)
     {
      Win32.AnimateWindow(this.Handle, 2000, Win32.AW_BLEND);
     }
     /**//*淡出窗体*/
     private void Form_FormClosing(object sender, FormClosingEventArgs e)
     {
      Win32.AnimateWindow(this.Handle, 2000, Win32.AW_SLIDE | Win32.AW_HIDE | Win32.AW_BLEND);
     }
    2,窗体居中 
    Code 
     
          /**//// <summary>
            /// 页面居中
            /// </summary>
            public static void SetMid(Form form)
            {
                // Center the Form on the user's screen everytime it requires a Layout.
                form.SetBounds((Screen.GetBounds(form).Width / 2) - (form.Width / 2),
                    (Screen.GetBounds(form).Height / 2) - (form.Height / 2),
                    form.Width, form.Height, BoundsSpecified.Location);
            }
    
    这里是一个静态方法,参数为你需要设置居中的窗口对象。你可以在该窗口加载的时候加载这个静态方法实现你要的效果!

    引用:http://blog.csdn.net/lwmjm/article/details/8085752
  • 相关阅读:
    字典的功能 (用在判断中的功能)
    PAT (Advanced Level) Practice 1144 The Missing Number (20分)
    LCA算法实际应用 PAT (Advanced Level) Practice 1151 LCA in a Binary Tree (30分) 四种方法完成题目+tarjan详细讲解!
    记笔记最好用的超高颜值软件之一!Typora 你值得拥有!
    PAT (Advanced Level) Practice 1150 Travelling Salesman Problem (25分) (正常思路)
    PAT (Advanced Level) Practice 1154 Vertex Coloring (25分) (直接存边遍历)
    PAT (Basic Level) Practice (中文) 1095 解码PAT准考证 (25分) (unordered_map的使用 多做几遍!!)
    PAT (Basic Level) Practice (中文) 1093 字符串A+B (20分)
    PAT (Basic Level) Practice (中文)1092 最好吃的月饼 (20分)
    51Nod 1192 Gcd表中的质数
  • 原文地址:https://www.cnblogs.com/baylor2019/p/13677254.html
Copyright © 2020-2023  润新知