This article describes a way for C # GDI to draw on a control as an example of drawing rectangles on a chart control and on a form. Share to everyone for your reference. Here's how:
The specific implementation method is not much explained, the note is very detailed, the code is very simple.
The main function code is as follows:
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 (); Firstpoint = new Point (0, 0); The 1th public point of the mouse secondpoint = new Point (0, 0); Mouse 2nd public bool begin = FALSE; Whether to start drawing rectangles///<summary>///Draw rectangles on 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>///On the form the mouse movement starts drawing///</summary>//<param name= "sender" ></param>/ <param name= "E" ></param> private void Data_mousemove (object sender, MouseEventArgs e) {if (Begi N) {//Clears the form drawing surface, which is equivalent to refreshing the form interface once and then redrawing the g.clear (this. BackColor); Gets the new lower-right corner coordinate secondpoint = new Point (e.x, e.y); Get two number of large 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>//Mouse Release stop drawing///</summary>//<param name= "Sender" ></param>//<param name= "E" ></param> private void Data_mouseup (object sender, MouseEventArgs e) { begin = FALSE; }///<summary>///To move the mouse drawing on the chart control///</summary>//<param name= "Sender" ></param> <param name= "E" ></param> private void Chart1_mousemove (object sender, MouseEventArgs e) {if (BEGIN) {//re-draw on chart above, no clear method here, clear clears the entire drawing interface The chart control clears this. Refresh (); Gets the new lower-right corner coordinate 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>//Mouse Release stop drawing///</summary>//<param name= "Sender" ></param> ///<param name= "E" ></param> private void Chart1_mouseup (object sender, MouseEventArgs e) {begin = Fal Se }///<summary>///press 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); } }}
I hope this article is helpful to everyone's C # program design
In addition to the Declaration,
Running GuestArticles are original, reproduced please link to the form of the address of this article
C # GDI methods for drawing on a control
This address: http://www.paobuke.com/develop/c-develop/pbk23526.html
Related content How to create a PDF grid and insert a picture in C # a summary of sort functions in C # use C # to write an asynchronous socket Server C # operation Windows Registry method
C # Implementation of simple screen monitoring methods C # Accurate calculation of age method Analysis C # Avoid backtracking methods the covariance and inversion examples of c#4.0 new characteristics
C # GDI methods for drawing on a control