VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > c#编程 >
  • C#教程之C# GDI在控件上绘图的方法

本文以在chart控件上和窗体上画矩形为例子讲述了C# GDI在控件上绘图的方法。分享给大家供大家参考。具体方法如下:

具体的实现方法就不多解释了,备注很详细,代码也很简单。

主要功能代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Configuration;
 
namespace WFApp2
{
  public partial class data : Form
  {
    public data()
    {
      InitializeComponent();
      //窗体
      g = this.CreateGraphics();
      //chart控件
      g2 = this.chart1.CreateGraphics();
    }
   
    public Point firstPoint = new Point(0, 0); //鼠标第一点
    public Point secondPoint = new Point(0, 0); //鼠标第二点
    public bool begin = false//是否开始画矩形
    /// <summary>
    /// 在from上画矩形
    /// </summary>
    Graphics g;
 
    /// <summary>
    /// 在chart1控件上画矩形
    /// </summary>
    Graphics g2;
 
    /// <summary>
    /// 在窗体上按下鼠标事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void data_MouseDown(object sender, MouseEventArgs e)
    {
      begin = true;
      firstPoint = new Point(e.X, e.Y);
    }
 
    /// <summary>
    /// 在窗体上鼠标移动开始绘图
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void data_MouseMove(object sender, MouseEventArgs e)
    {
      if (begin)
      {
        //清除窗体绘图面,相当于刷新了一次窗体界面然后重新绘制
        g.Clear(this.BackColor);
        //获取新的右下角坐标
        secondPoint = new Point(e.X, e.Y);
        //获取两个数中的大者或小者
        int minX = Math.Min(firstPoint.X, secondPoint.X);
        int minY = Math.Min(firstPoint.Y, secondPoint.Y);
        int maxX = Math.Max(firstPoint.X, secondPoint.X);
        int maxY = Math.Max(firstPoint.Y, secondPoint.Y);
 
        //画框
        g.DrawRectangle(new Pen(Color.Red), minX, minY, maxX - minX, maxY - minY);
        //ControlPaint.DrawReversibleFrame(new Rectangle(minX, minY, maxX - minX, maxY - minY),this.BackColor,FrameStyle.Dashed);
 
      }
    }
 
    /// <summary>
    /// 鼠标松开停止绘图
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void data_MouseUp(object sender, MouseEventArgs e)
    {
      begin = false;
    }
 
    /// <summary>
    /// 在chart控件上移动鼠标绘图
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void chart1_MouseMove(object sender, MouseEventArgs e)
    {
      if (begin)
      {
        //重新在chart上面绘图,此处不能用clear方法,clear会清除整个绘图界面chart控件会被清除
        this.Refresh();
        //获取新的右下角坐标
        secondPoint = new Point(e.X, e.Y);
        int minX = Math.Min(firstPoint.X, secondPoint.X);
        int minY = Math.Min(firstPoint.Y, secondPoint.Y);
        int maxX = Math.Max(firstPoint.X, secondPoint.X);
        int maxY = Math.Max(firstPoint.Y, secondPoint.Y);
 
        //画矩形
        g2.DrawRectangle(new Pen(Color.Red), minX, minY, maxX - minX, maxY - minY);
 
      }
    }
 
    /// <summary>
    /// 鼠标松开停止绘图
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void chart1_MouseUp(object sender, MouseEventArgs e)
    {
      begin = false;
    }
 
    /// <summary>
    /// 在chart控件上按下鼠标
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void chart1_MouseDown(object sender, MouseEventArgs e)
    {
      begin = true;
      firstPoint = new Point(e.X, e.Y);
    }
  }
}

希望本文所述对大家的C#程序设计有所帮助


相关教程