C # Getting started learning ----- simple drawing program

Source: Internet
Author: User

This example shows how to draw various shapes on a form, such as a rectangle, an ellipse, a line, or a text. The running effect is as follows:

 

Implementation process:

(1) create a form application

(2) Add a MenuScrip control and a ToolScrip control.

Change the DisplayStyle attribute to Text for each cell in the ToolScrip control.

 

(3) program code.

1. When creating a menu event, the background of the form is cleared in white to implement the "File Creation" function.

[Csharp]
Private void create ToolStripMenuItem_Click (object sender, EventArgs e)
{
Graphics g = this. CreateGraphics ();
G. Clear (backColor );
ToolStrip1.Enabled = true;
// Create a Bitmap
TheImage = new Bitmap (this. ClientRectangle. Width, this. ClientRectangle. Height );
EditFileName = "new file ";
// Modify the window title
This. Text = "MyDraw \ t" + editFileName;
Ig = Graphics. FromImage (theImage );
Ig. Clear (backColor );
}

 

2. Open the event to open the "open file" dialog box, select the corresponding image, and draw the image to the form.

 

 

[Csharp]
Private void open ToolStripMenuItem_Click (object sender, EventArgs e)
{
OpenFileDialog1.Multiselect = false;
If (openFileDialog1.ShowDialog () = DialogResult. OK)
{
// Modify the window title
This. Text = "MyDraw \ t" + openFileDialog1.FileName;
EditFileName = openFileDialog1.FileName;
TheImage = Image. FromFile (openFileDialog1.FileName );
Graphics g = this. CreateGraphics ();
G. DrawImage (theImage, this. ClientRectangle );
Ig = Graphics. FromImage (theImage );
Ig. DrawImage (theImage, this. ClientRectangle );
// ToolBar can be used
ToolStrip1.Enabled = true;
}
}


(3) Save the Click Event of the menu item to save the image in BMP format as the background of the form.

[Csharp]
Private void save ToolStripMenuItem_Click (object sender, EventArgs e)
{
SaveFileDialog1.Filter = "image (*. bmp) | *. bmp ";
SaveFileDialog1.FileName = editFileName;
If (saveFileDialog1.ShowDialog () = DialogResult. OK)
{
TheImage. Save (saveFileDialog1.FileName, ImageFormat. Bmp );
This. Text = "MyDraw \ t" + saveFileDialog1.FileName;
EditFileName = saveFileDialog1.FileName;
}
}

(4) In the Paint event, draw the Image saved in the Image.

[Csharp]
Private void Form1_Paint (object sender, PaintEventArgs e)
{
// Draw the Image saved in the Image
Graphics g = this. CreateGraphics ();
If (theImage! = Null)
{
G. Clear (Color. White );
G. DrawImage (theImage, this. ClientRectangle );
}
}

(5) Add the Frm_Text.cs text input box.

Add a Window form named Frm_Text and modify the properties of the form:

Change the FormBorderStyle attribute to None;

 

Change the Modifiers attribute to Public.

 

(6) In the form MouseDown event, if the string is currently drawn, the text box is displayed at the current position of the mouse; if the drawing is open, set the starting position of the image.

[Cpp]
Private void Frm_Main_MouseDown (object sender, MouseEventArgs e)
{
If (e. Button = MouseButtons. Left)
{
// If you select text input, the strInput form is opened.
If (drawTool = drawTools. String)
{
Frm_Text inputBox = new Frm_Text ();
InputBox. StartPosition = FormStartPosition. CenterParent;
If (inputBox. ShowDialog () = DialogResult. OK)
{
Graphics g = this. CreateGraphics ();
Font theFont = this. Font;
G. DrawString (inputBox. textBox1.Text, theFont, new SolidBrush (foreColor), e. X, e. Y );
Ig. DrawString (inputBox. textBox1.Text, theFont, new SolidBrush (foreColor), e. X, e. Y );
}
}
// If you start to draw, record the mouse position
Else if (isDrawing =! IsDrawing) = true)
{
StartPoint = new Point (e. X, e. Y );
OldPoint = new Point (e. X, e. Y );
}
}
}
 

(7) In the form MouseMove event, draw the specified image based on the size of the mouse movement.

[Cpp]
Private void form=mousemove (object sender, MouseEventArgs e)
{
Graphics g;
G = this. CreateGraphics ();
 
If (isDrawing)
{
Switch (drawTool)
{
Case drawTools. None:
Break;
Case drawTools. Pen:
// Draw a line segment from the previous point to the current point
G. DrawLine (new Pen (foreColor, 1), oldPoint, new Point (e. X, e. Y ));
Ig. DrawLine (new Pen (foreColor, 1), oldPoint, new Point (e. X, e. Y ));
OldPoint. X = e. X;
OldPoint. Y = e. Y;
Break;
Case drawTools. Line:
// Restore the image before this operation, and then add Line
This. Frm_Main_Paint (this, new PaintEventArgs (this. CreateGraphics (), this. ClientRectangle ));
G. DrawLine (new Pen (foreColor, 1), startPoint, new Point (e. X, e. Y ));
Break;
Case drawTools. Ellipse:
// Restore the image before this operation, and then add Ellipse
This. Frm_Main_Paint (this, new PaintEventArgs (this. CreateGraphics (), this. ClientRectangle ));
G. DrawEllipse (new Pen (foreColor, 1), startPoint. X, startPoint. Y, e. X-startPoint. X, e. Y-startPoint. Y );
Break;
Case drawTools. Rectangle:
// Restore the image before this operation, and then add the Rectangle
This. Frm_Main_Paint (this, new PaintEventArgs (this. CreateGraphics (), this. ClientRectangle ));
G. DrawRectangle (new Pen (foreColor, 1), startPoint. X, startPoint. Y, e. X-startPoint. X, e. Y-startPoint. Y );
Break;
Case drawTools. String:
Break;
Case drawTools. Rubber:
// Draw a wide line segment with the background color
G. DrawLine (new Pen (backColor, 20), oldPoint, new Point (e. X, e. Y ));
Ig. DrawLine (new Pen (backColor, 20), oldPoint, new Point (e. X, e. Y ));
OldPoint. X = e. X;
OldPoint. Y = e. Y;
Break;
}
}
}

(8) In the form MouseUp event, draw a straight line, an ellipse, or a rectangle Based on the selected paint brush.

[Csharp]
Private void form=mouseup (object sender, MouseEventArgs e)
{
IsDrawing = false;
Switch (drawTool)
{
Case drawTools. Line:
Ig. DrawLine (new Pen (foreColor, 1), startPoint, new Point (e. X, e. Y ));
Break;
Case drawTools. Ellipse:
Ig. DrawEllipse (new Pen (foreColor, 1), startPoint. X, startPoint. Y, e. X-startPoint. X, e. Y-startPoint. Y );
Break;
Case drawTools. Rectangle:
Ig. DrawRectangle (new Pen (foreColor, 1), startPoint. X, startPoint. Y, e. X-startPoint. X, e. Y-startPoint. Y );
Break;
}
}

========================================================== ========================================================== ==================================

The following explains why the line moves with the mouse when a straight line is pulled, and when you select a paint brush, move the mouse to draw a line.

There are two Graphics:

(1) Real Scenario graphics. Just the picture above is what I see.

Eg:

Private void Form1_Paint (object sender, PaintEventArgs e)
{
// Draw the Image saved in the Image
Graphics g = this. CreateGraphics ();

(2) Graphic used for temporary storage.

It is created when it is created:

[Csharp]
// Create a Bitmap
TheImage = new Bitmap (this. ClientRectangle. Width, this. ClientRectangle. Height );
EditFileName = "new file ";
// Modify the window title
This. Text = "MyDraw \ t" + editFileName;
Ig = Graphics. FromImage (theImage );
Ig. Clear (backColor );

Display: Display theImage to display the previously saved items in ig.

[Csharp]
Private void Frm_Main_Paint (object sender, PaintEventArgs e)
{
// Draw the Image saved in the Image
Graphics g = this. CreateGraphics ();
If (theImage! = Null)
{
G. Clear (Color. White );
G. DrawImage (theImage, this. ClientRectangle );
}
}

In Form1_MouseMove

If it is a paint brush, save the image to two graphic, so that we can see the moving painting, and finally show all the painting.

If it is a straight line or rectangle, it is first drawn to the first graphics, and only to the second graphic when the mouse is opened.

 

 

From the column chenyujing1234

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.