I'm trying to get my hands around Forms Painting in Visual C++, so I've created a Form, and on that Form I have added a Panel called cityPanel. I'd like to paint a green rectangle on it. I double clicked cityPanel, which resulted in Visual C++ creating this new function:

private: System::Void cityPanel_Paint(System::Object^  sender, System::Windows::Forms::PaintEventArgs^  e)

I've filled in the function as so:

private: System::Void cityPanel_Paint(System::Object^  sender, System::Windows::Forms::PaintEventArgs^  e) 
    Graphics graphicsObj = e->Graphics;
    SolidBrush aBrush = gcnew SolidBrush(System::Drawing::Color::Green);
    graphicsObj.FillRectangle(aBrush, 100, 100, 200, 300);

I get three errors, one on each line, in order:

Error 1 error C3767: 'System::Drawing::Graphics::Graphics': candidate function(s) not accessible

Error 2 error C2664: 'System::Drawing::SolidBrush::SolidBrush(System::Drawing::Color)' : cannot convert parameter 1 from 'System::Drawing::SolidBrush ^' to 'System::Drawing::Color'

Error 3 error C2664: 'void System::Drawing::Graphics::FillRectangle(System::Drawing::Brush ^,float,float,float,float)' : cannot convert parameter 1 from 'System::Drawing::SolidBrush' to 'System::Drawing::Brush ^'

I'm pretty new to the whole concept of "garbage collected" C++ and in particular to the ^ symbol, which I'm kind of using interchangeably with * in my head. Similarly I'm thinking that gcnew and new are the same except that I don't have to delete anything that I create with gcnew when I'm done with it. This is obviously an oversimplification, so in addition to the above code, if anyone has any good links explaining ^ and gcnew , I'd appreciate them. Thanks!

8 Years
Discussion Span
Last Post by VernonDozier
Featured Replies
  • 1
    ArkM 1,090   8 Years Ago

    Caret does not denote a pointer. It's MS .NET extention for handler of GC heap object. See, for example: [url]http://www.visualcplusdotnet.com/visualcplusdotnet14c.html[/url] Forget portability (in C++?!) with hats, gcnew and other MS VC++ tricks... Read More


I've been looking through some forums and some of them have to do with public/private/protected. Someone suggested the command make_public, which I wasn't sure how to use, but I've changed everything to public and it didn't seem to make a difference. However, I've gotten it to paint the green rectangle with this code:

public: System::Void cityPanel_Paint(System::Object^  sender, System::Windows::Forms::PaintEventArgs^  e) 
    Graphics^ graphicsObj = e->Graphics;
    Color aColor = Color::Green;
    SolidBrush^ aBrush = gcnew SolidBrush(aColor);
    e->Graphics->FillRectangle(aBrush, 100, 100, 200, 300);

So that part's solved, but does anyone have a good explanation or link as to the difference between using * and new and using ^ and gcnew? The latter is considered "safe" and the former "unsafe", I believe, but ^ and * are both pointers to objects, right? Thanks.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.