詳解C#如何手動改變自制窗體的大小
當(dāng)用戶去除Winform窗體邊框,自行設(shè)置窗體外觀時,用戶就不能使用Windows窗體應(yīng)用的功能對自定義窗體的大小進行隨意改變了。
此時手動改變無邊框窗體的大小需要自定義改變窗體大小的方法,才能實現(xiàn)通過鼠標(biāo)拖拽自定義窗體的右邊、下邊、右下角改變窗體的大小。改變自制窗體的大小時,主要用到了Cursor類的Position屬性以及窗體的Width、Height、Top和Left屬性,另外,還用到了改變窗體大小的計算方法。
1.Cursor類的Position屬性
Cursor類代表用于繪制鼠標(biāo)指針的圖像,其Position屬性用于獲取或設(shè)置光標(biāo)位置。該屬性的語法格式如下:
public static Point Position {get;set;}
參數(shù)說明
屬性值:代表光標(biāo)位置的Point(采用屏幕坐標(biāo))結(jié)構(gòu)。
2.改變窗體大小的計算方法
重要的算法就是如何根據(jù)鼠標(biāo)的移動來設(shè)置窗體的大?。菏紫龋卯?dāng)前鼠標(biāo)在屏幕上的X坐標(biāo)值減去窗體左邊距與屏幕左邊距的距離,然后再加上鼠標(biāo)與邊框右端的距離,并將值設(shè)為當(dāng)前窗體的寬度。實現(xiàn)代碼如下:
Form.Width =Cursor.Position.X-Frm.Left+(Pan.Width -Example_X);
其中,F(xiàn)orm為窗體名稱,Pan為顯示窗體邊框的控件名稱,Example_X為鼠標(biāo)按下時的X坐標(biāo)值。說明:以上計算方法是在Panel控件的MouseMove事件中執(zhí)行的。
3.Resources設(shè)計
(1)Resources資源圖片管理
(2)GetObject方法設(shè)計
圖片資源的屬性是自動生成的,但是在程序中獲取或加載圖片的GetObject方法需要手動設(shè)計進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 // // 對此文件的更改可能會導(dǎo)致不正確的行為,并且如果 // 重新生成代碼,這些更改將會丟失。 // </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> /// 重寫當(dāng)前線程的 CurrentUICulture 屬性,對 /// 使用此強類型資源類的所有資源查找執(zhí)行重寫。 /// </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;//獲取當(dāng)前鼠標(biāo)的屏幕坐標(biāo) myPosittion.Offset(CPoint.X, CPoint.Y);//重載當(dāng)前鼠標(biāo)的位置 Frm.DesktopLocation = myPosittion;//設(shè)置當(dāng)前窗體在屏幕上的位置 } } #endregion #region 獲取鼠標(biāo)的當(dāng)前位置 /// <summary> /// 獲取鼠標(biāo)的當(dāng)前位置 /// </summary> /// <param Frm="Form">窗體</param> /// <param e="MouseEventArgs">窗體上有關(guān)鼠標(biāo)的一些信息</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 改變窗體的大小(用于鼠標(biāo)的移動事件) /// <summary> /// 改變窗體的大小(用于鼠標(biāo)的移動事件) /// </summary> /// <param Frm="Form">窗體</param> /// <param Pan="Panel">設(shè)置窗體邊框的控件</param> /// <param e="MouseEventArgs">窗體上有關(guān)鼠標(biāo)的一些信息</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; //設(shè)置窗體的寬度為70 //如果用鼠標(biāo)向右移動窗體的右邊框 if (Cursor.Position.X - Frm.Left + (Pan.Width - Example_X) > Frm.Width) { //根據(jù)鼠標(biāo)的移動值,增加窗體的寬度 Frm.Width = Cursor.Position.X - Frm.Left + (Pan.Width - Example_X); } break; } //根據(jù)鼠標(biāo)的移動值,增加窗體的寬度 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)) { //根據(jù)鼠標(biāo)的移動改變窗體的大小 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; //設(shè)置窗體的寬度為70 if (Height <= (panel_Title!.Height + panel_BottomMiddle!.Height + 1))//如果窗體的高小于最小值 { Frm.Height = panel_Title.Height + panel_BottomMiddle.Height + 1;//設(shè)置窗體的最小高度 //如果用鼠標(biāo)向下移動窗體的底邊框 if (Cursor.Position.Y - Frm.Top + (Pan.Height - Example_Y) > Frm.Height) { //根據(jù)鼠標(biāo)的移動值,增加窗體的高度 Frm.Height = Cursor.Position.Y - Frm.Top + (Pan.Height - Example_Y); } break; } //如果用鼠標(biāo)向右移動窗體 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;//設(shè)置窗體的高度為最小值 Frm.Width = Cursor.Position.X - Frm.Left + (Pan.Width - Example_X);//改變窗體的寬度 //如果用鼠標(biāo)向下移動窗體的邊框 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;//設(shè)置窗體的高度為最小值 //如果用鼠標(biāo)向下移動窗體的下邊框 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);//獲取鼠標(biāo)的當(dāng)前位置 } 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)//如果移動的是標(biāo)題欄的中間部分 Tem_X = -e.X - panel_TitleLeft!.Width; if (Convert.ToInt16(((Panel)sender).Tag!.ToString()) == 3)//如果移動的是標(biāo)題欄的尾端 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); } } }
到此這篇關(guān)于詳解C#如何手動改變自制窗體的大小的文章就介紹到這了,更多相關(guān)C#手動改變窗體大小內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解析C#設(shè)計模式編程中外觀模式Facade Pattern的應(yīng)用
這篇文章主要介紹了C#設(shè)計模式編程中外觀模式Facade Pattern的應(yīng)用,外觀模式中分為門面(Facade)和子系統(tǒng)(subsystem)兩個角色來進行實現(xiàn),需要的朋友可以參考下2016-02-02