首页 > 布布扣 > 详细

winform设置button的边框颜色,或取消边框颜色,不显示边框

时间:2014-10-16 10:55:53      阅读:8849      评论:0      收藏:0      [点我收藏+]
winform设置边框颜色不像webform那么简单,可以通过设置FlatAppearance,也可以通过重绘实现。

一、设置按钮本身属性

buttonBubufx.FlatStyle = FlatStyle.Flat;
buttonBubufx.BackColor = Color.SkyBlue;
buttonBubufx.FlatAppearance.BorderColor = buttonBubufx.BackColor;


二、重绘,设置按钮的Region



        private static int WM_NCPAINT = 0x0085;
        private static int WM_ERASEBKGND = 0x0014;
        private static int WM_PAINT = 0x000F;

        [DllImport("user32.dll")]
        static extern IntPtr GetDCEx(IntPtr hwnd, IntPtr hrgnclip, uint fdwOptions);

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

        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);
            if (m.Msg == WM_NCPAINT || m.Msg == WM_ERASEBKGND || m.Msg == WM_PAINT)
            {
                IntPtr hdc = GetDCEx(m.HWnd, (IntPtr)1, 1 | 0x0020);

                if (hdc != IntPtr.Zero)
                {
                    Graphics graphics = Graphics.FromHdc(hdc);
                    Color borderColor = Color.HotPink;

                    Rectangle rectangle = new Rectangle(textBox1.Location.X, textBox1.Location.Y + (25), textBox1.Width, textBox1.Height);
                    ControlPaint.DrawBorder(graphics, rectangle, borderColor, ButtonBorderStyle.Solid);
                    m.Result = (IntPtr)1;
                    ReleaseDC(m.HWnd, hdc);

                }
            }
        }


winform设置button的边框颜色,或取消边框颜色,不显示边框

(4)
(4)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!