使用画笔和钢笔

来源:互联网 发布:js随机生成英文名字 编辑:程序博客网 时间:2024/04/27 19:56

1.       阴影画笔

 

                   HatchBrush hBrush1 = new HatchBrush

                       (HatchStyle.DashedVertical, Color.Chocolate, Color.Red);

                   HatchBrush hBrush2 = new HatchBrush

                       (HatchStyle.DarkDownwardDiagonal, Color.Green, Color.Black);

                   HatchBrush hBrush3 = new HatchBrush

                       (HatchStyle.SmallCheckerBoard, Color.BlueViolet, Color.Blue);

 

                   g.FillEllipse(hBrush1, 20, 40, 100, 120);         

                   Rectangle rect = new Rectangle(150, 80, 200, 140);           

                   g.FillPie(hBrush3, 40, 20, 200, 40, 0.0f, 60.0f );          

                   g.FillRectangle(hBrush2, rect); 

 

 

2. Form1_Load事件处理程序:创建一个位图并从此位图创建一个Graphics对象。

private void Form1_Load(object sender,

              System.EventArgs e)

         {

              // Get the full size of the form

              fullSize = SystemInformation

                   .PrimaryMonitorMaximizedWindowSize;

              // Create a bitmap using full size

              bitmap = new Bitmap(fullSize.Width,

                   fullSize.Height);

              // Create a Graphics object from Bitmap

              curGraphics = Graphics.FromImage(bitmap);

              // Set background color as form's color

              curGraphics.Clear(this.BackColor);

              // Default pen and brush button colors

              PenBtn.BackColor = Color.Black;

              BrushBtn.BackColor = Color.Black;

              // Create a new pen and brush as

              // default pen and brush

              curPen = new Pen(Color.Black);

              curBrush = new SolidBrush(Color.Black);

         }

3. 选择钢笔和画笔的颜色

     private void PenSettings_Click(object sender,

              System.EventArgs e)

         {

              ColorDialog colorDlg = new ColorDialog();

              colorDlg.ShowDialog();

              PenBtn.BackColor = colorDlg.Color;       

         }

 

         private void BrushSettings_Click(object sender,

              System.EventArgs e)

         {

              ColorDialog colorDlg = new ColorDialog();

              colorDlg.ShowDialog();

              BrushBtn.BackColor = colorDlg.Color;     

         }

4.      窗体的paint事件处理程序

以下代码设置钢笔的ColorWidth属性及画笔的Color属性。

private void Form1_Paint(object sender,

      System.Windows.Forms.PaintEventArgs e)

    {

      // Set current pens color

      curPen.Color = Color.FromArgb(

        Convert.ToInt16(

        TransCounter.Value.ToString()),

        PenBtn.BackColor.R,

        PenBtn.BackColor.G,

        PenBtn.BackColor.B);

      // Set pen's width

      curPen.Width = (float)PenWidthCounter.Value;

      // Set current brush's color

      curBrush.Color = Color.FromArgb(

        Convert.ToInt16(

        TransCounter.Value.ToString()),

        BrushBtn.BackColor.R,

        BrushBtn.BackColor.G,

        BrushBtn.BackColor.B);

   

      Graphics g = e.Graphics;

      // If dragmode is true, draw selected

      // graphics shape

      if (dragMode)

      {

        switch (drawIndex)

        {

          case 1:

          {

            g.DrawLine(curPen, curX, curY, x, y);

            break;

          }

          case 2:

          {

            g.DrawEllipse(curPen,

              curX, curY, diffX, diffY);

            break;

          }

          case 3:

          {

            g.DrawRectangle(curPen,

              curX, curY, diffX, diffY);

            break;

          }

          case 4:

          {      

            g.FillRectangle(curBrush,

              curX, curY, diffX, diffY);           

            break;

          }

          case 5:

          {

            g.FillEllipse(curBrush,

              curX, curY, diffX, diffY);

            break;

          }      

        }

      }

}