eHow launches Android app: Get the best of eHow on the go.

How To

How to Draw a String, Square, Rectangle, Circle, Ellipse, and Polygon in Microsoft Visual C# (C Sharp)

Member
By Derek Hardenbergh
User-Submitted Article
(0 Ratings)
Draw a String, Square, Rectangle, Circle, Ellipse, and Polygon in Microsoft Visual C# (C Sharp)
Draw a String, Square, Rectangle, Circle, Ellipse, and Polygon in Microsoft Visual C# (C Sharp)

Draw strings, squares, rectangles, circles, ellipses, and polygons in Microsoft Visual C#.

Difficulty: Moderate
Instructions

Things You'll Need:

  • Microsoft Visual C# 2008 Express (free)
  1. Step 1

    Note: This article assumes you have installed Microsoft Visual C# 2008 Express Edition. You may download it for free from here: http://www.microsoft.com/express/download/

    Open Microsoft Visual C#. Click on "Project..." to the right of Create in the Recent Projects area of the Start Page.

    The New Project window will open. Click on "Windows Forms Application", enter a Name, and click OK.

    By default, the only form in the project will be called "Form1" and you will be in Design mode for that form.

  2. Step 2

    Right click on the form and select Properties.

  3. Step 3

    In the Properties Window, click the button with the lightning bolt. This will show the Events for the form.

  4. Step 4

    Find the event named Paint and double click the empty cell to it's right. The Paint event is in the Appearance category.

  5. Step 5

    You will now be in the Form1_Paint method. This method is called any time the form is moved, resized, or restored.

  6. Step 6

    Add the following code in the Form1_Paint method:

    // DrawString(string s, Font font, Brush brush, float x, float y)
    e.Graphics.DrawString("C# rocks!", new Font("Arial", 12), Brushes.White, 15, 10);
    e.Graphics.DrawString("C# rocks!", new Font("Arial", 12), Brushes.Black, 16, 11);

    // FillRectangle(Brush brush, int x, int y, int width, int height)
    e.Graphics.FillRectangle(Brushes.White, 15, 35, 50, 50);
    e.Graphics.DrawRectangle(Pens.Red, 15, 35, 50, 50);

    // FillEllipse(Brush brush, int x, int y, int width, int height)
    e.Graphics.FillEllipse(Brushes.White, 15, 100, 50, 50);
    e.Graphics.DrawEllipse(Pens.Red, 15, 100, 50, 50);

    // FillPolygon(Brush brush, Point[] points)
    e.Graphics.FillPolygon(Brushes.White, new Point[3] { new Point(10, 210), new Point(40, 160), new Point(70, 210) });
    e.Graphics.DrawPolygon(Pens.Red, new Point[3] { new Point(10, 210), new Point(40, 160), new Point(70, 210) });

    First we will draw a string with a shadow effect. The second parameter is a new Font object.

    Then we draw various shapes. Note the the Draw methods use Pens and that Fill methods use Brushes.

    Also note that the polygon methods take an array of Point objects. Each Point is an X and Y integer that represents the units away from the upper left corner.

  7. Step 7

    Go up to the toolbar and run the program by clicking on the Start Debugging (f5) play button.

    NOTE: If you got any kind of error after clicking the play button, you probably made a syntax error when typing the code. Reread the code until you find and correct the error and try again.

  8. Step 8

    The form will take a moment and then pop up. Not bad.

Tips & Warnings
  • Play around with the Help. Tons of good stuff in there.
  • You might get addicted to C#. Watch out Java and C++ folks!!!

Post a Comment

Post a Comment
  • Have you done this? Click here to let us know.
I Did This

Related Ads

Tags
Computers
Alexia Petrakos,

Meet Alexia Petrakos eHow's Computers Expert.

Get Free Computers Newsletters

Copyright © 1999-2009 eHow, Inc. Use of this web site constitutes acceptance of the eHow Terms of Use and Privacy Policy.   en-US Portions of this page are modifications based on work created and shared by Google and used according to terms described in the Creative Commons 3.0 Attribution License.

eHow Computers
eHow_eHow Technology and Electronics