【C#】WinForm自界说控件及窗体

打印 上一主题 下一主题

主题 1027|帖子 1027|积分 3081

前言

   WinForm(Windows Forms)是Microsoft.NET框架中的技术,用于开发Windows桌面应用步伐。它提供了一套丰富的控件和组件。通过拖放控件、编写事件处置惩罚步伐等方式快速构建用户界面。
     通过属性窗口定制这些控件的表面和行为。
通过数据绑定,将UI控件与数据源毗连,实现数据的展示和更新。
通过上面的方法可以资助开发者高效地创建桌面窗体应用步伐,尤其恰当初学者和必要快速开发的项目。
    本文先容了怎样创建Winform窗体,并自界说窗体样式和窗体的根本功能。
   1、窗体关闭、最大化、最小化、适应。
2、无边框窗体移动、调解窗体巨细。
3、菜单睁开折叠。
  界面预览


代码

自界说按钮

  1. 用户自定义按钮:        
复制代码
  SelectedState:用户点击后状态取反。
Radius:按钮圆角半径。
HoverColor:鼠标悬停时的背景致。
  1. public class UCButton : Button
  2. {
  3.     #region  公共字段、属性
  4.     private bool _selectedState = false;
  5.     [Category("UserProperty")]
  6.     [Description("选中状态")]
  7.     public bool SelectedState
  8.     {
  9.         get => _selectedState;
  10.         private set
  11.         {
  12.             _selectedState = value;
  13.             this.Invalidate();
  14.         }
  15.     }
  16.     private int radius = 15;
  17.     [Category("UserProperty")]
  18.     [Description("圆角半径")]
  19.     public int Radius
  20.     {
  21.         get { return radius; }
  22.         set
  23.         {
  24.             radius = value;
  25.             this.Invalidate();
  26.         }
  27.     }
  28.     private Color _defaultColor;
  29.     private Color _hoverColor = Color.LightBlue;
  30.     [Category("UserProperty")]
  31.     [Description("鼠标悬停时的背景色")]
  32.     public Color HoverColor
  33.     {
  34.         get => _hoverColor;
  35.         set => _hoverColor = value;
  36.     }
  37.     #endregion
  38.     public UCButton()
  39.     {
  40.         Initialize();
  41.     }
  42.     private void Initialize()
  43.     {
  44.         this.DoubleBuffered = true;
  45.         this.FlatStyle = FlatStyle.Flat;
  46.         this.FlatAppearance.BorderSize = 0;
  47.         this.SetStyle(ControlStyles.UserPaint
  48.             | ControlStyles.AllPaintingInWmPaint
  49.             | ControlStyles.OptimizedDoubleBuffer
  50.             | ControlStyles.ResizeRedraw
  51.             | ControlStyles.SupportsTransparentBackColor, true);
  52.         _defaultColor = BackColor;
  53.         this.MouseEnter += UCButton_MouseEnter;
  54.         this.MouseLeave += UCButton_MouseLeave;
  55.     }
  56.     private void UCButton_MouseEnter(object sender, EventArgs e)
  57.     {
  58.         this.BackColor = HoverColor; // 鼠标进入时更改背景色
  59.     }
  60.     private void UCButton_MouseLeave(object sender, EventArgs e)
  61.     {
  62.         this.BackColor = _defaultColor; // 鼠标离开时恢复默认背景色
  63.     }
  64.     protected override void OnClick(EventArgs e)
  65.     {
  66.         base.OnClick(e);
  67.         _selectedState = !_selectedState;
  68.     }
  69.     protected override void OnPaint(PaintEventArgs e)
  70.     {
  71.         base.OnPaint(e);
  72.         e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;                     // 设置抗锯齿
  73.         e.Graphics.CompositingQuality = CompositingQuality.HighQuality;         // 高质量合成
  74.         e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;    // 高质量插值
  75.         // 绘制圆角矩形
  76.         using (GraphicsPath path = new GraphicsPath())
  77.         {
  78.             path.AddArc(0, 0, radius, radius, 180, 90);                         // 左上角
  79.             path.AddArc(this.Width - radius, 0, radius, radius, 270, 90);       // 右上角
  80.             path.AddArc(this.Width - radius, this.Height - radius, radius, radius, 0, 90);  // 右下角
  81.             path.AddArc(0, this.Height - radius, radius, radius, 90, 90);                   // 左下角
  82.             path.CloseFigure();
  83.             this.Region = new Region(path); // 设置按钮的区域为圆角矩形
  84.         }
  85.         // 绘制按钮文本
  86.         using (Brush brush = new SolidBrush(this.ForeColor))
  87.         {
  88.             SizeF textSize = e.Graphics.MeasureString(this.Text, this.Font);
  89.             PointF textLocation = new PointF((this.Width - textSize.Width) / 2, (this.Height - textSize.Height) / 2);
  90.             e.Graphics.DrawString(this.Text, this.Font, brush, textLocation);
  91.         }
  92.     }
  93. }
复制代码
窗体代码

   1、窗体关闭、最大化、最小化、适应。
2、无边框窗体移动、调解窗体巨细。
3、菜单睁开折叠。
  1. public partial class MainForm : Form
  2. {
  3.     private int ButtonWidth = 62;
  4.     #region 窗体初始化、加载、关闭
  5.     public MainForm()
  6.     {
  7.         InitializeComponent();
  8.         this.CenterToParent();
  9.         this.CenterToScreen();
  10.     }
  11.     private void MainForm_Load(object sender, System.EventArgs e)
  12.     {
  13.         WinMoveBinding(panel_TopBorderItem);
  14.         WinMoveBinding(pic_WinIcon);
  15.         this.WindowState = FormWindowState.Normal;
  16.         this.MinimumSize = new System.Drawing.Size(150, 150);
  17.         panel_MenuItemText.Hide();
  18.         ButtonWidth = btn_Expand.Width;
  19.     }
  20.     private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
  21.     {
  22.     }
  23.     #endregion
  24.    
  25.     /// <summary>
  26.     /// 窗体移动功能事件绑定
  27.     /// </summary>
  28.     private void WinMoveBinding(Control control)
  29.     {
  30.         control.MouseDown += topBorderPanel_MouseDown;
  31.         control.MouseMove += topBorderPanel_MouseMove;
  32.         control.MouseUp += topBorderPanel_MouseUp;
  33.     }
  34.     #region 窗体拖动
  35.     private Point mouseOffset;
  36.     private void topBorderPanel_MouseDown(object sender, MouseEventArgs e)
  37.     {
  38.         if (e.Button == MouseButtons.Left)
  39.         {
  40.             mouseOffset = new Point(-e.X, -e.Y);
  41.         }
  42.     }
  43.     private void topBorderPanel_MouseMove(object sender, MouseEventArgs e)
  44.     {
  45.         if (e.Button == MouseButtons.Left)
  46.         {
  47.             Point mousePos = Control.MousePosition;
  48.             mousePos.Offset(mouseOffset.X, mouseOffset.Y);
  49.             this.Location = mousePos;
  50.         }
  51.     }
  52.     private void topBorderPanel_MouseUp(object sender, MouseEventArgs e)
  53.     {
  54.         mouseOffset = Point.Empty;
  55.     }
  56.     #endregion
  57.     #region 无边框窗体随意拖动和改变尺寸
  58.     const int WM_NCHITTEST = 0x0084;
  59.     const int HTLEFT = 10;
  60.     const int HTRIGHT = 11;
  61.     const int HTTOP = 12;
  62.     const int HTTOPLEFT = 13;
  63.     const int HTTOPRIGHT = 14;
  64.     const int HTBOTTOM = 15;
  65.     const int HTBOTTOMLEFT = 0x10;
  66.     const int HTBOTTOMRIGHT = 17;
  67.     protected override void WndProc(ref Message m)
  68.     {
  69.         base.WndProc(ref m);
  70.         switch (m.Msg)
  71.         {
  72.             case WM_NCHITTEST:
  73.                 Point vPoint = new Point((int)m.LParam & 0xFFFF,
  74.                     (int)m.LParam >> 16 & 0xFFFF);
  75.                 vPoint = PointToClient(vPoint);
  76.                 if (vPoint.X <= 5)
  77.                     if (vPoint.Y <= 5)
  78.                         m.Result = (IntPtr)HTTOPLEFT;
  79.                     else if (vPoint.Y >= ClientSize.Height - 5)
  80.                         m.Result = (IntPtr)HTBOTTOMLEFT;
  81.                     else m.Result = (IntPtr)HTLEFT;
  82.                 else if (vPoint.X >= ClientSize.Width - 5)
  83.                     if (vPoint.Y <= 5)
  84.                         m.Result = (IntPtr)HTTOPRIGHT;
  85.                     else if (vPoint.Y >= ClientSize.Height - 5)
  86.                         m.Result = (IntPtr)HTBOTTOMRIGHT;
  87.                     else m.Result = (IntPtr)HTRIGHT;
  88.                 else if (vPoint.Y <= 5)
  89.                     m.Result = (IntPtr)HTTOP;
  90.                 else if (vPoint.Y >= ClientSize.Height - 5)
  91.                     m.Result = (IntPtr)HTBOTTOM;
  92.                 break;
  93.         }
  94.     }
  95.     #endregion
  96.     #region 窗体关闭、最大化、最小化
  97.     private void btn_ClosingWindow_Click(object sender, System.EventArgs e)
  98.     {
  99.         if (MessageBox.Show("是否关闭窗体!", "关闭", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK)
  100.         {
  101.             this.Close();
  102.         }
  103.     }
  104.     private void btn_Maximize_Click(object sender, System.EventArgs e)
  105.     {
  106.         Button button = sender as Button;
  107.         if (this.WindowState == FormWindowState.Maximized)
  108.         {
  109.             this.WindowState = FormWindowState.Normal;
  110.             button.Image = global::ModbusDemo.Properties.Resources.maximize_blue_32;
  111.         }
  112.         else
  113.         {
  114.             this.WindowState = FormWindowState.Maximized;
  115.             button.Image = global::ModbusDemo.Properties.Resources.restore_blue_32;
  116.         }
  117.     }
  118.     private void btn_Minimize_Click(object sender, System.EventArgs e)
  119.     {
  120.         this.WindowState = FormWindowState.Minimized;
  121.     }
  122.     #endregion
  123.     /// <summary>
  124.     /// 折叠按钮
  125.     /// </summary>
  126.     private void btn_Expand_Click(object sender, System.EventArgs e)
  127.     {
  128.         //展开
  129.         if (!btn_Expand.SelectedState)
  130.         {
  131.             btn_Expand.Image = global::ModbusDemo.Properties.Resources.collapse_left_blue_32;
  132.             panel_MenuItemIcon.Width = ButtonWidth;
  133.             panel_MenuItemText.ScrollControlIntoView(btn_Expand);
  134.             panel_MenuItemText.Show();
  135.             panel_LeftMenuItem.Width = 256;
  136.         }
  137.         //折叠
  138.         else
  139.         {
  140.             btn_Expand.Image = global::ModbusDemo.Properties.Resources.collapse_right_blue_32;
  141.             
  142.             panel_MenuItemIcon.Width = ButtonWidth;
  143.             panel_LeftMenuItem.Width = ButtonWidth;
  144.             panel_MenuItemText.Hide();
  145.         }
  146.     }
  147.     /// <summary>
  148.     /// 首页按钮
  149.     /// </summary>
  150.     private void btn_Home_Click(object sender, EventArgs e)
  151.     {
  152.     }
  153. }
复制代码
结语

   既是分享,也是备份。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

美食家大橙子

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表