WinForm 开发过程中的一些小功能记录

本文记录了WinForm开发中的实用技巧,包括设置窗体圆角、无边框窗体状态下显示状态栏和重绘标题栏,以及自定义Panel控件实现倒角、圆角和标题功能。此外,还解决了线程间操作无效的问题。
摘要由CSDN通过智能技术生成

窗体类

设置窗体圆角

public partial class Form1 : Form

		public Form1()
        {
   

            InitializeComponent();
            SetFormRoundRectRgn(this, 5);    //设置圆角

        }
        #region Public extern methods

        [DllImport("gdi32.dll")]
        public static extern int CreateRoundRectRgn(int x1, int y1, int x2, int y2, int x3, int y3);

        [DllImport("user32.dll")]
        public static extern int SetWindowRgn(IntPtr hwnd, int hRgn, Boolean bRedraw);

        [DllImport("gdi32.dll", EntryPoint = "DeleteObject", CharSet = CharSet.Ansi)]
        public static extern int DeleteObject(int hObject);

        [DllImport("user32.dll")]
        public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);

        [DllImport("user32.dll")]
        public static extern bool ReleaseCapture();

        public const int WM_SYSCOMMAND = 0x0112;
        public const int SC_MOVE = 0xF010;
        public const int HTCAPTION = 0x0002;
        #endregion
                /// <summary>
        /// 设置窗体的圆角矩形
        /// </summary>
        /// <param name="form">需要设置的窗体</param>
        /// <param name="rgnRadius">圆角矩形的半径</param>
        public static void SetFormRoundRectRgn(Form form, int rgnRadius)
        {
   
            int hRgn = 0;
            hRgn = CreateRoundRectRgn(0, 0, form.Width + 1, form.Height + 1, rgnRadius, rgnRadius);
            SetWindowRgn(form.Handle, hRgn, true);
            DeleteObject(hRgn);
        }

要在窗体大小改变的事件中添加 SetFormRoundRectRgn,不然窗体大小改变的时候会无法绘制回原样

        private void Form1_SizeChanged(object sender, EventArgs e)
        {
   
            SetFormRoundRectRgn(this, 5);    
        }

使用无边框窗体时

FormBorderStyle 属性设置为None

this.FormBorderStyle = FormBorderStyle.None;

显示电脑状态栏

在窗体初始化事件Form1_Load或者构造函数中添加

this.MaximizedBounds = Screen.PrimaryScreen.WorkingArea;//在窗体初始化后添加一句代码 最大化的时候才会显示电脑状态栏

重绘窗体标题栏

在无边框窗体中添加一个Panel控件用于显示标题和最小化、最大化、关闭按钮
并且实现拖拽窗体

		//控件绘制事件
        private void panel1_Paint(object sender, PaintEventArgs e)
        {
   
            Rectangle vRectangle = new Rectangle(0, 0, panel_Top.Width, panel_Top.Height);
            Graphics vGraphics = e.Graphics;
            //设置渐变矩形
            //LinearGradientMode.Vertical设置渐变方向
            vGraphics.FillRectangle(new LinearGradientBrush(vRectangle, Color.FromArgb(50, 95, 119, 244), Color.FromArgb(100, 99, 113, 255), LinearGradientMode.Vertical), vRectangle);

            StringFormat vStringFormat = new StringFormat();
            vStringFormat.Alignment = StringAlignment.Center;
            vStringFormat.LineAlignment = StringAlignment.Center;
            Font vFont = new Font("黑体", 16);
            vGraphics.DrawString("我是窗体标题", vFont, Brushes.BlanchedAlmond, vRectangle, vStringFormat);
            vGraphics.Dispose();
        }
        //鼠标按下事件
        private void panel1_MouseDown(object sender, MouseEventArgs e)
        {
   
            //拖动窗体
            ReleaseCapture();
            SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
        }
        private void Bn_CloseForm_Click(object sender, EventArgs e)
        {
   
            this.Close();
        }
        private void Bn_MaximizeForm_Click(object sender, EventArgs e)
        {
   
            if (this.WindowState == FormWindowState.Maximized)
            {
   
                this.WindowState = FormWindowState.Normal;
            }
            else
            {
   
                this.WindowState = FormWindowState.Maximized;
            }
        }
        private void Bn_Minimize_Click(object sender, EventArgs e)
        {
   
            if (this.WindowState == FormWindowState.Minimized)
            {
   
                this.WindowState = FormWindowState.Normal;
            }
            else
            {
   
                this.WindowState = FormWindowState.Minimized;
            }
        }

自定义控件、重绘控件类

Panel——倒角、圆角、带标题

在这里插入图片描述

倒角、圆角的Panel

 public partial class PolyonPanel : Panel
    {
   
        public PolyonPanel()
        {
   
            InitializeComponent();
            SetStyle(ControlStyles.DoubleBuffer, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, false);
            SetStyle(ControlStyles.ResizeRedraw, true);
            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            this.Padding = new System.Windows.Forms.Padding(0, 0, 0, 0);
            this.Margin = new System.Windows.Forms.Padding(0, 0, 0, 0);
            this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
            this.Paint += new PaintEventHandler(PanelEx_Paint);
        }

        public PolyonPanel(IContainer container)
        {
   
            container.Add(this);

            InitializeComponent();
        }
        private Color backColor = Color.FromArgb(1, 28, 58);

        [Category("自定义属性"), Description("组件的背景颜色。"), Browsable(true)]
        public override Color BackColor
        {
   
            get {
    return backColor; }
            set
            {
   
                backColor = value;
                this.Invalidate();
            }
        }

        [DllImport("user32.dll")]
        static extern IntPtr GetWindowDC(IntPtr hwnd);
        [DllImport("user32.dll")]
        static extern int ReleaseDC(IntPtr hwnd, IntPtr hdc);

        public enum polyonEnum
        {
   
            倒角,
            圆角
        }

        public polyonEnum polyon;
        [Category("自定义属性"), Description("设置形状"), Browsable(true), DefaultValue("属性默认值")]
        public polyonEnum Polyon
        {
   
            get {
    return polyon; }
            set {
    polyon = value; this.Invalidate(); }
        }

        Color _borderColor = Color.Black;
        [Category("自定义属性"), Description("边框颜色。"), Browsable(true)]
        public Color BorderColor
        {
   
            get {
    return _borderColor; }
            set {
    _borderColor = value; this.Invalidate(); }
        }

        int _borderWidth = 1;
        [Category("自定义属性"), Description("边框宽度。"), Browsable(true)]
        public int BorderWidth
        {
   
            get {
    return _borderWidth; }
            set {
    _borderWidth = value; this.Invalidate(); }
        }

        [Category("自定义属性"), Description("生成该颜色的图片填充背景图片"), Browsable(true)]
        public Color BackImgColor
        {
   
            get {
    return _currBColor; }
            set {
    SetBackColorImg
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

多巴胺耐受

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值