Mouse drawing

xiaoxiao2021-03-06  76

Picture with a mouse in the form

(Author: Zhang Jun-hong)

Let's define a class and an interface in the same namespace:

using System; using System.Collections.Generic; using System.Text; using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters; using System.Runtime.Serialization.Formatters.Binary;

Public interface ishape {void Draw (Graphics G);} // This class provides a method of drawing line [serializable] public class line: ishape {point startp, endp; public line (Point SP, Point EP) {this.startp = SP } Public void draw (graphics g) {g.drawline (pens.black, startp, endp);}}

Create a new form: Form1

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Windows.Forms; using System.IO; using System.Runtime.Serialization; using System.Collections;

/ / Define window body level common variables

// Start the starting point coordinates of the line, the value of this variable will determine Point StartPoint in the mousedown event; // Define an ArrayList for storage of object private arraylist shapes = new arraylist ();

Protected Override Void OnPaint (Painteventargs E) {Graphics G = E.Graphics; // GetENUMERATOR Returns the enumeration number object, this object is class ienumerator shapeenum = shapes.GeteNumerator (); // while is looking for a suitable class and utilizing Its DRAW method while (Shapeenum.Movenext ()) {ishape shape = (ishape) shapeenum.current; // uses the related class DRAW method, // is useful when we define multiple methods in front, we can also add some // DrawRectangle, etc. / / Here we only define a method (drawer).

Shape.draw (g);}}

Private void form1_mousedown (object sender, mouseeventargs e) {if (e.button == system.windows.forms.mousebuttons.left) {// mouse Press to determine the coordinates of the starting point, put this variable to the StartPoint value StartPoint = New Point (EX, EY); Add the LINE class to the defined ArraryList to // OnPaint event processing. Shapes.add (new line (startpoint, endpp));} // This sentence is critical, there is no such thing as you can't see your line. // onpaint event, is in the form of activation, change, size, loading, and refresh, so when our action is completed, the form must be // refreshed. THIS.REFRESH ();

After the new form is created, you should pay attention to the background of the form to white !!

转载请注明原文地址:https://www.9cbs.com/read-120796.html

New Post(0)