Are there any examples of using CreateGraphics() inside a function, seperate from an Windows Forms
I have a function that loops - every time it does, I need to draw a red rectangle :
Graphics* gr2;
gr2 = cc->CreateGraphics();
SolidBrush.....
gr2->FillRectangle(.....
I get object reference not set to instance of object - I thought cc is an instance
inside the Form declaration :
public: static System::Windows::Forms::Control * cc; // not sure if this is legal...
public: static System::Drawing::Graphics * gr2;
Any help would be very much appreciated.
Regards,
ak

drawing on a form from within a seperate function
gowtham173656
You know what In a last attempt, could you send me a zip of the project to jvdbeek_13(removeme)@hotmail.com
Or, if creating a new project with the steps I provided also works for you, let me know.
pooyaJoonam
An unhandled exception of type 'System.NullReferenceException' occurred in system.windows.forms.dll
Additional information: Object reference not set to an instance of an object.
This exception definitely occurs at :
gr2 = cc-> CreateGraphics();
My function prototype looks like :
BOOL X(System::Windows::Forms::Control* cc, System::Drawing::SolidBrush* sb, System::Drawing::Graphics* gr2, .......
Regards,
ak
FSOL
Here's everything I have that deals with the control :
inside the Form definition :
public: static System::Windows::Forms::Control * dd;
public: static System::Drawing::Graphics * gr2;
public: static System::Drawing::SolidBrush * sb;
inside InitializeComponent :
this->dd = new System::Windows::Forms::Control();
//this->gr2 = new System::Drawing::Graphics
this->sb = new System::Drawing::SolidBrush(Color::Red);
this->Controls->Add(this->dd);
the function call :
X(dd, sb, gr2, textBox1,...
another Graphic that works fine (the red rectangles are drawn onto this bitmap :
private: System::Void button3_Click(System::Object * sender, System::EventArgs * e)
{
bitmap = new Bitmap(S"test.bmp");
this->Invalidate();
this->Update();
}
private: System::Void Form2_Paint(System::Object * sender, System::Windows::Forms::PaintEventArgs * e)
{
Graphics * gr = e->Graphics;
if (bitmap != NULL)
{
gr->DrawImage(bitmap, 20, 10, 500, 500);
}
}
inside the function X :
Graphics* gr2;
gr2= cc->CreateGraphics();
SolidBrush* sb = new SolidBrush(Color::Red);
gr2->FillRectangle(sb, 300+x, 300-y , 5, 5);
gr2->Dispose();
Remember, that when gr2 is created & used inside the Form, everything is OK. It's when I'm doing this inside a function that no rectangles are drawn. If everything looks fine, an example of CreateGraphics used in a standalone function would be helpful...
bbeckles
public: static System::Windows::Forms::Control * dd;
public: static System::Drawing::Graphics * gr2;
public: static System::Drawing::SolidBrush * sb;
private:
System::ComponentModel::Container* components;
void InitializeComponent(void)
{
this->dd = new System::Windows::Forms::Control();
this->sb = new System::Drawing::SolidBrush(Color::Red);
}
inside the function :
Graphics* gr2;
gr2= cc->CreateGraphics();
gr2->FillRectangle(sb, ....
Thomas Thayil
I'm sorry man, I'm working on C# only...
Anyway the CreateGraphics method works on my example, I'm calling it from the form on another loaction and paints the rectangle, sorry I can't help you more...
Heng-yi Liu
Can you double check where that exception come from the call to CreateGraphics() will never return a null object.
clark121121
Interact
KyleHenly
Thanks very much for the help.
Martinezg
I've tried to do what your are tring to do, it seems to work fine:
I've define a control:
class
DemoControl : Control{
public DemoControl(){
}
}
Than, I've add it to a form:
this.demoControl1 = new ControlsChecker.DemoControl(); this.demoControl1.Location = new System.Drawing.Point(141, 439); this.demoControl1.Name = "demoControl1"; this.demoControl1.Size = new System.Drawing.Size(75, 23); this.demoControl1.TabIndex = 9; this.demoControl1.Text = "demoControl1";this.Controls.Add(demoControl);
And on a button click, I've try to draw on it:
private void button1_Click(object sender, EventArgs e){
Graphics g = demoControl1.CreateGraphics();g.FillRectangle(
Brushes.Red, ClientRectangle);g.Dispose();
}
Well, Its works...
If this is the lead of what you are tring to do, maybe you should start from a simple control like this, check if this works, and than add the functionallty slowly, to find where it falls.
Sushil Chordia
Ok... tried to reproduce your problem with your code. I can paint in a control in something else than the paint event handler. I also paint the thing when a button is clicked. Some remarks about your code:
-Why do you create a Control I have used Panel instead of Control, which worked. Control also didn't paint anything, don't know why, but I didn't get the error message.
-Try not to initialize additional code in the InitializeComponent class. This class is created by the designer, and the creation of your brush will be gone each time you've entered the designer.
-Why did you make the controls static I didn't, because I didn't see why...
-Why did you make data members of the graphics object and the brush object Remember that the Graphics object is invalid after the current message has been processed. You need to call CreateGraphics each time. You can't cache it! From the docs:
"The Graphics is only valid for the duration of the current window's message."
-Don't forget to dispose of the brush too!
What I did was:
1. drag a panel onto a form.
2. Upon some event, like the click of a button, use this code:
Graphics* gr2;
gr2= panel1->CreateGraphics();
SolidBrush* sb = new SolidBrush(Color::Red);
gr2->FillRectangle(sb, 0, 0 , panel1->Width, panel1->Height);
gr2->Dispose();
sb->Dispose();
I think this fixes your problems, doesn't it
Technobabble80
I guess I'll try creating a new project with just the Graphics code to see if that works.