Starting from a drawing program-basics, GDI + (1)

Source: Internet
Author: User

This is a very small. Net Program for drawing. It can draw too much because it can only draw line segments.

Why do you want to write it? You can learn from new users. But this is not the main reason for writing it. This is a comparison. I will compare it with the method after using the mode, so that you can see the power of the mode. A newbie may wish to compare the two implementations. At the very least, you can learn how to draw a line, and you can also see that the mode is a good thing.

 

Because this program is too simple, I will only briefly introduce the main functions here.

 

The following is a program for drawing a line. The main problem is that you need to erase the original line when you detach it. In GDI +, the XOR line method is not provided, so you can use it as needed.



This is a bit of a problem: Why does GDI + not provide XOR? In addition, I can view MSDN, and the rubber line can be implemented through DLLimport, but the following method is recommended for GDI docotor.

I don't know what is the difference in efficiency. Is the difference big?

Another way is to use this method to erase other lines that have passed through. I don't know if the XOR method will work like this.

So on the homepage, Which of the following is a guide by the GDI experts. Put it in the newbie zone.

Public void DrawLine (Point start, Point end)

{

DrawingGraphics. DrawLine (erasePen, start, previousPoint );

DrawingGraphics. DrawLine (drawPen, start, end );

PreviousPoint = end;

}

The following section will take a long time. It mainly refers to what you want to do when you click the mouse. We need to discuss the situation and see the damn if else (the pattern is to eliminate such conditional judgment ). I believe everyone can understand it.

Private void form=mousedown (object sender, MouseEventArgs e)

{

 

If (bDrawLine & bFirstDown)

{

This. Cursor = Cursors. Cross;

Line newLine = new Line ();

NewLine. StartPoint = new Point (e. X, e. Y );

DrawingLine = newLine;

BFirstDown = false;

}

Else if (bDrawLine = true & bFirstDown = false)

{

If (drawingLine! = Null)

{

DrawingLine. EndPoint = new Point (e. X, e. Y );

AddLine (drawingLine );

This. Cursor = Cursors. Arrow;

BFirstDown = true;

}

}

}


 

Then there is the effect of moving the mouse, where the first function is used.

Private void form=mousemove (object sender, MouseEventArgs e)

{

If (bFirstDown = false & bDrawLine = true)

{

If (drawingLine! = Null)

{

Point mousePoint = new Point (e. X, e. Y );

DrawLine (drawingLine. StartPoint, mousePoint );

}

}

}

 

I am not going to introduce it in detail. From these functions, we can see that there are too many tasks in the Form1_MouseDown function, and there are also some conditional judgments. The current function is so simple, but the logic is not clear. If I want to implement the line capture function, what else can I do if I have to do so? Form1_MouseDown is also estimated to be understandable only by the writer. This is the power of our design model. Please refer to the next decomposition.

 

Can you think of a solution in what mode? As a newbie, I will give you a Wallop as a reward, so don't rob me.

 

 



Source code download

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.