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!

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.