【C#】WinForm自定義控件及窗體
|
admin
2025年3月29日 0:10
本文熱度 67
|
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 編輯過
|
|