C # drawing on controls by using GDI,

Source: Internet
Author: User

C # drawing on controls by using GDI,

 

This document uses the chart control and the form to draw a rectangle as an example.

The code is very simple.

Some children's shoes need other source code. Please send me an email.

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 (); // Form g = this. createGraphics (); // chart control g2 = this. chart1.CreateGraphics ();} public Point firstPoint = new Point (0, 0); // the first Point of the mouse is public Point secondPoint = new Point (0, 0 ); // The second point of the mouse is public bool begin = false; // do you want to start drawing a rectangle? // <summary> // draw a rectangle from /// </summary> Graphics g; /// <summary> /// draw a rectangle on the chart1 control /// </summary> Graphics g2; /// <summary> /// press the mouse event on the form /// </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> /// move the mouse over the form to start drawing // </summary> /// <param name = "sender"> </param> // /<param name = "e"> </param> private void data_MouseMove (object sender, mouseEventArgs e) {if (begin) {// clear the form drawing surface, which is equivalent to refreshing the form interface and re-drawing g. clear (this. backColor); // obtain the coordinates secondPoint = new Point (e. x, e. y); // obtain the big or small 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); // frame 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> /// release the mouse to stop the drawing /// </summary> /// <param name = "sender"> </param> // <param name = "e"> </param> private void data_MouseUp (object sender, mouseEventArgs e) {begin = false ;} /// <summary> /// move the mouse over the chart control /// </summary> /// <param name = "sender"> </param> // /<param name = "e"> </param> private void chart1_MouseMove (object sender, mouseEventArgs e) {if (begin) {// drawing on the chart again. The clear method cannot be used here. clear clears the chart control of the entire drawing interface and clears this. refresh (); // obtain the coordinates 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); // draw a rectangle g2.DrawRectangle (new Pen (Color. red), minX, minY, maxX-minX, maxY-minY );}} /// <summary> /// release the mouse to stop the drawing /// </summary> /// <param name = "sender"> </param> // <param name = "e"> </param> private void chart1_MouseUp (object sender, mouseEventArgs e) {begin = false ;} /// <summary> /// press the mouse on the chart control /// </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 );}}}

In C language-> what?

-> Is a whole. It is used to point to a struct, class in C ++, and other pointers containing sub-data to obtain sub-data. In other words, if we define a struct in C and declare a pointer pointing to this struct, we need to use "->" to retrieve the data in the struct using the pointer ".
For example:
Struct Data
{
Int a, B, c;
};/* Define struct */
Struct Data * p;/* define struct pointer */
Struct Data A = {1, 2, 3};/* declare variable */
Int x;/* declare a variable x */
P = & A;/* point p to */
X = p-> a;/* indicates that the data item a in the struct pointed to by p is assigned to x */
/* Because p points to A, p-> a = A. a, that is, 1 */

For the first problem, p = p-> next; this should appear in the linked list of C language. next here should be a struct pointer of the same type as p, and its definition format should be:
Struct Data
{
Int;
Struct Data * next;
};/* Define struct */
............
Main ()
{
Struct Data * p;/* declare the pointer Variable p */
......
P = p-> next;/* assign the value in next to p */
}
The linked list pointer is a difficulty in C language, but it is also the key. It is very useful to learn it. To be careful, you must first talk about variables and pointers.
What is a variable? The so-called variables should not be simply thought that the amount will become a variable. Let's use the question of our Dean: "Is the classroom changing ?" Change, because there are different people in the classroom every day, but they do not change, because the classroom is always there, and it does not become larger or smaller. This is the variable: There is a constant address and a variable storage space. Under normal circumstances, we only see the variable in the room, that is, its content, but do not pay attention to the variable address, but the C language pointer is the address of the room. We declare that variables are equivalent to building a house to store things. We can directly watch things in the house, while declaring pointers is equivalent to getting a positioner. When a pointer points to a variable, it is to use the pointer to locate the variable. Then we can use the pointer to find the variable "tracked" and get the content in it.
What about struct? The structure is equivalent to a villa composed of several houses, and several houses are bound for use together. Suppose there are many such villas distributed in a big maze, and each villa has a house. The location information of another villa is put in it. Now you have found the first villa with the positioner and obtained what you want from it (the data part of the linked list ), then, calculate the location of the next villa into your positioner (p = p-> next), and go down to the next villa ...... If you go on like this, you will know that the information of a villa on the ground is gone (p-> next = NULL), and your trip is over. This is the process of traversing a linked list. Now you can understand the meaning of p = p-> next!
Write so much. I hope you can understand.
If you want to learn c and C ++ well, you must be familiar with linked lists and pointers!

In C language-> what?

-> Is a whole. It is used to point to a struct, class in C ++, and other pointers containing sub-data to obtain sub-data. In other words, if we define a struct in C and declare a pointer pointing to this struct, we need to use "->" to retrieve the data in the struct using the pointer ".
For example:
Struct Data
{
Int a, B, c;
};/* Define struct */
Struct Data * p;/* define struct pointer */
Struct Data A = {1, 2, 3};/* declare variable */
Int x;/* declare a variable x */
P = & A;/* point p to */
X = p-> a;/* indicates that the data item a in the struct pointed to by p is assigned to x */
/* Because p points to A, p-> a = A. a, that is, 1 */

For the first problem, p = p-> next; this should appear in the linked list of C language. next here should be a struct pointer of the same type as p, and its definition format should be:
Struct Data
{
Int;
Struct Data * next;
};/* Define struct */
............
Main ()
{
Struct Data * p;/* declare the pointer Variable p */
......
P = p-> next;/* assign the value in next to p */
}
The linked list pointer is a difficulty in C language, but it is also the key. It is very useful to learn it. To be careful, you must first talk about variables and pointers.
What is a variable? The so-called variables should not be simply thought that the amount will become a variable. Let's use the question of our Dean: "Is the classroom changing ?" Change, because there are different people in the classroom every day, but they do not change, because the classroom is always there, and it does not become larger or smaller. This is the variable: There is a constant address and a variable storage space. Under normal circumstances, we only see the variable in the room, that is, its content, but do not pay attention to the variable address, but the C language pointer is the address of the room. We declare that variables are equivalent to building a house to store things. We can directly watch things in the house, while declaring pointers is equivalent to getting a positioner. When a pointer points to a variable, it is to use the pointer to locate the variable. Then we can use the pointer to find the variable "tracked" and get the content in it.
What about struct? The structure is equivalent to a villa composed of several houses, and several houses are bound for use together. Suppose there are many such villas distributed in a big maze, and each villa has a house. The location information of another villa is put in it. Now you have found the first villa with the positioner and obtained what you want from it (the data part of the linked list ), then, calculate the location of the next villa into your positioner (p = p-> next), and go down to the next villa ...... If you go on like this, you will know that the information of a villa on the ground is gone (p-> next = NULL), and your trip is over. This is the process of traversing a linked list. Now you can understand the meaning of p = p-> next!
Write so much. I hope you can understand.
If you want to learn c and C ++ well, you must be familiar with linked lists and pointers!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.