GDI+ Graphics Object
Jan 30, 2009 at 5:46pm Jan 30, 2009 at 5:46pm UTC
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.
Jan 30, 2009 at 6:23pm Jan 30, 2009 at 6:23pm UTC
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);
}
Jan 30, 2009 at 7:07pm Jan 30, 2009 at 7:07pm UTC
That works great, thanks a lot for your help
Topic archived. No new replies allowed.