Problems with this->CreateGraphics()

S

saunderl

Hello Everyone,

I'm trying to just play with managed VC++. I just want to draw a box
on the form when it is clicked. Here is my click event handler

System::Void Test_Click(System::Object^ sender, System::EventArgs^ e)

{
// Create a Graphics object for the Control.
Graphics* g = this->CreateGraphics() ;

g->DrawRectangle(gcnew Pen(Color::Green, 1.0), 1, 1, 100, 100);
}

Trying to create a graphics object this way (Graphics* g =
this->CreateGraphics();)
I get this error:

Error 1 error C3699: '*' : cannot use this indirection on type
'System::Drawing::Graphics' c:\documents and settings\saul01\my
documents\visual studio
2005\projects\cc2\managedxp\managedxp\Test.h 77


Trying to create a graphics object this way (Graphics g =
this->CreateGraphics();)

Error 1 error C3767: 'System::Drawing::Graphics::Graphics': candidate
function(s) not accessible c:\documents and settings\saul01\my
documents\visual studio
2005\projects\cc2\managedxp\managedxp\Test.h 77


I'm using VS2005 and all the other code was generated by Visual Studio.

Thanks for the help, L. Lee Saunders
 
P

Peter Oliphant

You can only draw your box while in the Paint event of your form. Thus, you
need something along the lines of (typed from memory, so might be a little
off):

void OnPaint( Object^, PaintEventArgs^ e ) override
{
Graphics graphics = e->Graphics ;
graphics->DrawRectangle(...) ;
}

However, the error you got is becuase you're mixing syntaxes. '*' is the
pointer character in 'old style' syntax, you need to use '^' in the new
syntax. And note you're (trying to) using both at the same time! : )

[==P==]
 
S

saunderl

Hello Everyone,

Further more, I cannot even create variable of type Graphics even in:

System::Void Test_Paint(System::Object^ sender,
System::Windows::Forms::paintEventArgs^ e)

I get the same errors. I can though draw using:

e->Graphics->DrawRectangle(gcnew Pen(Color::Green, 1.0), 1, 1, 100,
100);

Thanks for the help, L. Lee Saunders
 
P

Peter Oliphant

correction:

void OnPaint( Object^, PaintEventArgs^ e ) override
{
Graphics^ graphics = e->Graphics ; // important!!! :)
graphics->DrawRectangle(...) ;
}

Peter Oliphant said:
You can only draw your box while in the Paint event of your form. Thus,
you need something along the lines of (typed from memory, so might be a
little off):

void OnPaint( Object^, PaintEventArgs^ e ) override
{
Graphics graphics = e->Graphics ;
graphics->DrawRectangle(...) ;
}

However, the error you got is becuase you're mixing syntaxes. '*' is the
pointer character in 'old style' syntax, you need to use '^' in the new
syntax. And note you're (trying to) using both at the same time! : )

[==P==]

Hello Everyone,

I'm trying to just play with managed VC++. I just want to draw a box
on the form when it is clicked. Here is my click event handler

System::Void Test_Click(System::Object^ sender, System::EventArgs^ e)

{
// Create a Graphics object for the Control.
Graphics* g = this->CreateGraphics() ;

g->DrawRectangle(gcnew Pen(Color::Green, 1.0), 1, 1, 100, 100);
}

Trying to create a graphics object this way (Graphics* g =
this->CreateGraphics();)
I get this error:

Error 1 error C3699: '*' : cannot use this indirection on type
'System::Drawing::Graphics' c:\documents and settings\saul01\my
documents\visual studio
2005\projects\cc2\managedxp\managedxp\Test.h 77


Trying to create a graphics object this way (Graphics g =
this->CreateGraphics();)

Error 1 error C3767: 'System::Drawing::Graphics::Graphics': candidate
function(s) not accessible c:\documents and settings\saul01\my
documents\visual studio
2005\projects\cc2\managedxp\managedxp\Test.h 77


I'm using VS2005 and all the other code was generated by Visual Studio.

Thanks for the help, L. Lee Saunders
 
P

Peter Oliphant

Just to emphasize, this is not proper syntax:

Graphics* graphics ;

this is:

Graphics^ graphics ;

[==P==]
 
S

saunderl

Thanks Peter!

Why oh why does every example of Microsofts uses the * ?

I've seen examples with the ^ but I've never seen a discussion on what
it is or used for.

Thanks again, Lee
 
W

William DePalo [MVP VC++]

Why oh why does every example of Microsofts uses the * ?

There are two "dialects" of C++ that can be used to target the .Net
environment. The older is called Managed Extensions for C++ aka MC++. The
newer one - just out last month with VS 2005 - is called C++/CLI (common
language infrastructure) or something.

MC++ uses pointers syntax _both_ for addresses of native objects and for the
GC handles to managed objects.

C++/CLI stresses the difference between pointer and handle by using the
circumflex (^).
I've seen examples with the ^ but I've never seen a discussion on what
it is or used for.

You can start reading here

http://msdn.microsoft.com/msdnmag/issues/05/02/PureC/

and then google for

C++/CLI

Regards,
Will
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top