Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / August 2006

Tip: Looking for answers? Try searching our database.

C# 2005 and simple graphics

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Delme Greening - 23 Aug 2006 11:48 GMT
I have an empty project with a button on a form.

What code do I need to put behind the button to draw a rectangle on my form
when it is clicked?
Vadym Stetsyak - 23 Aug 2006 11:55 GMT
Hello, Delme!

DG> I have an empty project with a button on a form.

DG> What code do I need to put behind the button to draw a rectangle on my
DG> form when it is clicked?

You will need to use GDI+
Have a look at  ( http://www.c-sharpcorner.com/2/gdi_plus.asp )

--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
Delme Greening - 23 Aug 2006 13:27 GMT
Thank you very much, having not done anything with graphics I thought I
would start somewhere simple.

Great link.

Delme.

> Hello, Delme!
>
[quoted text clipped - 9 lines]
> Regards, Vadym Stetsyak
> www: http://vadmyst.blogspot.com
Kevin Spencer - 23 Aug 2006 13:58 GMT
You should also check out Bob Powell's GDI+ FAQ:

http://www.bobpowell.net/gdiplus_faq.htm

Signature

HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Surgery

It takes a tough man to make a tender chicken salad.

> Thank you very much, having not done anything with graphics I thought I
> would start somewhere simple.
[quoted text clipped - 17 lines]
>> Regards, Vadym Stetsyak
>> www: http://vadmyst.blogspot.com
Morten Wennevik - 23 Aug 2006 14:21 GMT
Hi Delme,

Basically, all draing is done using a Graphics object, which can point to  
a control, your main form (which is also a control), or a Bitmap image and  
so on.

The quick and dirty way to draw using a button event is something like this

// constructor
        public Form1()
        {
            Button b = new Button();
            this.Controls.Add(b);
            b.Click += new EventHandler(b_Click);
        }

// click event method
        void b_Click(object sender, EventArgs e)
        {
            using (Graphics g = this.CreateGraphics())
            {
                g.DrawEllipse(Pens.Black, this.ClientRectangle);
            }
        }

But!!!, you should avoid drawing inside your button event for several  
reasons.
1) Any drawing will be lost if someting covers the window.
2) Using the graphics object of a control directly may cause unwanted side  
effects

Instead do something like this instead

        bool flag = false;
        void b_Click(object sender, EventArgs e)
        {
            flag = true;
            this.Refresh();
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            if (flag)
                e.Graphics.DrawEllipse(Pens.Black, this.ClientRectangle);
        }

OnPaint will be called each time the window needs to be repainted, like  
when it is uncovered.

If you swap 'flag = true' with 'flag = !flag' you show and hide the circle.

Good luck!

> Thank you very much, having not done anything with graphics I thought I
> would start somewhere simple.
[quoted text clipped - 17 lines]
>> Regards, Vadym Stetsyak
>> www: http://vadmyst.blogspot.com

Signature

Happy Coding!
Morten Wennevik [C# MVP]

Nicholas Paldino [.NET/C# MVP] - 24 Aug 2006 16:31 GMT
Morten,

   This isn't completely true.

   The drawing will be lost when a repaint is forced.  This can be due to
many factors, not just having the window covered.  You can resize the
window, you can have it covered, a call to invalidate the window could be
made, the display settings could be changed.

   Also, I am curious, what side effects are you getting by using the DC of
a control?  When you override the OnPaint method, or attach to the Paint
event, the DC of the control is exactly what you are getting.

Signature

         - Nicholas Paldino [.NET/C# MVP]
         - mvp@spam.guard.caspershouse.com

Hi Delme,

Basically, all draing is done using a Graphics object, which can point to
a control, your main form (which is also a control), or a Bitmap image and
so on.

The quick and dirty way to draw using a button event is something like this

// constructor
        public Form1()
        {
            Button b = new Button();
            this.Controls.Add(b);
            b.Click += new EventHandler(b_Click);
        }

// click event method
        void b_Click(object sender, EventArgs e)
        {
            using (Graphics g = this.CreateGraphics())
            {
                g.DrawEllipse(Pens.Black, this.ClientRectangle);
            }
        }

But!!!, you should avoid drawing inside your button event for several
reasons.
1) Any drawing will be lost if someting covers the window.
2) Using the graphics object of a control directly may cause unwanted side
effects

Instead do something like this instead

        bool flag = false;
        void b_Click(object sender, EventArgs e)
        {
            flag = true;
            this.Refresh();
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            if (flag)
                e.Graphics.DrawEllipse(Pens.Black, this.ClientRectangle);
        }

OnPaint will be called each time the window needs to be repainted, like
when it is uncovered.

If you swap 'flag = true' with 'flag = !flag' you show and hide the circle.

Good luck!

On Wed, 23 Aug 2006 14:27:56 +0200, Delme Greening
<this_part_is_rubbish@delme@i-byte.co.uk> wrote:

> Thank you very much, having not done anything with graphics I thought I
> would start somewhere simple.
[quoted text clipped - 17 lines]
>> Regards, Vadym Stetsyak
>> www: http://vadmyst.blogspot.com

Signature

Happy Coding!
Morten Wennevik [C# MVP]

Morten Wennevik - 25 Aug 2006 09:40 GMT
Nicholas,

Yes, but isn't it completely true that it will be lost when the control is  
covered?

As for unwanted side effects:

[How to use the CreateGraphics method.]
http://www.bobpowell.net/creategraphics.htm

> Morten,
>
[quoted text clipped - 10 lines]
> a control?  When you override the OnPaint method, or attach to the Paint
> event, the DC of the control is exactly what you are getting.

Signature

Happy Coding!
Morten Wennevik [C# MVP]

Mythran - 23 Aug 2006 17:39 GMT
>I have an empty project with a button on a form.
>
> What code do I need to put behind the button to draw a rectangle on my
> form when it is clicked?

Also, along with the other good replies, you may want to check out Managed
DirectX if you are gearing more towards Gaming.  If you are doing graphics
for business applications and don't need DirectX, then GDI+ is the way to go
:)

HTH,
Mythran

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.