none
How can I have blank space for drawing in my c# GUI?

    Question

  • Hi All

    I am new in c#,I need to have a blank space in my c# application for drawing ,I have used Bitmap class and write needed event handlers for mouse events ,but when I do the mouse movements doesnot happen anything in blank space,I dont know what is wrong

    please help
    thanks
    Monday, May 19, 2008 10:08 PM

Answers

  • I'm assuming your making a windows forms application.

     

    You can either draw directly on any form itself, or you can place a control (like a panel) in your GUI and use that as the drawing area as well as to handle the mouse events.

     

    Here is a very simple bit of code that draws a line and a circle on a panel, and a line directly on the form itself.

     

    In the designer I have added a single panel to the forms client area, which by default is named panel1.

     

    The key is to use the Graphics object that is created via the CreateGraphics method.

     

    using System;

    using System.Collections.Generic;

    using System.ComponentModel;

    using System.Data;

    using System.Drawing;

    using System.Text;

    using System.Windows.Forms;

    namespace WindowsFormsApplication1

    {

    public partial class Form1 : Form

    {

    public Form1()

    {

    InitializeComponent();

    }

    private void Form1_Paint(object sender, PaintEventArgs e)

    {

    Graphics GR = this.panel1.CreateGraphics();

    System.Drawing.Pen P = new System.Drawing.Pen(System.Drawing.Color.Black);

    GR.DrawEllipse(P,new Rectangle(10,10,50,50));

    GR.DrawLine(P,new Point(10,10),new Point (100,100));

     

    Graphics GR2 = this.CreateGraphics();

    GR2.DrawLine(P, new Point(0, 0), new Point(50, 50));

     

     

    }

    }

     

    }

     

    You can of course put bitmap images onto the same areas using the drawimage method of the Graphics class.

     

     

    Hope that helps!

    Tuesday, May 20, 2008 1:13 AM

All replies

  • Hi all

    I am new in c#,I want to create a blank space in my c# application like in paint program for drawing shapes ,I have used Bitmap class and I have wrote event handlers method for mouse events but when I click or move the mouse in that it doesnt do anything,can any body help me?

    Thanks
    Monday, May 19, 2008 9:37 PM
  • I think your in the wrong forum, but anyway use the "Search Microsoft.com for" textbox to search for "Scribble for C#".

    Monday, May 19, 2008 9:50 PM
  • I'm assuming your making a windows forms application.

     

    You can either draw directly on any form itself, or you can place a control (like a panel) in your GUI and use that as the drawing area as well as to handle the mouse events.

     

    Here is a very simple bit of code that draws a line and a circle on a panel, and a line directly on the form itself.

     

    In the designer I have added a single panel to the forms client area, which by default is named panel1.

     

    The key is to use the Graphics object that is created via the CreateGraphics method.

     

    using System;

    using System.Collections.Generic;

    using System.ComponentModel;

    using System.Data;

    using System.Drawing;

    using System.Text;

    using System.Windows.Forms;

    namespace WindowsFormsApplication1

    {

    public partial class Form1 : Form

    {

    public Form1()

    {

    InitializeComponent();

    }

    private void Form1_Paint(object sender, PaintEventArgs e)

    {

    Graphics GR = this.panel1.CreateGraphics();

    System.Drawing.Pen P = new System.Drawing.Pen(System.Drawing.Color.Black);

    GR.DrawEllipse(P,new Rectangle(10,10,50,50));

    GR.DrawLine(P,new Point(10,10),new Point (100,100));

     

    Graphics GR2 = this.CreateGraphics();

    GR2.DrawLine(P, new Point(0, 0), new Point(50, 50));

     

     

    }

    }

     

    }

     

    You can of course put bitmap images onto the same areas using the drawimage method of the Graphics class.

     

     

    Hope that helps!

    Tuesday, May 20, 2008 1:13 AM