详解C#如何手动改变自制窗体的大小
当用户去除Winform窗体边框,自行设置窗体外观时,用户就不能使用Windows窗体应用的功能对自定义窗体的大小进行随意改变了。
此时手动改变无边框窗体的大小需要自定义改变窗体大小的方法,才能实现通过鼠标拖拽自定义窗体的右边、下边、右下角改变窗体的大小。改变自制窗体的大小时,主要用到了Cursor类的Position属性以及窗体的Width、Height、Top和Left属性,另外,还用到了改变窗体大小的计算方法。
1.Cursor类的Position属性
Cursor类代表用于绘制鼠标指针的图像,其Position属性用于获取或设置光标位置。该属性的语法格式如下:
public static Point Position {get;set;}
参数说明
属性值:代表光标位置的Point(采用屏幕坐标)结构。
2.改变窗体大小的计算方法
重要的算法就是如何根据鼠标的移动来设置窗体的大小:首先,用当前鼠标在屏幕上的X坐标值减去窗体左边距与屏幕左边距的距离,然后再加上鼠标与边框右端的距离,并将值设为当前窗体的宽度。实现代码如下:
Form.Width =Cursor.Position.X-Frm.Left+(Pan.Width -Example_X);
其中,Form为窗体名称,Pan为显示窗体边框的控件名称,Example_X为鼠标按下时的X坐标值。说明:以上计算方法是在Panel控件的MouseMove事件中执行的。
3.Resources设计
(1)Resources资源图片管理
(2)GetObject方法设计
图片资源的属性是自动生成的,但是在程序中获取或加载图片的GetObject方法需要手动设计进Resources.Designer.cs中。
internal static Image GetObject(string v) { return v switch { "panel_TitleLeft" => panel_TitleLeft, "panel_TitleMiddle" => panel_TitleMiddle, "panel_TitleRight" => panel_TitleRight, "panel_Left" => panel_Left, "panel_Right" => panel_Right, "panel_BottomLeft" => panel_BottomLeft, "panel_BottomMiddle" => panel_BottomMiddle, "panel_BottomRight" => panel_BottomRight, "pictureBox1_Image" => pictureBox1_Image, _ => null }; }
// Resources.Designer.cs //------------------------------------------------------------------------------ // <auto-generated> // 此代码由工具生成。 // 运行时版本:4.0.30319.42000 // // 对此文件的更改可能会导致不正确的行为,并且如果 // 重新生成代码,这些更改将会丢失。 // </auto-generated> //------------------------------------------------------------------------------ namespace _164.Properties { using System; /// <summary> /// 一个强类型的资源类,用于查找本地化的字符串等。 /// </summary> // 此类是由 StronglyTypedResourceBuilder // 类通过类似于 ResGen 或 Visual Studio 的工具自动生成的。 // 若要添加或移除成员,请编辑 .ResX 文件,然后重新运行 ResGen // (以 /str 作为命令选项),或重新生成 VS 项目。 [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "17.0.0.0")] [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] internal class Resources { private static global::System.Resources.ResourceManager resourceMan; private static global::System.Globalization.CultureInfo resourceCulture; [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] internal Resources() { } /// <summary> /// 返回此类使用的缓存的 ResourceManager 实例。 /// </summary> [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] internal static global::System.Resources.ResourceManager ResourceManager { get { if (object.ReferenceEquals(resourceMan, null)) { global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("_164.Properties.Resources", typeof(Resources).Assembly); resourceMan = temp; } return resourceMan; } } /// <summary> /// 重写当前线程的 CurrentUICulture 属性,对 /// 使用此强类型资源类的所有资源查找执行重写。 /// </summary> [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] internal static global::System.Globalization.CultureInfo Culture { get { return resourceCulture; } set { resourceCulture = value; } } /// <summary> /// 查找 System.Drawing.Bitmap 类型的本地化资源。 /// </summary> internal static System.Drawing.Bitmap panel_BottomLeft { get { object obj = ResourceManager.GetObject("panel_BottomLeft", resourceCulture); return ((System.Drawing.Bitmap)(obj)); } } /// <summary> /// 查找 System.Drawing.Bitmap 类型的本地化资源。 /// </summary> internal static System.Drawing.Bitmap panel_BottomMiddle { get { object obj = ResourceManager.GetObject("panel_BottomMiddle", resourceCulture); return ((System.Drawing.Bitmap)(obj)); } } /// <summary> /// 查找 System.Drawing.Bitmap 类型的本地化资源。 /// </summary> internal static System.Drawing.Bitmap panel_BottomRight { get { object obj = ResourceManager.GetObject("panel_BottomRight", resourceCulture); return ((System.Drawing.Bitmap)(obj)); } } /// <summary> /// 查找 System.Drawing.Bitmap 类型的本地化资源。 /// </summary> internal static System.Drawing.Bitmap panel_Left { get { object obj = ResourceManager.GetObject("panel_Left", resourceCulture); return ((System.Drawing.Bitmap)(obj)); } } /// <summary> /// 查找 System.Drawing.Bitmap 类型的本地化资源。 /// </summary> internal static System.Drawing.Bitmap panel_Right { get { object obj = ResourceManager.GetObject("panel_Right", resourceCulture); return ((System.Drawing.Bitmap)(obj)); } } /// <summary> /// 查找 System.Drawing.Bitmap 类型的本地化资源。 /// </summary> internal static System.Drawing.Bitmap panel_TitleLeft { get { object obj = ResourceManager.GetObject("panel_TitleLeft", resourceCulture); return ((System.Drawing.Bitmap)(obj)); } } /// <summary> /// 查找 System.Drawing.Bitmap 类型的本地化资源。 /// </summary> internal static System.Drawing.Bitmap panel_TitleMiddle { get { object obj = ResourceManager.GetObject("panel_TitleMiddle", resourceCulture); return ((System.Drawing.Bitmap)(obj)); } } /// <summary> /// 查找 System.Drawing.Bitmap 类型的本地化资源。 /// </summary> internal static System.Drawing.Bitmap panel_TitleRight { get { object obj = ResourceManager.GetObject("panel_TitleRight", resourceCulture); return ((System.Drawing.Bitmap)(obj)); } } /// <summary> /// 查找 System.Drawing.Bitmap 类型的本地化资源。 /// </summary> internal static System.Drawing.Bitmap pictureBox1_Image { get { object obj = ResourceManager.GetObject("pictureBox1_Image", resourceCulture); return ((System.Drawing.Bitmap)(obj)); } } internal static Image GetObject(string v) { return v switch { "panel_TitleLeft" => panel_TitleLeft, "panel_TitleMiddle" => panel_TitleMiddle, "panel_TitleRight" => panel_TitleRight, "panel_Left" => panel_Left, "panel_Right" => panel_Right, "panel_BottomLeft" => panel_BottomLeft, "panel_BottomMiddle" => panel_BottomMiddle, "panel_BottomRight" => panel_BottomRight, "pictureBox1_Image" => pictureBox1_Image, _ => null }; } } }
4.示例
// 手动改变自制窗体的大小 using _164.Properties; namespace _164 { public partial class Form1 : Form { private Panel? panel_BR; private Panel? panel_Center; private Panel? panel_TitleRight; private Panel? panel_BottomLeft; private Panel? panel_BottomMiddle; private PictureBox? pictureBox1; private Panel? panel_TitleLeft; private Panel? panel_Right; private Panel? panel_Left; private Panel? panel_Title; private Panel? panel_Bottom; private Panel? panel_TitleMiddle; private static int example_X = 0; private static int example_Y = 0; private static int example_W = 0; private static Point cPoint; public static int Example_X { get => example_X; set => example_X = value; } public static int Example_Y { get => example_Y; set => example_Y = value; } public static int Example_W { get => example_W; set => example_W = value; } public static Point CPoint { get => cPoint; set => cPoint = value; } public Form1() { InitializeComponent(); Load += Form1_Load; } private void Form1_Load(object? sender, EventArgs e) { // // panel_Right // panel_Right = new Panel { BackColor = Color.Transparent, BackgroundImage = Resources.GetObject("panel_Right")!, BackgroundImageLayout = ImageLayout.Stretch, Cursor = Cursors.SizeWE, Dock = DockStyle.Right, Location = new Point(286, 19), Name = "panel_Right", Size = new Size(6, 96), TabIndex = 10 }; panel_Right.MouseDown += new MouseEventHandler(Panel_Right_MouseDown!); panel_Right.MouseMove += new MouseEventHandler(Panel_Right_MouseMove!); // // panel_Left // panel_Left = new Panel { BackColor = Color.Transparent, BackgroundImage = Resources.GetObject("panel_Left")!, BackgroundImageLayout = ImageLayout.Stretch, Dock = DockStyle.Left, Location = new Point(0, 19), Name = "panel_Left", Size = new Size(6, 96), TabIndex = 9 }; panel_Left.MouseDown += new MouseEventHandler(Panel_Right_MouseDown!); panel_Left.MouseMove += new MouseEventHandler(Panel_Right_MouseMove!); // // panel_Center // panel_Center = new Panel { BackColor = Color.Honeydew, Dock = DockStyle.Fill, Location = new Point(6, 19), Name = "panel_Center", Size = new Size(280, 96), TabIndex = 13 }; // // panel_TitleRight // panel_TitleRight = new Panel { BackColor = Color.Transparent, BackgroundImage = Resources.GetObject("panel_TitleRight")!, BackgroundImageLayout = ImageLayout.Stretch, Dock = DockStyle.Right, Location = new Point(284, 0), Name = "panel_TitleRight", Size = new Size(8, 19), TabIndex = 1, Tag = "3" }; panel_TitleRight.MouseDown += new MouseEventHandler(Panel_TitleLeft_MouseDown!); panel_TitleRight.MouseMove += new MouseEventHandler(Panel_TitleLeft_MouseMove!); // // panel_TitleMiddle // panel_TitleMiddle = new Panel { BackColor = Color.Transparent, BackgroundImage = Resources.GetObject("panel_TitleMiddle")!, BackgroundImageLayout = ImageLayout.Stretch, Dock = DockStyle.Fill, Location = new Point(12, 0), Name = "panel_TitleMiddle", Size = new Size(272, 19), TabIndex = 2, Tag = "2" }; panel_TitleMiddle.Controls.Add(pictureBox1); panel_TitleMiddle.MouseDown += new MouseEventHandler(Panel_TitleLeft_MouseDown!); panel_TitleMiddle.MouseMove += new MouseEventHandler(Panel_TitleLeft_MouseMove!); panel_TitleMiddle.SuspendLayout(); // // pictureBox1 // pictureBox1 = new PictureBox { Anchor = AnchorStyles.Top | AnchorStyles.Right, BackColor = Color.Transparent, Cursor = Cursors.Hand, Image = Resources.GetObject("pictureBox1.Image")!, SizeMode = PictureBoxSizeMode.StretchImage, Location = new Point(260, 3), Name = "pictureBox1", Size = new Size(11, 11), TabIndex = 0, TabStop = false, Tag = "11" }; pictureBox1.Click += new EventHandler(PictureBox1_Click!); // // panel_TitleLeft // panel_TitleLeft = new Panel { BackColor = Color.Transparent, BackgroundImage = Resources.GetObject("panel_TitleLeft")!, BackgroundImageLayout = ImageLayout.Stretch, Dock = DockStyle.Left, Location = new Point(0, 0), Name = "panel_TitleLeft", Size = new Size(12, 19), TabIndex = 0, Tag = "1" }; panel_TitleLeft.MouseDown += new MouseEventHandler(Panel_TitleLeft_MouseDown!); panel_TitleLeft.MouseMove += new MouseEventHandler(Panel_TitleLeft_MouseMove!); // // panel_BottomLeft // panel_BottomLeft = new Panel { BackColor = Color.Transparent, BackgroundImage =Resources.GetObject("panel_BottomLeft")!, BackgroundImageLayout = ImageLayout.Stretch, Cursor = Cursors.SizeNS, Dock = DockStyle.Left, Location = new Point(0, 0), Name = "panel_BottomLeft", Size = new Size(8, 10), TabIndex = 0 }; // // panel_BottomMiddle // panel_BottomMiddle = new Panel { BackColor = Color.Transparent, BackgroundImage = Resources.GetObject("panel_BottomMiddle")!, BackgroundImageLayout = ImageLayout.Stretch, Cursor = Cursors.SizeNS, Dock = DockStyle.Fill, Location = new Point(8, 0), Name = "panel_Bn", Size = new Size(276, 10), TabIndex = 2 }; panel_BottomMiddle.MouseDown += new MouseEventHandler(Panel_Right_MouseDown!); panel_BottomMiddle.MouseMove += new MouseEventHandler(Panel_Right_MouseMove!); // // panel_BottomRight // panel_BR = new Panel { BackColor = Color.Transparent, BackgroundImage = Resources.GetObject("panel_BottomRight")!, BackgroundImageLayout = ImageLayout.Stretch, Cursor = Cursors.SizeNWSE, Dock = DockStyle.Right, Location = new Point(284, 0), Name = "panel_BR", Size = new Size(8, 10), TabIndex = 1 }; panel_BR.MouseDown += new MouseEventHandler(Panel_Right_MouseDown!); panel_BR.MouseMove += new MouseEventHandler(Panel_Right_MouseMove!); // // panel_Title // panel_Title = new Panel { BackColor = Color.Transparent, Dock = DockStyle.Top, Location = new Point(0, 0), Name = "panel_Title", Size = new Size(292, 19), TabIndex = 7 }; panel_Title.Controls.Add(panel_TitleMiddle); panel_Title.Controls.Add(panel_TitleRight); panel_Title.Controls.Add(panel_TitleLeft); panel_Title.SuspendLayout(); // // panel_Bottom // panel_Bottom = new Panel { BackColor = Color.Transparent, Dock = DockStyle.Bottom, Location = new Point(0, 115), Name = "panel_Bottom", Size = new Size(292, 10), TabIndex = 8 }; panel_Bottom.Controls.Add(panel_BottomMiddle); panel_Bottom.Controls.Add(panel_BR); panel_Bottom.Controls.Add(panel_BottomLeft); panel_Bottom.SuspendLayout(); // // Form1 // AutoScaleDimensions = new SizeF(6F, 12F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(292, 125); Controls.Add(panel_Center); Controls.Add(panel_Right); Controls.Add(panel_Left); Controls.Add(panel_Title); Controls.Add(panel_Bottom); FormBorderStyle = FormBorderStyle.None; Name = "Form1"; Text = "Form1"; panel_BottomMiddle.ResumeLayout(false); panel_Title.ResumeLayout(false); panel_Bottom.ResumeLayout(false); } #region 利用窗体上的控件移动窗体 /// <summary> /// 利用控件移动窗体 /// </summary> /// <param Frm="Form">窗体</param> /// <param e="MouseEventArgs">控件的移动事件</param> public static void FormMove(Form Frm, MouseEventArgs e) //Form或MouseEventArgs添加命名空间using System.Windows.Forms; { if (e.Button == MouseButtons.Left) { Point myPosittion = Control.MousePosition;//获取当前鼠标的屏幕坐标 myPosittion.Offset(CPoint.X, CPoint.Y);//重载当前鼠标的位置 Frm.DesktopLocation = myPosittion;//设置当前窗体在屏幕上的位置 } } #endregion #region 获取鼠标的当前位置 /// <summary> /// 获取鼠标的当前位置 /// </summary> /// <param Frm="Form">窗体</param> /// <param e="MouseEventArgs">窗体上有关鼠标的一些信息</param> public static void FormScreenSizeInfo(Form Frm, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { Example_X = e.X; Example_Y = e.Y; Example_W = Frm.Width; } } #endregion #region 改变窗体的大小(用于鼠标的移动事件) /// <summary> /// 改变窗体的大小(用于鼠标的移动事件) /// </summary> /// <param Frm="Form">窗体</param> /// <param Pan="Panel">设置窗体边框的控件</param> /// <param e="MouseEventArgs">窗体上有关鼠标的一些信息</param> public void FormScreen_EnlargeSize(Form Frm, Panel Pan, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { switch (Pan.Name) { case "panel_Right": //如果移动的是窗体的右边框 { if (Width <= 70) //如果窗体的宽度小于等于70 { Frm.Width = 70; //设置窗体的宽度为70 //如果用鼠标向右移动窗体的右边框 if (Cursor.Position.X - Frm.Left + (Pan.Width - Example_X) > Frm.Width) { //根据鼠标的移动值,增加窗体的宽度 Frm.Width = Cursor.Position.X - Frm.Left + (Pan.Width - Example_X); } break; } //根据鼠标的移动值,增加窗体的宽度 Frm.Width = Cursor.Position.X - Frm.Left + (Pan.Width - Example_X); break; } case "panel_BR": //如果移动的是窗体的右下角 { //如果窗体的大小不为窗体大小的最小值 if (Width > 70 && Height > (panel_Title!.Height + panel_BottomMiddle!.Height + 1)) { //根据鼠标的移动改变窗体的大小 Frm.Height = Cursor.Position.Y - Frm.Top + (Pan.Height - Example_Y); Frm.Width = Cursor.Position.X - Frm.Left + (Pan.Width - Example_X); } else { if (Width <= 70) //如果窗体的宽度小于等于最小值 { Frm.Width = 70; //设置窗体的宽度为70 if (Height <= (panel_Title!.Height + panel_BottomMiddle!.Height + 1))//如果窗体的高小于最小值 { Frm.Height = panel_Title.Height + panel_BottomMiddle.Height + 1;//设置窗体的最小高度 //如果用鼠标向下移动窗体的底边框 if (Cursor.Position.Y - Frm.Top + (Pan.Height - Example_Y) > Frm.Height) { //根据鼠标的移动值,增加窗体的高度 Frm.Height = Cursor.Position.Y - Frm.Top + (Pan.Height - Example_Y); } break; } //如果用鼠标向右移动窗体 if (Cursor.Position.X - Frm.Left + (Pan.Width - Example_X) > Frm.Width) { //增加窗体的宽度 Frm.Width = Cursor.Position.X - Frm.Left + (Pan.Width - Example_X); } break; } if (Height <= (panel_Title!.Height + panel_BottomMiddle!.Height + 1))//如果窗体的高度小于等于最小值 { Frm.Height = panel_Title.Height + panel_BottomMiddle.Height + 1;//设置窗体的高度为最小值 Frm.Width = Cursor.Position.X - Frm.Left + (Pan.Width - Example_X);//改变窗体的宽度 //如果用鼠标向下移动窗体的边框 if (Cursor.Position.Y - Frm.Top + (Pan.Height - Example_Y) > Frm.Height) { Frm.Height = Cursor.Position.Y - Frm.Top + (Pan.Height - Example_Y);//增加窗体的高度 } break; } } break; } case "panel_Bn"://如果移动的是窗体的底边框 { if (Height <= (panel_Title!.Height + panel_BottomMiddle!.Height + 1))//如果窗体的高度小于等于最小值 { Frm.Height = panel_Title.Height + panel_BottomMiddle.Height + 1;//设置窗体的高度为最小值 //如果用鼠标向下移动窗体的下边框 if (Cursor.Position.Y - Frm.Top + (Pan.Height - Example_Y) > Frm.Height) { Frm.Height = Cursor.Position.Y - Frm.Top + (Pan.Height - Example_Y); //增加窗体的高度 } break; } Frm.Height = Cursor.Position.Y - Frm.Top + (Pan.Height - Example_Y); //增加窗体的高度 break; } } } } #endregion private void Panel_Right_MouseDown(object sender, MouseEventArgs e) { FormScreenSizeInfo(this, e);//获取鼠标的当前位置 } private void Panel_Right_MouseMove(object sender, MouseEventArgs e) { FormScreen_EnlargeSize(this, (Panel)sender, e);//改变窗体的大小 } private void PictureBox1_Click(object sender, EventArgs e) { Close(); } private void Panel_TitleLeft_MouseDown(object sender, MouseEventArgs e) { int Tem_X = -e.X; if (Convert.ToInt16(((Panel)sender).Tag!.ToString()) == 2)//如果移动的是标题栏的中间部分 Tem_X = -e.X - panel_TitleLeft!.Width; if (Convert.ToInt16(((Panel)sender).Tag!.ToString()) == 3)//如果移动的是标题栏的尾端 Tem_X = -(Width - ((Panel)sender).Width) - e.X; CPoint = new Point(Tem_X, -e.Y); } private void Panel_TitleLeft_MouseMove(object sender, MouseEventArgs e) { FormMove(this, e); } } }
到此这篇关于详解C#如何手动改变自制窗体的大小的文章就介绍到这了,更多相关C#手动改变窗体大小内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
最新评论