473,705 Members | 6,195 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C# 2005 and simple graphics

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?
Aug 23 '06 #1
7 1739
Hello, Delme!

DGI have an empty project with a button on a form.

DGWhat code do I need to put behind the button to draw a rectangle on my
DGform 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
Aug 23 '06 #2
Thank you very much, having not done anything with graphics I thought I
would start somewhere simple.

Great link.

Delme.
"Vadym Stetsyak" <va*****@ukr.ne twrote in message
news:OZ******** ******@TK2MSFTN GP04.phx.gbl...
Hello, Delme!

DGI have an empty project with a button on a form.

DGWhat code do I need to put behind the button to draw a rectangle on my
DGform 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

Aug 23 '06 #3
You should also check out Bob Powell's GDI+ FAQ:

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

--
HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Surgery

It takes a tough man to make a tender chicken salad.
"Delme Greening" <this_part_is_r ubbish@de***@i-byte.co.ukwrote in message
news:%2******** **********@TK2M SFTNGP02.phx.gb l...
Thank you very much, having not done anything with graphics I thought I
would start somewhere simple.

Great link.

Delme.
"Vadym Stetsyak" <va*****@ukr.ne twrote in message
news:OZ******** ******@TK2MSFTN GP04.phx.gbl...
>Hello, Delme!

DGI have an empty project with a button on a form.

DGWhat code do I need to put behind the button to draw a rectangle on
my
DGform 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


Aug 23 '06 #4
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.A dd(b);
b.Click += new EventHandler(b_ Click);
}

// click event method
void b_Click(object sender, EventArgs e)
{
using (Graphics g = this.CreateGrap hics())
{
g.DrawEllipse(P ens.Black, this.ClientRect angle);
}
}

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(PaintEv entArgs e)
{
if (flag)
e.Graphics.Draw Ellipse(Pens.Bl ack, this.ClientRect angle);
}

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_r ubbish@de***@i-byte.co.ukwrote :
Thank you very much, having not done anything with graphics I thought I
would start somewhere simple.

Great link.

Delme.
"Vadym Stetsyak" <va*****@ukr.ne twrote in message
news:OZ******** ******@TK2MSFTN GP04.phx.gbl...
>Hello, Delme!

DGI have an empty project with a button on a form.

DGWhat code do I need to put behind the button to draw a rectangle on
my
DGform 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



--
Happy Coding!
Morten Wennevik [C# MVP]
Aug 23 '06 #5

"Delme Greening" <this_part_is_r ubbish@de***@i-byte.co.ukwrote in message
news:u1******** ******@TK2MSFTN GP06.phx.gbl...
>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
Aug 23 '06 #6
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Morten Wennevik" <Mo************ @hotmail.comwro te in message
news:op******** *******@tr024.b ouvet.no...
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.A dd(b);
b.Click += new EventHandler(b_ Click);
}

// click event method
void b_Click(object sender, EventArgs e)
{
using (Graphics g = this.CreateGrap hics())
{
g.DrawEllipse(P ens.Black, this.ClientRect angle);
}
}

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(PaintEv entArgs e)
{
if (flag)
e.Graphics.Draw Ellipse(Pens.Bl ack, this.ClientRect angle);
}

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_r ubbish@de***@i-byte.co.ukwrote :
Thank you very much, having not done anything with graphics I thought I
would start somewhere simple.

Great link.

Delme.
"Vadym Stetsyak" <va*****@ukr.ne twrote in message
news:OZ******** ******@TK2MSFTN GP04.phx.gbl...
>Hello, Delme!

DGI have an empty project with a button on a form.

DGWhat code do I need to put behind the button to draw a rectangle on
my
DGform 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



--
Happy Coding!
Morten Wennevik [C# MVP]
Aug 24 '06 #7
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
On Thu, 24 Aug 2006 17:31:32 +0200, Nicholas Paldino [.NET/C# MVP]
<mv*@spam.guard .caspershouse.c omwrote:
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.



--
Happy Coding!
Morten Wennevik [C# MVP]
Aug 25 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
1354
by: jfathman | last post by:
Hello, I am thinking of spending some holiday time reading the third edition of Programming C# by Jesse Liberty, using Visual C# .Net Standard 2003. But I see at www.oreilly.com that a new fourth edition of that book is due out in Feb 2005, and I see in this newsgroup that C# 2005 is forthcoming. Does it make sense to go ahead with the third edition of the book using
11
1709
by: Peter Oliphant | last post by:
I've been trying all morning to convert my 2003 project (managed) to 2005 (/clr since I have both managed and unmanaged code). I'm guessing I have tens of thousands of lines of code to change. Did a lot of converting '__gc' to 'ref', converting '*' to '^', converting 'new' to 'gcnew'. Of course this can't be done with a blanket replace, as my code has both managed and unmanaged segments. Thus I have to do them one-by-one. Also, things are...
0
1516
by: Dr. Zharkov | last post by:
Hello. To see the graphics of technology DirectX 9.0 SDK Update - June 2005 in project Visual Basic, WindowsApplication1 from Visual Studio 2005 Beta 1, in file My Project, MyApplication.vb in a method: <Global.System.Diagnostics.DebuggerStepThrough()> _ Protected Overrides Sub OnCreateMainForm()
3
2206
by: fripper | last post by:
Is there a way to use the windows bitblt function in a VB 2005 app? bitblt requires device context parameters for the source and destination controls but those are not used in VB 2005. Is there some way around this? I have a program that uses a timer ... when it fires I want to copy a small block on the screen to a particular location. Thanks
10
5730
by: Divvy Ballacks | last post by:
I just downloaded this "can't give it away" pile of sick on my 56Kbps modem (460Mb) and I can't believe how difficult it is to write a really simple program. All I wanna do is have a form with a button and a bitmap "1.bmp" in a picture box. When you clcik the button it changes the bitmap from "1.bmp" to " 2.bmp". How difficult can it be? Can anyone help?
4
3110
by: Carsten Schmitt | last post by:
Hello, I want to draw a simple pixel (i.e. a red pixel in the center of the screen), which is always in the foreground - even when running a fullscreen application like a DirectX game. I need this as an easy entry for programming later a HUD, which shows several informations, like date, time, company logo, etc. Any help or just a simple code example would be appreciated.
2
1769
by: =?Utf-8?B?Umlja1NlYW4=?= | last post by:
I have a long form within a panel which exceeds the maximum height of 876. I use the following code to print the form but it prints only the portion of the form that is displayed on the screen (max sized to 876). How do I print the entire form? Graphics mygraphics = this.CreateGraphics(); Size s = this.Size; memoryImage = new Bitmap(s.Width, s.Height, mygraphics); Graphics memoryGraphics = Graphics.FromImage(memoryImage); IntPtr dc1 =...
0
858
by: Robin9876 | last post by:
Using Graphics Server .Net v3.5 in a ASP.Net project worked correctly in VS 2003. The project has been upgraded to VS 2005 and now the Graphics Server control does not display in VS 2005 it displays the following error message. Error 199 'Chart' could not be initialized. Details: Object reference not set to an instance of an object. It does render a graph when the application is viewed in a web browser.
4
2443
by: AWW | last post by:
XP VB 2005 drawing line graphics in a PictureBox - from a simple minded former FORTRAN programmer. The help examples have the graphics drawing code (lines & rectangles) in the PictureBox paint event handler - making it a static display. But I don't want a static display - I want to draw dynamic data so how do I get the data to the event handler - if I need to do that. I have also done line drawings on the Form but those drawings have an...
0
8690
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9274
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9139
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7895
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5933
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4440
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4704
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3138
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2083
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.