分享至
  • {{item.text}}
  • 本站大部分功能不支持IE浏览器,请使用谷歌内核的浏览器进行访问。

WinForm利用picturebox控件Paint方法绘制图形

WinForm利用picturebox控件Paint方法绘制图形

WinForm利用picturebox控件Paint方法绘制图形 实心圆、实心矩形

  1. 首先创建一个WinForm窗体应用
  2. 拖一个菜单控件(设置几个按钮)和一个picturebox控件(Name属性这里设置为 DrawPanel)
  3. 后台代码如下
public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
            this.DrawPanel.Paint += DrawPanel_Paint;
            this.SizeChanged += Form2_SizeChanged;
        }
        private void Form2_SizeChanged(object sender, EventArgs e)
        {
            this.DrawPanel.Refresh();
        }
        enum DrawStyle
        {
            圆形, 方形, 表格,文字
        }
        int effects = 10;//控件边距位置偏移量
        DrawStyle drawStyle;
        private void DrawPanel_Paint(object sender, PaintEventArgs e)
        {
            switch (drawStyle)
            {
                case DrawStyle.圆形:
                    this.DrawYuanXing(e);
                    break;
                case DrawStyle.方形:
                    this.DrawFangXing(e);
                    break;
                case DrawStyle.表格:
                    this.DrawBiaoGe(e);
                    break;
                case DrawStyle.文字:
                    this.DrawText(e);
                    break;
            }
        }
		
        void DrawYuanXing(PaintEventArgs e)
        {
            Brush brush = new SolidBrush(Color.Red);
            Pen pen = new Pen(brush);
            e.Graphics.DrawEllipse(pen, new RectangleF(e.ClipRectangle.Left+effects, 
			e.ClipRectangle.Top +
			effects, e.ClipRectangle.Width -(2* effects), 
			e.ClipRectangle.Height - (2 * effects)));
			//实心填充
            Rectangle r = new Rectangle(e.ClipRectangle.Left + effects, e.ClipRectangle.Top +
            effects, e.ClipRectangle.Width - (2 * effects), e.ClipRectangle.Height - (2 * effects));//标识区域
            e.Graphics.DrawEllipse(pen, r);
            e.Graphics.FillEllipse(brush, r);
        }
        void DrawFangXing(PaintEventArgs e)
        {
            Brush brush = new SolidBrush(Color.Red);
            e.Graphics.FillRectangle(brush, new RectangleF(e.ClipRectangle.Left + effects, 
			e.ClipRectangle.Top 
			+ effects, e.ClipRectangle.Width - (2 * effects), e.ClipRectangle.Height - (2 * effects)));
				//实心填充
			 Pen pen = new Pen(brush);
            Rectangle r = new Rectangle(0, 0, e.ClipRectangle.Width, e.ClipRectangle.Height);//标识区域
            e.Graphics.DrawRectangle(pen, r);
            e.Graphics.FillRectangle(brush, r);
        }
        void DrawBiaoGe(PaintEventArgs e)
        {

            Pen TableGridLineColor = new Pen(Color.Red, 2F);
            #region 绘制表格网格线 
            
            //先画外边框 左、上、右、下
         
            e.Graphics.DrawLine(TableGridLineColor, e.ClipRectangle.Left + effects, 
			e.ClipRectangle.Top + effects, e.ClipRectangle.Top + effects, e.ClipRectangle.Bottom - effects);
            e.Graphics.DrawLine(TableGridLineColor, e.ClipRectangle.Left+ effects, e.ClipRectangle.Top+
			effects, e.ClipRectangle.Right- effects, effects);
			e.Graphics.DrawLine(TableGridLineColor, e.ClipRectangle.Right - effects, e.ClipRectangle.Top
			+ effects, e.ClipRectangle.Right- effects, e.ClipRectangle.Bottom- effects);
			e.Graphics.DrawLine(TableGridLineColor, effects, e.ClipRectangle.Bottom- effects,
			e.ClipRectangle.Right- effects, e.ClipRectangle.Bottom- effects);

            ///绘制网格竖线 从左至右
            int ColumNum = 5;
            int ColumnWidth = e.ClipRectangle.Width/ColumNum;
            for (int i = 0; i < ColumNum; i++)
            {
                if (i <= 0)
                {
                    continue;
                }
                e.Graphics.DrawLine(TableGridLineColor, e.ClipRectangle.Top + (i *ColumnWidth),
				e.ClipRectangle.Top + effects, e.ClipRectangle.Top + (i * ColumnWidth), 
				e.ClipRectangle.Bottom - effects);
            }
            ///绘制网格横线 从上至下
            int RowNum = 5;
            int RowWidth = e.ClipRectangle.Height / RowNum;
            for (int i = 0; i < RowNum; i++)
            {
                if (i <= 0 )
                {
                    continue;

                }
                //绘制网格横线 从上往下
                e.Graphics.DrawLine(TableGridLineColor, e.ClipRectangle.Left+ effects, e.ClipRectangle.Top +
				(i*RowWidth), e.ClipRectangle.Right- effects, e.ClipRectangle.Top + (i * RowWidth));
            }
            #endregion
        }

        void DrawText(PaintEventArgs e) {
            Brush brush = new SolidBrush(Color.Red);
            Font font = DefaultFont;

            e.Graphics.DrawString("我是文字内容", DefaultFont, brush, e.ClipRectangle.Width / 2-50,
			e.ClipRectangle.Height / 2-50);
        }

        private void YuanXing_Click(object sender, EventArgs e)
        {
            drawStyle = DrawStyle.圆形;
            this.DrawPanel.Refresh();
        }

        private void FangXing_Click(object sender, EventArgs e)
        {
            drawStyle = DrawStyle.方形;
            this.DrawPanel.Refresh();
        }

        private void WangGe_Click(object sender, EventArgs e)
        {
            drawStyle = DrawStyle.表格;
            this.DrawPanel.Refresh();
        }

        private void TextContent_Click(object sender, EventArgs e)
        {
            drawStyle = DrawStyle.文字;
            this.DrawPanel.Refresh();
        }
    }

运行效果图如下

1. 圆形(椭圆) WinForm利用picturebox控件Paint方法绘制图形

2. 矩形 WinForm利用picturebox控件Paint方法绘制图形

3. 网格 WinForm利用picturebox控件Paint方法绘制图形

4. 文本 WinForm利用picturebox控件Paint方法绘制图形