男女做爽爽爽网站-男女做羞羞高清-男女做爰高清无遮挡免费视频-男女做爰猛烈-男女做爰猛烈吃奶啪啪喷水网站-内射白浆一区

LOGO OA教程 ERP教程 模切知識(shí)交流 PMS教程 CRM教程 開發(fā)文檔 其他文檔  
 
網(wǎng)站管理員

【C#】WinForm自定義控件及窗體

admin
2025年3月29日 0:10 本文熱度 443

【前言】                                                              
      WinForm(Windows Forms)是Microsoft.NET框架中的技術(shù),用于開發(fā)Windows桌面應(yīng)用程序。它提供了一套豐富的控件和組件。
    通過(guò)拖放控件、編寫事件處理程序等方式快速構(gòu)建用戶界面。通過(guò)屬性窗口定制這些控件的外觀和行為。
    通過(guò)數(shù)據(jù)綁定,將UI控件與數(shù)據(jù)源連接,實(shí)現(xiàn)數(shù)據(jù)的展示和更新。    通過(guò)上面的方法可以幫助開發(fā)者高效地創(chuàng)建桌面窗體應(yīng)用程序,尤其適合初學(xué)者和需要快速開發(fā)的項(xiàng)目。

?本文介紹了如何創(chuàng)建Winform窗體,并自定義窗體樣式,及窗體基本功能。

    1、窗體關(guān)閉、最大化、最小化、適應(yīng)。
    2、無(wú)邊框窗體移動(dòng)、調(diào)整窗體大小。
    3、菜單展開折疊。
【界面預(yù)覽】                                                        
【代碼】                                                              
    自定義按鈕                              
    用戶自定義按鈕:
        SelectedState:用戶點(diǎn)擊后狀態(tài)取反。
        Radius:按鈕圓角半徑。
        HoverColor:鼠標(biāo)懸停時(shí)的背景色。
public class UCButton : Button{    #region  公共字段、屬性    private bool _selectedState = false;
    [Category("UserProperty")]    [Description("選中狀態(tài)")]    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("鼠標(biāo)懸停時(shí)的背景色")]    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; // 鼠標(biāo)進(jìn)入時(shí)更改背景色    }    private void UCButton_MouseLeave(object sender, EventArgs e)    {        this.BackColor = _defaultColor; // 鼠標(biāo)離開時(shí)恢復(fù)默認(rèn)背景色    }    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;                     // 設(shè)置抗鋸齒        e.Graphics.CompositingQuality = CompositingQuality.HighQuality;         // 高質(zhì)量合成        e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;    // 高質(zhì)量插值        // 繪制圓角矩形        using (GraphicsPath path = new GraphicsPath())        {            path.AddArc(00, radius, radius, 18090);                         // 左上角            path.AddArc(this.Width - radius, 0, radius, radius, 27090);       // 右上角            path.AddArc(this.Width - radius, this.Height - radius, radius, radius, 090);  // 右下角            path.AddArc(0this.Height - radius, radius, radius, 9090);                   // 左下角            path.CloseFigure();
            this.Region = new Region(path); // 設(shè)置按鈕的區(qū)域?yàn)閳A角矩形        }        // 繪制按鈕文本        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);        }    }}
    窗體代碼                              
    窗體關(guān)閉、最大化、最小化、適應(yīng)。
    無(wú)邊框窗體移動(dòng)、調(diào)整窗體大小。
    菜單展開折疊。
public partial class MainForm : Form{    private int ButtonWidth = 62;
    #region 窗體初始化、加載、關(guān)閉    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(150150);        panel_MenuItemText.Hide();        ButtonWidth = btn_Expand.Width;    }    private void MainForm_FormClosing(object sender, FormClosingEventArgs e)    {
    }    #endregion
    /// <summary>    /// 窗體移動(dòng)功能事件綁定    /// </summary>    private void WinMoveBinding(Control control)    {        control.MouseDown += topBorderPanel_MouseDown;        control.MouseMove += topBorderPanel_MouseMove;        control.MouseUp += topBorderPanel_MouseUp;    }
    #region 窗體拖動(dòng)    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 無(wú)邊框窗體隨意拖動(dòng)和改變尺寸    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 窗體關(guān)閉、最大化、最小化    private void btn_ClosingWindow_Click(object sender, System.EventArgs e)    {        if (MessageBox.Show("是否關(guān)閉窗體!""關(guān)閉", 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
    /// <summary>    /// 折疊按鈕    /// </summary>    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();        }    }
    /// <summary>    /// 首頁(yè)按鈕    /// </summary>    private void btn_Home_Click(object sender, EventArgs e)    {
    }}

閱讀原文:原文鏈接


該文章在 2025/3/31 11:40:51 編輯過(guò)
關(guān)鍵字查詢
相關(guān)文章
正在查詢...
點(diǎn)晴ERP是一款針對(duì)中小制造業(yè)的專業(yè)生產(chǎn)管理軟件系統(tǒng),系統(tǒng)成熟度和易用性得到了國(guó)內(nèi)大量中小企業(yè)的青睞。
點(diǎn)晴PMS碼頭管理系統(tǒng)主要針對(duì)港口碼頭集裝箱與散貨日常運(yùn)作、調(diào)度、堆場(chǎng)、車隊(duì)、財(cái)務(wù)費(fèi)用、相關(guān)報(bào)表等業(yè)務(wù)管理,結(jié)合碼頭的業(yè)務(wù)特點(diǎn),圍繞調(diào)度、堆場(chǎng)作業(yè)而開發(fā)的。集技術(shù)的先進(jìn)性、管理的有效性于一體,是物流碼頭及其他港口類企業(yè)的高效ERP管理信息系統(tǒng)。
點(diǎn)晴WMS倉(cāng)儲(chǔ)管理系統(tǒng)提供了貨物產(chǎn)品管理,銷售管理,采購(gòu)管理,倉(cāng)儲(chǔ)管理,倉(cāng)庫(kù)管理,保質(zhì)期管理,貨位管理,庫(kù)位管理,生產(chǎn)管理,WMS管理系統(tǒng),標(biāo)簽打印,條形碼,二維碼管理,批號(hào)管理軟件。
點(diǎn)晴免費(fèi)OA是一款軟件和通用服務(wù)都免費(fèi),不限功能、不限時(shí)間、不限用戶的免費(fèi)OA協(xié)同辦公管理系統(tǒng)。
Copyright 2010-2025 ClickSun All Rights Reserved

主站蜘蛛池模板: 欧美黑人添添高潮A片视频 欧美黑人性暴力猛交免费看 | 国产亚洲精品久久久久久久软件 | 亚洲成人高清在线 | 日本熟妇乱子a片另类 | 国精产品69永久中国有限 | 久久综合九色综合97_ 久久久 | 动漫在线观看片A免费观看 都市人妻古典武侠另类校园 | 亚洲免费高清视频 | 日本高清在线视频无码 | 青青久在线视频免费视频 | 99久久久无码国产精品性蜜奴 | a级片在线免费看 | 家庭教师波多野吉衣 | 欧美午夜片欧美片在线观看 | 精品日韩午夜电影在线看 | 99国产精品久久久久久久久久久 | 一级做a爱过程免费视频时看 | 2024自拍偷区亚洲综合第一页 | 亚洲女同一区二区 | 国产老妇伦国产熟女老妇视频 | āV第三区亚洲狠狠婷婷综合久久 | 97人妻久久久精品系列A片 | 欧美又大又长又粗又爽A片 欧美又大又粗AAA片免费看 | 国产成人av综合 | 国产精品久久久久成人免费 | 久久精品国产亚洲av天美18 | 色中色地址 | 精品国产不卡一二三区看片 | 国语高清精品一区二区三区 | 国内偷拍2019在线偷拍视频 | 天天躁人人躁人人躁狂躁 | 日本无码黄人妻一区二区 | 精品国产人妻国语 | 天天爱天天干天天透 | 亚洲老熟女AV一区二区在线播放 | 国产精品恋恋影视 | 四虎影在线在永久观看 | 久久免费看少妇高潮A片小说 | 黑人巨大精品欧美一区二区免费 | 浪荡女天天不停挨cao日常视频 | 免费播放一区二区三区 |