473,320 Members | 2,006 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

System::Drawing::Graphics - how do I use in managed C++?

There doesn't seem to be any documentation on how to create and/or use an
instance of System::Drawing::Graphics. On-line MSDN talks about this class,
and says:

" The Graphics class provides methods for drawing objects to the display
device. A Graphics object is associated with a specific device context. "

Great. So how about explaining HOW to do this? Or perhaps a code example? Or
at least a CONSTRUCTOR (or some explanation on how to create an instance)?
For example, why isn't there a link on 'associated with a specific device
context' showing how to associate a device context with a Graphics object? A
lot of MSDN seems to be 'circular' and sometimes lacking practical info in
it's explanations...
Nov 17 '05 #1
4 4497
Peter,
There doesn't seem to be any documentation on how to create and/or use an
instance of System::Drawing::Graphics. On-line MSDN talks about this
class, and says:

" The Graphics class provides methods for drawing objects to the display
device. A Graphics object is associated with a specific device context. "

Great. So how about explaining HOW to do this? Or perhaps a code example?
Or at least a CONSTRUCTOR (or some explanation on how to create an
instance)? For example, why isn't there a link on 'associated with a
specific device context' showing how to associate a device context with a
Graphics object? A lot of MSDN seems to be 'circular' and sometimes
lacking practical info in it's explanations...


Normally, you don't create a Graphics object yourself, but instead get
handed one during the Paint event for the control/form you're painting (or
if you overload the OnPaint() method of the control/form).

If you need one, I usually just use the CreateGraphics() method of the
Control class on the control I want to paint on.
--
Tomas Restrepo
to****@mvps.org
http://www.winterdom.com/
Nov 17 '05 #2
Maybe I should just say what I'm trying to do...hehe.

I want to draw points, lines, rectangles, and ellipses (solid/wire-frame)
of a given color (and possibly thickness) similar to how a control is placed
on a container/form.

To date I've created a solid rectangle by 'cheating' in the following way: I
created a Label with no text in it! This can then be used to create a line
by making it thin. This doesn't seem like it's the way it should be done,
but I can't find anyway to just draw a line on the form (which I thought was
what the Graphics class was for)...

How do I do this 'correctly'?

[==P==]

"Tomas Restrepo (MVP)" <to****@mvps.org> wrote in message
news:ek**************@TK2MSFTNGP12.phx.gbl...
Peter,
There doesn't seem to be any documentation on how to create and/or use an
instance of System::Drawing::Graphics. On-line MSDN talks about this
class, and says:

" The Graphics class provides methods for drawing objects to the display
device. A Graphics object is associated with a specific device context. "

Great. So how about explaining HOW to do this? Or perhaps a code example?
Or at least a CONSTRUCTOR (or some explanation on how to create an
instance)? For example, why isn't there a link on 'associated with a
specific device context' showing how to associate a device context with a
Graphics object? A lot of MSDN seems to be 'circular' and sometimes
lacking practical info in it's explanations...


Normally, you don't create a Graphics object yourself, but instead get
handed one during the Paint event for the control/form you're painting (or
if you overload the OnPaint() method of the control/form).

If you need one, I usually just use the CreateGraphics() method of the
Control class on the control I want to paint on.
--
Tomas Restrepo
to****@mvps.org
http://www.winterdom.com/

Nov 17 '05 #3
"Peter Oliphant" <po*******@RoundTripInc.com> wrote in message
news:OF**************@tk2msftngp13.phx.gbl...
Maybe I should just say what I'm trying to do...hehe.

I want to draw points, lines, rectangles, and ellipses (solid/wire-frame)
of a given color (and possibly thickness) similar to how a control is
placed on a container/form.


To go over a little background:

There are two times when you need to "paint" your form.

One is when the window manager tells you that you must. It will do this
whenever it detects that there is a change to that portion of the desktop
enclosed by your form. For example when your form is first created the
window manager realizes that what was "under" your form before should now be
replaced by something of your application's choosing. Or as another example,
when the user moves a window that uncovers your form the window manager
realizes that there is an update that needs to be made to the form.

At the level of the API, the window manager sends a WM_PAINT message to a
window. At the managed code level, the window manager calls the OnPaint()
method of the form. It is there where you need to implement the bulk your
paint algorithm.

The other time to paint is when your application knows that its form needs a
new paint job. As Tomas has already pointed out, you can do that at any time
by calling CreateGraphics().

To simplify an application's code, sometimes only the first case is actually
implemented. To deal with application / user requested paints an application
can simply call the Invlidate() method of the Form class. This causes the
window manager to dispatch a WM_PAINT message natively or the OnPaint()
method on the .Net platform.

Of course, in your case, your application must keep track of all the lines,
ellipses etc that it needs to paint. The window manager can't possibly do
that.

In case you are new to this stuff, I've pasted the code to a managed
application that draws some text, a line and an ellipse. I'm not sure
posting it is wise because it is a little off the beaten path.

You see I am going to listen to Charles Petzold (
http://www.charlespetzold.com/ ) speak this week on the topic of whether
using the wizards in Visual Studio rots the mind. :-) I've thought about
that in the past have been thinking about it more in advance of his talk. So
my little sample does WinForms in Managed C++ _without_ using _any_ of the
wizards in VS.

To do that, I choose to create an _empty_ WIN32 APPLICATION and then I set
the "Use Managed Extensions" option manually.

Regards,
Will

// Class implementation Po2.cpp

#include "po2.h"

void HelloWorld::Main()
{
Application::Run( new HelloWorld() );
}

HelloWorld::HelloWorld()
{
Text = "Hello, world!";
BackColor = Color::White;

penRed = new Pen(Color::Red);
penBlue = new Pen(Color::Blue);
}

void HelloWorld::OnPaint(PaintEventArgs *pea)
{
Graphics __gc *grfx = pea->Graphics;
Rectangle rc;

grfx->DrawString("Hello, World!", Font, Brushes::Black, 0, 0);

rc = get_ClientRectangle();
rc.Width -= 1;
rc.Height -= 1;

grfx->DrawLine(penRed, 0, 0, rc.Width, rc.Height);

grfx->DrawEllipse(penBlue, rc);
}

// Hack avoids including <windows.h>

int __stdcall WinMain(int, int, int, int)
{
HelloWorld::Main();
return 0;
}

// Class declaration po2.h

#using <System.dll>
#using <mscorlib.dll>
#using <System.Drawing.dll>
#using <System.Windows.Forms.dll>

using namespace System;
using namespace System::Drawing;
using namespace System::Windows::Forms;
using namespace System::ComponentModel;

public __gc class HelloWorld : public Form
{
private:

Pen __gc *penRed;
Pen __gc *penBlue;

public:

HelloWorld();
static void Main();

protected:

void OnPaint(PaintEventArgs __gc *);
};

Nov 17 '05 #4
Thanx, William! : )

[==P==]

"William DePalo [MVP VC++]" <wi***********@mvps.org> wrote in message
news:OW**************@TK2MSFTNGP12.phx.gbl...
"Peter Oliphant" <po*******@RoundTripInc.com> wrote in message
news:OF**************@tk2msftngp13.phx.gbl...
Maybe I should just say what I'm trying to do...hehe.

I want to draw points, lines, rectangles, and ellipses
(solid/wire-frame) of a given color (and possibly thickness) similar to
how a control is placed on a container/form.


To go over a little background:

There are two times when you need to "paint" your form.

One is when the window manager tells you that you must. It will do this
whenever it detects that there is a change to that portion of the desktop
enclosed by your form. For example when your form is first created the
window manager realizes that what was "under" your form before should now
be replaced by something of your application's choosing. Or as another
example, when the user moves a window that uncovers your form the window
manager realizes that there is an update that needs to be made to the
form.

At the level of the API, the window manager sends a WM_PAINT message to a
window. At the managed code level, the window manager calls the OnPaint()
method of the form. It is there where you need to implement the bulk your
paint algorithm.

The other time to paint is when your application knows that its form needs
a new paint job. As Tomas has already pointed out, you can do that at any
time by calling CreateGraphics().

To simplify an application's code, sometimes only the first case is
actually implemented. To deal with application / user requested paints an
application can simply call the Invlidate() method of the Form class. This
causes the window manager to dispatch a WM_PAINT message natively or the
OnPaint() method on the .Net platform.

Of course, in your case, your application must keep track of all the
lines, ellipses etc that it needs to paint. The window manager can't
possibly do that.

In case you are new to this stuff, I've pasted the code to a managed
application that draws some text, a line and an ellipse. I'm not sure
posting it is wise because it is a little off the beaten path.

You see I am going to listen to Charles Petzold (
http://www.charlespetzold.com/ ) speak this week on the topic of whether
using the wizards in Visual Studio rots the mind. :-) I've thought about
that in the past have been thinking about it more in advance of his talk.
So my little sample does WinForms in Managed C++ _without_ using _any_ of
the wizards in VS.

To do that, I choose to create an _empty_ WIN32 APPLICATION and then I set
the "Use Managed Extensions" option manually.

Regards,
Will

// Class implementation Po2.cpp

#include "po2.h"

void HelloWorld::Main()
{
Application::Run( new HelloWorld() );
}

HelloWorld::HelloWorld()
{
Text = "Hello, world!";
BackColor = Color::White;

penRed = new Pen(Color::Red);
penBlue = new Pen(Color::Blue);
}

void HelloWorld::OnPaint(PaintEventArgs *pea)
{
Graphics __gc *grfx = pea->Graphics;
Rectangle rc;

grfx->DrawString("Hello, World!", Font, Brushes::Black, 0, 0);

rc = get_ClientRectangle();
rc.Width -= 1;
rc.Height -= 1;

grfx->DrawLine(penRed, 0, 0, rc.Width, rc.Height);

grfx->DrawEllipse(penBlue, rc);
}

// Hack avoids including <windows.h>

int __stdcall WinMain(int, int, int, int)
{
HelloWorld::Main();
return 0;
}

// Class declaration po2.h

#using <System.dll>
#using <mscorlib.dll>
#using <System.Drawing.dll>
#using <System.Windows.Forms.dll>

using namespace System;
using namespace System::Drawing;
using namespace System::Windows::Forms;
using namespace System::ComponentModel;

public __gc class HelloWorld : public Form
{
private:

Pen __gc *penRed;
Pen __gc *penBlue;

public:

HelloWorld();
static void Main();

protected:

void OnPaint(PaintEventArgs __gc *);
};

Nov 17 '05 #5

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

Similar topics

1
by: news.microsoft.com | last post by:
Hello group, My goal is to attach an image over another image. Top image should be transparent so the back image is visible through the top one. Bellow is a test code in VB.NET. You need to...
3
by: Barry Anderberg | last post by:
I'm using the .NET Memory Profiler by Sci Tech and I wrote a little test application to verify something odd I observed and it appears that System.Drawing.Font fails to dispose of its FontFamily. ...
1
by: Paul Hoad | last post by:
I'm trying to use MeasureString() to determine the length in pixels of a string However to do I need a System.Drawing.Graphics object to do this I need to create a System.Drawing.Graphics...
1
by: DBC User | last post by:
Hi Sharpies, This is a very basic question. I am using .Net IDE 2003 and System.Drawing.dll (1.1.4322). I wrote a simple program like this (ommitting the commons) using System.Drawing; .....
1
by: Hadar | last post by:
Hi, I'm getting "object is currently in use elsewhere" when I use System.Drawing.Graphics.MesureString. This is what I do: My controls use a utility class the helps it to mesure strings. To...
3
by: Qwert | last post by:
Hello, I want to draw from one System.Drawing.Bitmap to another. All I can find is how to draw to a Graphics object. System.Drawing, System.Drawing.Drawing2D or System.Drawing.Imaging have no...
3
by: Eduard Witteveen | last post by:
Hello list, I have code the draw MyDrawingObject information on a System.Drawing.Graphics object. The code is more/less the following: I now want to rotate / mirror the object i draw. I've...
5
by: Dave Bootsma | last post by:
I have an application where I want to redraw a polygon from points I retrieve from a file. The file is read with streamreader line by line each line contains the points for each polygon. Below is...
2
by: ThatsIT.net.au | last post by:
I have this code that writes a pie chart in a asp.net page, but I want to use it in a server control. When I try I get a error on the last line "Response.OutputStream" Obviously there is no...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.