Simple graphics in win forms

This is a discussion on Simple graphics in win forms within the C# Programming forums, part of the General Programming Boards category; Hey What is the easiest way to make simple graphical figures in Win forms (basic geometrical figures like rectangles, triangles, ...

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    5

    Simple graphics in win forms

    Hey

    What is the easiest way to make simple graphical figures in Win forms (basic geometrical figures like rectangles, triangles, circles etc.).

    TNX

  2. #2
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    Get a DC to some component (probably the form itself) and paint to it. Check out the Graphics class on msdn.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Location
    england
    Posts
    209
    As mentioned use the Graphics class built into .net. It's basically a GDI+ wrapper and once you get into it, you can have a lot of fun. Here's a little starter to point you in the right direction.

    Code:
    using System.Drawing;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication2
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                this.Paint += new PaintEventHandler(Form1_Paint);
            }
    
            void Form1_Paint(object sender, PaintEventArgs e)
            {
                Graphics g = e.Graphics;
                g.FillRectangle(Brushes.Green, new Rectangle(0, 0, 100, 100));
                g.FillEllipse(Brushes.Red, new Rectangle(10, 10, 50, 50));
            }
        }
    }

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    5
    How can I use these graphics as "marks" on the form (e.g when I click on it, a label or a textbox shows the size of the rectangle in pixels).

  5. #5
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    Well, when you draw a rectangle you also save it's size. Then on a mouse event handler you see if that point is inside of the rectangle. If it's inside the rectangle, use that rectangles dimensions to set the Text property of some control.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. egavga.bgi problem
    By sunil21 in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 09-22-2003, 05:06 PM
  2. simple graphics in C... ??
    By imbecile in C in forum C Programming
    Replies: 8
    Last Post: 07-12-2003, 09:51 AM
  3. Simple simple graphics
    By triplem in forum C Programming
    Replies: 2
    Last Post: 05-19-2003, 02:52 AM
  4. Simple Graphics in C
    By Harry albertys in forum C Programming
    Replies: 3
    Last Post: 03-18-2003, 09:14 AM
  5. Can I get this to loop back?
    By Unregistered in forum C Programming
    Replies: 9
    Last Post: 05-07-2002, 03:34 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21