Click here to Skip to main content
15,167,925 members
Articles / Desktop Programming / Win32
Posted 7 Jan 2008


30 bookmarked

2D Drawing Plug-in

Rate me:
Please Sign up or sign in to vote.
2.60/5 (6 votes)
14 Feb 2008CPOL2 min read
Draw primitive graphics on your window.

Image 1


This is a class library that implements a user control that acts as a plug-in. This plug-in encapsulates basic drawing functionalities and exposes an interface to retrieve the drawing in the form of two dimensional coordinate values. The user can just drag and drop this component from the Toolbox to any control (Container type). Immediately after, it adds the capability of drawing basic graphical objects using mouse commands.


The idea of developing this kind of a plug-in is to reduce the effort which comes every time a developer develops an application where he/she needs graphical drawing inputs from the user and has to do some processing over them. For example, an application to calculate the intersection of some polygons like circles, rectangles etc. Writing code every time to allow the user to draw objects and reflect them on the window will waste a lot of time of the developer. Using this component enables the developer to start his work on his own algorithms and not the drawing code.

Using the code

The code can be divided into these sections:

  1. Register for the events.
  2. Draw on the window Device Context and memory bitmap.
  3. Calculate the update region for each type of drawing.
  4. Modify parameters using right clicks.

1. Register for the events

The plug-in component registers the parent control event for drawing purposes.

this.Parent.MouseDown += new MouseEventHandler(Parent_MouseDown);
this.Parent.MouseMove += new MouseEventHandler(Parent_MouseMove);
this.Parent.MouseUp += new MouseEventHandler(Parent_MouseUp);
this.Parent.Paint += new PaintEventHandler(Parent_Paint);

2. Drawing on the window Device Context and memory bitmap

void DrawOnMem(Graphics g)
    g.SmoothingMode = SmoothingMode.HighQuality;
    Pen pen = new Pen(penColor, penWidth);
    Brush brush = new SolidBrush(Color.FromArgb(127, brushColor));

    if (startPoint != Point.Empty && endPoint != Point.Empty)
        switch (drawType)
            case type.LINE:
                g.DrawLine(pen, startPoint, endPoint);
            case type.CIRCLE:
                if (Fill.Checked)
                    g.FillEllipse(brush, Rectangle(startPoint, endPoint));
                g.DrawEllipse(pen, Rectangle(startPoint, endPoint));
            case type.RECT:
                if (Fill.Checked)
                    g.FillRectangle(brush, Rectangle(startPoint, endPoint));
                g.DrawRectangle(pen, Rectangle(startPoint, endPoint));
            case type.FREEHAND:
                g.DrawLine(pen, startPoint, endPoint);
            case type.NONE:

3. Calculate the update region for each type of drawing

Calculating the optimum size region for each type of drawing is done to stop flickering during the drawing of objects. FindEnclosingRect calculates the immediate outer region of a line.

private Region FindEnclosingRect(Point st,Point en)
    double r = Math.Sqrt(Math.Pow((en.Y - st.Y), 2) + Math.Pow((en.X - st.X), 2));
    Rectangle rc = new Rectangle(0, 0, (int)r, 0);
    rc.Inflate((int)penWidth+1, (int)penWidth+1);
    Region rgn = new Region(rc);

    double m = (((double)en.Y - st.Y))/ (en.X - st.X);
    double angle = Math.Atan(m);

    if (en.X < st.X)
        angle += 3.14;
    Matrix mtrx = new Matrix();
    mtrx.Rotate((float)(angle*180 * 7 / 22));
    mtrx.Translate(st.X, st.Y);

    return rgn; 

There are some other functions as well, mainly calculates the bounding region of rectangles, circles, and lines, while the mouse move draws these objects on the window Device Context, and once the drawing finishes, stores the drawing in the memory device context.

Points of interest

If the developer wants the 2D coordinates of each drawing, then just uncomment the line which stores the coordinates in the arrays and read at runtime.


No updates..... till 07/01/2008.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

vikas maan
Software Developer (Senior)
India India
Vikas Maan is a software developer for last 4 years working on windows platform with multiple programming languages mainly VC++, etc.

Comments and Discussions

GeneralError on paint Pin
jamesyla18-Sep-09 2:11
Memberjamesyla18-Sep-09 2:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.