当前位置:首页 > 电子 > GDI绘制圆角矩形边框、颜色、内部填充样式样式和字体

GDI绘制圆角矩形边框、颜色、内部填充样式样式和字体

/// <summary>
        /// 绘制Panel容器
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Set_Paint(object sender, PaintEventArgs e)
        {
            //圆角矩形实心颜色,交叉对角线的模式:HatchStyle.DiagonalCross,Color.White前景色、Color.Red背景色
            Brush brush = new HatchBrush(HatchStyle.DiagonalCross, Color.White, Color.Red);
            //圆角矩形边框颜色
            Pen pen = new Pen(Color.Black, 2);
            pen.DashStyle = DashStyle.Dash;
            //初始化确定圆角实心矩形初始X、Y位置和Width、height宽高
            Rectangle rectangle = new Rectangle(4,4,82,44);
            //绘制圆角矩形、实现方法
            FillRoundRectangle(e.Graphics, rectangle, brush, pen, 15); 
        } 

        /// <summary>  
        /// C# GDI+ 绘制圆角实心矩形  
        /// </summary>  
        /// <param name="g">Graphics 对象</param>  
        /// <param name="rectangle">要填充的矩形</param>  
        /// <param name="brush">填充背景色和样式</param> 
        /// <param name="pen">外部边框颜色和样式</param>
        /// <param name="r">圆角半径</param>  
        public void FillRoundRectangle(Graphics g, Rectangle rectangle, Brush brush,Pen pen, int r)
        {
            rectangle = new Rectangle(rectangle.X, rectangle.Y, rectangle.Width - 1, rectangle.Height - 1); //确定圆角实心矩形初始X、Y位置和Width、height宽高
            g.FillPath(brush, GetRoundRectangle(rectangle, r)); //绘制圆角实心矩形
            g.DrawPath(pen, GetRoundRectangle(rectangle, r));   //绘制圆角实心矩形边框

            Brush brush_word = new SolidBrush(Color.Black);  //绘制的字体颜色(new记得放在方法外)
            g = this.panel.CreateGraphics();//为panel容器创建Graphics对象
            Font ft = new Font("黑体", 8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));//定义字体
            g.DrawString("Hello", ft, brush_word, (rectangle.Width / 2) - 10, (rectangle.Height / 2) - 5);
        }

        /// <summary>  
        /// 根据普通矩形得到圆角矩形的路径  
        /// </summary>  
        /// <param name="rectangle">原始矩形</param>  
        /// <param name="r">半径</param>  
        /// <returns>图形路径</returns>  
        private static GraphicsPath GetRoundRectangle(Rectangle rectangle, int r)
        {
            int l = 2 * r;
            // 把圆角矩形分成八段直线、弧的组合,依次加到路径中  
            GraphicsPath gp = new GraphicsPath();
            gp.AddLine(new Point(rectangle.X + r, rectangle.Y), new Point(rectangle.Right - r, rectangle.Y));
            gp.AddArc(new Rectangle(rectangle.Right - l, rectangle.Y, l, l), 270F, 90F);

            gp.AddLine(new Point(rectangle.Right, rectangle.Y + r), new Point(rectangle.Right, rectangle.Bottom - r));
            gp.AddArc(new Rectangle(rectangle.Right - l, rectangle.Bottom - l, l, l), 0F, 90F);

            gp.AddLine(new Point(rectangle.Right - r, rectangle.Bottom), new Point(rectangle.X + r, rectangle.Bottom));
            gp.AddArc(new Rectangle(rectangle.X, rectangle.Bottom - l, l, l), 90F, 90F);

            gp.AddLine(new Point(rectangle.X, rectangle.Bottom - r), new Point(rectangle.X, rectangle.Y + r));
            gp.AddArc(new Rectangle(rectangle.X, rectangle.Y, l, l), 180F, 90F);
            return gp;
        }

效果图:边框、颜色、内部字符串按实际情况定义即可,一般会满足各种情况


具体样式