Winform知识积累:Winform窗体启动在屏幕中间位置
1 //C# winform窗口打开特效及窗口位置居中 2 //在启动一个程序时,我们希望窗口显示的位置处于屏幕的正中心,可以如下设置: 3 MainForm mainForm = new MainForm(); 4 mainForm.StartPosition = FormStartPosition.CenterScreen; 5 mainForm.Show(); 6 //如果在允许操作主窗口之前,必须先登录,则弹出登录窗口。此时主窗口出现在登录窗口后面,无法进行操作。 7 MainForm mainForm = new MainForm(); 8 LoginForm dlg=new LoginForm(); 9 dlg.ShowDialog(); 10 //这里ShowDialog方法表示你必须先操作完dlg窗口,才能操作后面的主窗体。 11 //如果要登录窗口显示在主窗口的中心,则在显示之前设置如下 12 dlg.StartPosition = FormStartPosition.CenterParent; 13 dlg.ShowDialog(); 14 //能够这样做的前提是主窗体必须先定义和显示。否则登录窗体可能无法找到父窗体。 15 //除此之外,也可以手动设置窗口显示的位置,即窗口坐标。 16 //首先必须把窗体的显示位置设置为手动。 17 dlg.StartPosition=FormStartPosition.Manual; 18 //随后获取屏幕的分辨率,也就是显示器屏幕的大小。 19 int xWidth = SystemInformation.PrimaryMonitorSize.Width;//获取显示器屏幕宽度 20 int yHeight = SystemInformation.PrimaryMonitorSize.Height;//高度 21 //然后定义窗口位置,以主窗体为例 22 mainForm.Location = new Point(xWidth/2, yHeight/2);//这里需要再减去窗体本身的宽度和高度的一半 23 mainForm.Show(); 24 //这样三步之后,一个准确定位在屏幕位置上的窗体就显示出来了。 25 //用Point类时,必须先把它包含进来,在程序最前面写上: 26 using System.Drawing; 27 //通过上面的一些简单介绍,您应该明白在C#中怎样设置窗体位置了吧 28 29 30 //出处:http://blog.csdn.net/qshpeng/article/details/1672359
1 //再看另外一个设置窗口打开的特效已经居中的方法,大家可以参考使用。 2 3 using System.Runtime.InteropServices; 4 public class Win32 5 { 6 public const Int32 AW_HOR_POSITIVE = 0x00000001; // 从左到右打开窗口 7 public const Int32 AW_HOR_NEGATIVE = 0x00000002; // 从右到左打开窗口 8 public const Int32 AW_VER_POSITIVE = 0x00000004; // 从上到下打开窗口 9 public const Int32 AW_VER_NEGATIVE = 0x00000008; // 从下到上打开窗口 10 public const Int32 AW_CENTER = 0x00000010; //若使用了AW_HIDE标志,则使窗口向内重叠;若未使用AW_HIDE标志,则使窗口向外扩展。 11 public const Int32 AW_HIDE = 0x00010000; //隐藏窗口,缺省则显示窗口。 12 public const Int32 AW_ACTIVATE = 0x00020000; //激活窗口。在使用了AW_HIDE标志后不要使用这个标志。 13 public const Int32 AW_SLIDE = 0x00040000; //使用滑动类型。缺省则为滚动动画类型。当使用AW_CENTER标志时,这个标志就被忽略。 14 public const Int32 AW_BLEND = 0x00080000; //使用淡出效果。只有当hWnd为顶层窗口的时候才可以使用此标志。 15 [DllImport("user32.dll", CharSet = CharSet.Auto)] 16 public static extern bool AnimateWindow( 17 IntPtr hwnd, // handle to window 18 int dwTime, // duration of animation 19 int dwFlags // animation type 20 ); 21 } 22 /**//*淡入窗体*/ 23 private void Form_Load(object sender, EventArgs e) 24 { 25 Win32.AnimateWindow(this.Handle, 2000, Win32.AW_BLEND); 26 } 27 /**//*淡出窗体*/ 28 private void Form_FormClosing(object sender, FormClosingEventArgs e) 29 { 30 Win32.AnimateWindow(this.Handle, 2000, Win32.AW_SLIDE | Win32.AW_HIDE | Win32.AW_BLEND); 31 } 32 33 /// <summary> 34 /// 页面居中 35 /// </summary> 36 public static void SetMid(Form form) 37 { 38 // Center the Form on the user's screen everytime it requires a Layout. 39 form.SetBounds((Screen.GetBounds(form).Width / 2) - (form.Width / 2), 40 (Screen.GetBounds(form).Height / 2) - (form.Height / 2), 41 form.Width, form.Height, BoundsSpecified.Location); 42 } 43 //这里是一个静态方法,参数为你需要设置居中的窗口对象。 44 //你可以在该窗口加载的时候加载这个静态方法实现你要的效果! 45 46 //http://blog.csdn.net/lwmjm/article/details/8085752
1 using System.Runtime.InteropServices; 2 public class Win32 3 { 4 public const Int32 AW_HOR_POSITIVE = 0x00000001; // 从左到右打开窗口 5 public const Int32 AW_HOR_NEGATIVE = 0x00000002; // 从右到左打开窗口 6 public const Int32 AW_VER_POSITIVE = 0x00000004; // 从上到下打开窗口 7 public const Int32 AW_VER_NEGATIVE = 0x00000008; // 从下到上打开窗口 8 public const Int32 AW_CENTER = 0x00000010; //若使用了AW_HIDE标志,则使窗口向内重叠;若未使用AW_HIDE标志,则使窗口向外扩展。 9 public const Int32 AW_HIDE = 0x00010000; //隐藏窗口,缺省则显示窗口。 10 public const Int32 AW_ACTIVATE = 0x00020000; //激活窗口。在使用了AW_HIDE标志后不要使用这个标志。 11 public const Int32 AW_SLIDE = 0x00040000; //使用滑动类型。缺省则为滚动动画类型。当使用AW_CENTER标志时,这个标志就被忽略。 12 public const Int32 AW_BLEND = 0x00080000; //使用淡出效果。只有当hWnd为顶层窗口的时候才可以使用此标志。 13 [DllImport("user32.dll", CharSet = CharSet.Auto)] 14 public static extern bool AnimateWindow( 15 IntPtr hwnd, // handle to window 16 int dwTime, // duration of animation 17 int dwFlags // animation type 18 ); 19 } 20 /**//*淡入窗体*/ 21 private void Form_Load(object sender, EventArgs e) 22 { 23 Win32.AnimateWindow(this.Handle, 2000, Win32.AW_BLEND); 24 } 25 /**//*淡出窗体*/ 26 private void Form_FormClosing(object sender, FormClosingEventArgs e) 27 { 28 Win32.AnimateWindow(this.Handle, 2000, Win32.AW_SLIDE | Win32.AW_HIDE | Win32.AW_BLEND); 29 }
1 //再看另外一个设置窗口打开的特效已经居中的方法,大家可以参考使用。 2 3 using System.Runtime.InteropServices; 4 public class Win32 5 { 6 public const Int32 AW_HOR_POSITIVE = 0x00000001; // 从左到右打开窗口 7 public const Int32 AW_HOR_NEGATIVE = 0x00000002; // 从右到左打开窗口 8 public const Int32 AW_VER_POSITIVE = 0x00000004; // 从上到下打开窗口 9 public const Int32 AW_VER_NEGATIVE = 0x00000008; // 从下到上打开窗口 10 public const Int32 AW_CENTER = 0x00000010; //若使用了AW_HIDE标志,则使窗口向内重叠;若未使用AW_HIDE标志,则使窗口向外扩展。 11 public const Int32 AW_HIDE = 0x00010000; //隐藏窗口,缺省则显示窗口。 12 public const Int32 AW_ACTIVATE = 0x00020000; //激活窗口。在使用了AW_HIDE标志后不要使用这个标志。 13 public const Int32 AW_SLIDE = 0x00040000; //使用滑动类型。缺省则为滚动动画类型。当使用AW_CENTER标志时,这个标志就被忽略。 14 public const Int32 AW_BLEND = 0x00080000; //使用淡出效果。只有当hWnd为顶层窗口的时候才可以使用此标志。 15 [DllImport("user32.dll", CharSet = CharSet.Auto)] 16 public static extern bool AnimateWindow( 17 IntPtr hwnd, // handle to window 18 int dwTime, // duration of animation 19 int dwFlags // animation type 20 ); 21 } 22 /**//*淡入窗体*/ 23 private void Form_Load(object sender, EventArgs e) 24 { 25 Win32.AnimateWindow(this.Handle, 2000, Win32.AW_BLEND); 26 } 27 /**//*淡出窗体*/ 28 private void Form_FormClosing(object sender, FormClosingEventArgs e) 29 { 30 Win32.AnimateWindow(this.Handle, 2000, Win32.AW_SLIDE | Win32.AW_HIDE | Win32.AW_BLEND); 31 } 32 33 /// <summary> 34 /// 页面居中 35 /// </summary> 36 public static void SetMid(Form form) 37 { 38 // Center the Form on the user's screen everytime it requires a Layout. 39 form.SetBounds((Screen.GetBounds(form).Width / 2) - (form.Width / 2), 40 (Screen.GetBounds(form).Height / 2) - (form.Height / 2), 41 form.Width, form.Height, BoundsSpecified.Location); 42 } 43 //这里是一个静态方法,参数为你需要设置居中的窗口对象。 44 //你可以在该窗口加载的时候加载这个静态方法实现你要的效果! 45 46 //http://blog.csdn.net/lwmjm/article/details/8085752
//https://www.cnblogs.com/baylor2019/p/13677254.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步