GDI+ Graphics Object

I was just wondering, are we able to create graphics objects from controls such as an imagebox? And if so, can it be handled in the _Paint method?
The picture box is on top of the form itself and therefore blocks out view of the text drawn by GDI+ on the main form.

I am currently using the code
1
2
3
4
5
6
7
8
9
private: System::Void MainGame_Paint(System::Object^  sender, System::Windows::Forms::PaintEventArgs^  e) {

			  Graphics ^ g;
g = this->CreateGraphics();
System::Drawing::Drawing2D::LinearGradientBrush^ myBrush = gcnew   System::Drawing::Drawing2D::LinearGradientBrush(ClientRectangle, Color::Blue, Color::Yellow, System::Drawing::Drawing2D::   LinearGradientMode::Horizontal);
System::Drawing::Font^ myFont =  gcnew System::Drawing::Font(L"Calibri", 18);
g->DrawString(L"Test", myFont, myBrush, 10, 10);
		
		 }

To draw the text onto the main form, which works fine, however changing it to
1
2
3
4
5
6
7
8
9
private: System::Void MainGame_Paint(System::Object^  sender, System::Windows::Forms::PaintEventArgs^  e) {

			  Graphics ^ g;
g = pictureBox2->CreateGraphics();
System::Drawing::Drawing2D::LinearGradientBrush^ myBrush = gcnew   System::Drawing::Drawing2D::LinearGradientBrush(ClientRectangle, Color::Blue, Color::Yellow, System::Drawing::Drawing2D::   LinearGradientMode::Horizontal);
System::Drawing::Font^ myFont =  gcnew System::Drawing::Font(L"Calibri", 18);
g->DrawString(L"Test", myFont, myBrush, 10, 10);
		
		 }

Does not seem to work, am i creating the graphics object incorrectly, should this not be performed in _Pain or is it not possible to draw onto a imagebox.
closed account (z05DSL3A)
You need to have a handeler for the Paint Event for the PictureBox.

You should also use the Graphics object via the PaintEventArgs:
1
2
3
4
5
6
private: System::Void pictureBox1_Paint(System::Object^  sender, System::Windows::Forms::PaintEventArgs^  e) 
{
    System::Drawing::Drawing2D::LinearGradientBrush^ myBrush = gcnew   System::Drawing::Drawing2D::LinearGradientBrush(ClientRectangle, Color::Blue, Color::Yellow, System::Drawing::Drawing2D::   LinearGradientMode::Horizontal);
    System::Drawing::Font^ myFont =  gcnew System::Drawing::Font(L"Calibri", 18);
    e->Graphics->DrawString(L"Test", myFont, myBrush, 10, 10);
}
That works great, thanks a lot for your help
Topic archived. No new replies allowed.