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 );}}}

C Language & |! What are

& Is the address fetch operator used to extract the address of a variable.
For example, if you define a variable, the system will allocate a space in the memory during compilation.
The location of the space in the memory is its address. & Extract its address.
E. g int a; assign an address to it during compilation, for example, 2000; & a is 2000.
If an integer pointer Variable p, p = & a; is defined, the address 2000 of a is assigned to p. P = 2000 after running.
Another example is scanf ("% d", & a). When you enter 3, it first knows the address of a according to & a, and finds the space of a in the memory by the address, write 3 to this space.
* Is a pointer operator, which is opposite to &. It extracts the value of a Variable Based on the address of the variable.
For example, * the value of a is 3 of variable.
The following is a summary of the pointer used in the definition and description.
Int * p; defines a pointer to integer data.
Int * p [n]; defines the pointer array p, which consists of n pointer elements pointing to integer data.
Int (* p) [n]; p is the pointer variable pointing to a one-dimensional array containing n elements.
Int * p (); p is the function that returns a pointer pointing to integer data.
Int (* p) (); p is the pointer to the function. This function returns an integer value.
Int ** p; p is a pointer variable that points to an integer Data Pointer variable.
If you want to learn more about the system, you can refer to tan haoqiang's c Programming (the third edition), which is easy to understand. Is a good C language learning material.

C Language & |! What are

& Is the address fetch operator used to extract the address of a variable.
For example, if you define a variable, the system will allocate a space in the memory during compilation.
The location of the space in the memory is its address. & Extract its address.
E. g int a; assign an address to it during compilation, for example, 2000; & a is 2000.
If an integer pointer Variable p, p = & a; is defined, the address 2000 of a is assigned to p. P = 2000 after running.
Another example is scanf ("% d", & a). When you enter 3, it first knows the address of a according to & a, and finds the space of a in the memory by the address, write 3 to this space.
* Is a pointer operator, which is opposite to &. It extracts the value of a Variable Based on the address of the variable.
For example, * the value of a is 3 of variable.
The following is a summary of the pointer used in the definition and description.
Int * p; defines a pointer to integer data.
Int * p [n]; defines the pointer array p, which consists of n pointer elements pointing to integer data.
Int (* p) [n]; p is the pointer variable pointing to a one-dimensional array containing n elements.
Int * p (); p is the function that returns a pointer pointing to integer data.
Int (* p) (); p is the pointer to the function. This function returns an integer value.
Int ** p; p is a pointer variable that points to an integer Data Pointer variable.
If you want to learn more about the system, you can refer to tan haoqiang's c Programming (the third edition), which is easy to understand. Is a good C language learning material.

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.