【C#】WinForm自定义控件及窗体
				
									
					
					
						|  | 
							admin 2025年3月29日 0:10
								本文热度 1604 | 
					
				 
				| WinForm(Windows Forms)是Microsoft.NET框架中的技术,用于开发Windows桌面应用程序。它提供了一套丰富的控件和组件。    通过拖放控件、编写事件处理程序等方式快速构建用户界面。通过属性窗口定制这些控件的外观和行为。    通过数据绑定,将UI控件与数据源连接,实现数据的展示和更新。    通过上面的方法可以帮助开发者高效地创建桌面窗体应用程序,尤其适合初学者和需要快速开发的项目。 | 
本文介绍了如何创建Winform窗体,并自定义窗体样式,及窗体基本功能。

public class UCButton : Button{    #region  公共字段、属性    private bool _selectedState = false;
    [Category("UserProperty")]    [Description("选中状态")]    public bool SelectedState    {        get => _selectedState;        private set        {            _selectedState = value;            this.Invalidate();        }    }
    private int radius = 15;
    [Category("UserProperty")]    [Description("圆角半径")]    public int Radius    {        get { return radius; }        set        {            radius = value;            this.Invalidate();        }    }    private Color _defaultColor;
    private Color _hoverColor = Color.LightBlue;
    [Category("UserProperty")]    [Description("鼠标悬停时的背景色")]    public Color HoverColor     {         get => _hoverColor;         set => _hoverColor = value;     }    #endregion    public UCButton()    {        Initialize();    }    private void Initialize()    {        this.DoubleBuffered = true;        this.FlatStyle = FlatStyle.Flat;        this.FlatAppearance.BorderSize = 0;        this.SetStyle(ControlStyles.UserPaint            | ControlStyles.AllPaintingInWmPaint            | ControlStyles.OptimizedDoubleBuffer            | ControlStyles.ResizeRedraw            | ControlStyles.SupportsTransparentBackColor, true);        _defaultColor = BackColor;
        this.MouseEnter += UCButton_MouseEnter;        this.MouseLeave += UCButton_MouseLeave;    }    private void UCButton_MouseEnter(object sender, EventArgs e)    {        this.BackColor = HoverColor;     }    private void UCButton_MouseLeave(object sender, EventArgs e)    {        this.BackColor = _defaultColor;     }    protected override void OnClick(EventArgs e)    {        base.OnClick(e);        _selectedState = !_selectedState;    }    protected override void OnPaint(PaintEventArgs e)    {        base.OnPaint(e);        e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;                             e.Graphics.CompositingQuality = CompositingQuality.HighQuality;                 e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;                    using (GraphicsPath path = new GraphicsPath())        {            path.AddArc(0, 0, radius, radius, 180, 90);                                     path.AddArc(this.Width - radius, 0, radius, radius, 270, 90);                   path.AddArc(this.Width - radius, this.Height - radius, radius, radius, 0, 90);              path.AddArc(0, this.Height - radius, radius, radius, 90, 90);                               path.CloseFigure();
            this.Region = new Region(path);         }                using (Brush brush = new SolidBrush(this.ForeColor))        {            SizeF textSize = e.Graphics.MeasureString(this.Text, this.Font);            PointF textLocation = new PointF((this.Width - textSize.Width) / 2, (this.Height - textSize.Height) / 2);            e.Graphics.DrawString(this.Text, this.Font, brush, textLocation);        }    }}
public partial class MainForm : Form{    private int ButtonWidth = 62;
    #region 窗体初始化、加载、关闭    public MainForm()    {        InitializeComponent();        this.CenterToParent();        this.CenterToScreen();    }    private void MainForm_Load(object sender, System.EventArgs e)    {        WinMoveBinding(panel_TopBorderItem);        WinMoveBinding(pic_WinIcon);        this.WindowState = FormWindowState.Normal;        this.MinimumSize = new System.Drawing.Size(150, 150);        panel_MenuItemText.Hide();        ButtonWidth = btn_Expand.Width;    }    private void MainForm_FormClosing(object sender, FormClosingEventArgs e)    {
    }    #endregion
                private void WinMoveBinding(Control control)    {        control.MouseDown += topBorderPanel_MouseDown;        control.MouseMove += topBorderPanel_MouseMove;        control.MouseUp += topBorderPanel_MouseUp;    }
    #region 窗体拖动    private Point mouseOffset;    private void topBorderPanel_MouseDown(object sender, MouseEventArgs e)    {        if (e.Button == MouseButtons.Left)        {            mouseOffset = new Point(-e.X, -e.Y);        }    }
    private void topBorderPanel_MouseMove(object sender, MouseEventArgs e)    {        if (e.Button == MouseButtons.Left)        {            Point mousePos = Control.MousePosition;            mousePos.Offset(mouseOffset.X, mouseOffset.Y);            this.Location = mousePos;        }    }
    private void topBorderPanel_MouseUp(object sender, MouseEventArgs e)    {        mouseOffset = Point.Empty;    }    #endregion
    #region 无边框窗体随意拖动和改变尺寸    const int WM_NCHITTEST = 0x0084;    const int HTLEFT = 10;    const int HTRIGHT = 11;    const int HTTOP = 12;    const int HTTOPLEFT = 13;    const int HTTOPRIGHT = 14;    const int HTBOTTOM = 15;    const int HTBOTTOMLEFT = 0x10;    const int HTBOTTOMRIGHT = 17;    protected override void WndProc(ref Message m)    {        base.WndProc(ref m);        switch (m.Msg)        {            case WM_NCHITTEST:                Point vPoint = new Point((int)m.LParam & 0xFFFF,                    (int)m.LParam >> 16 & 0xFFFF);                vPoint = PointToClient(vPoint);                if (vPoint.X <= 5)                    if (vPoint.Y <= 5)                        m.Result = (IntPtr)HTTOPLEFT;                    else if (vPoint.Y >= ClientSize.Height - 5)                        m.Result = (IntPtr)HTBOTTOMLEFT;                    else m.Result = (IntPtr)HTLEFT;                else if (vPoint.X >= ClientSize.Width - 5)                    if (vPoint.Y <= 5)                        m.Result = (IntPtr)HTTOPRIGHT;                    else if (vPoint.Y >= ClientSize.Height - 5)                        m.Result = (IntPtr)HTBOTTOMRIGHT;                    else m.Result = (IntPtr)HTRIGHT;                else if (vPoint.Y <= 5)                    m.Result = (IntPtr)HTTOP;                else if (vPoint.Y >= ClientSize.Height - 5)                    m.Result = (IntPtr)HTBOTTOM;                break;        }    }    #endregion
    #region 窗体关闭、最大化、最小化    private void btn_ClosingWindow_Click(object sender, System.EventArgs e)    {        if (MessageBox.Show("是否关闭窗体!", "关闭", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK)        {            this.Close();        }    }
    private void btn_Maximize_Click(object sender, System.EventArgs e)    {        Button button = sender as Button;        if (this.WindowState == FormWindowState.Maximized)        {            this.WindowState = FormWindowState.Normal;            button.Image = global::ModbusDemo.Properties.Resources.maximize_blue_32;        }        else        {            this.WindowState = FormWindowState.Maximized;            button.Image = global::ModbusDemo.Properties.Resources.restore_blue_32;
        }    }
    private void btn_Minimize_Click(object sender, System.EventArgs e)    {        this.WindowState = FormWindowState.Minimized;    }
    #endregion
                private void btn_Expand_Click(object sender, System.EventArgs e)    {                if (!btn_Expand.SelectedState)        {            btn_Expand.Image = global::ModbusDemo.Properties.Resources.collapse_left_blue_32;            panel_MenuItemIcon.Width = ButtonWidth;            panel_MenuItemText.ScrollControlIntoView(btn_Expand);            panel_MenuItemText.Show();            panel_LeftMenuItem.Width = 256;        }                else        {            btn_Expand.Image = global::ModbusDemo.Properties.Resources.collapse_right_blue_32;
            panel_MenuItemIcon.Width = ButtonWidth;            panel_LeftMenuItem.Width = ButtonWidth;            panel_MenuItemText.Hide();        }    }
                private void btn_Home_Click(object sender, EventArgs e)    {
    }}
阅读原文:原文链接
该文章在 2025/3/31 11:40:51 编辑过