Solved

Need help with Paint problems with .NET control (PictureBox)

Posted on 2004-10-17
12
662 Views
Last Modified: 2012-08-13
I have a simple application where I'm using GDI+ objects to draw text onto a PictureBox. In the form's Paint method, I've got a call to redraw the text and that works fine when you move a window through the PictureBox.

However, if you move another window on top of the PictureBox, then switch back to my application, the PictureBox redraws that area to the default background colour after my form paint code runs.

What is the proper way to make sure that a control gets repainted when needed?
0
Comment
Question by:tma050898
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 2
  • 2
  • +2
12 Comments
 
LVL 48

Expert Comment

by:Mikal613
ID: 12333876
autoredraw = false
0
 
LVL 3

Author Comment

by:tma050898
ID: 12333934
Where exactly is this property? I don't see any property having to do with drawing on the PictureBox property list.
0
 
LVL 48

Expert Comment

by:Mikal613
ID: 12334106
on the form
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 28

Expert Comment

by:iboutchkine
ID: 12337510
.NET does not have autoredraw property. VB6 does
0
 
LVL 3

Author Comment

by:tma050898
ID: 12338310
When I looked at  he other person's bio, I kinda figured it was a vb thing. Hopefully, someone here will know the answer for .NET. (Not sure if it matters in terms of winforms, but I'm using Managed C++).
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 12338782
What does the code for the Paint event look like?

Bob
0
 
LVL 3

Author Comment

by:tma050898
ID: 12338832
private: System::Void Form1_Paint(System::Object *  sender,
                                                   System::Windows::Forms::PaintEventArgs *  e)
{
  PaintText();
}

protected: System::Void PaintText()
{
  if (textToDisplay)
  {
    // Create the font that will be used.
    System::Drawing::Font* font =
      new System::Drawing::Font(S"Times New Roman",
        Convert::ToSingle(fontSize), FontStyle::Regular);

    // Create a Graphics object from the picture box
    Graphics* g = picText->CreateGraphics();

    // Clear the picture box using a white background so that
    // the gradient and hatch text shows up best
    g->Clear(Color::White);

    // Measure the user-supplied text
    SizeF textSize = g->MeasureString(textToDisplay, font);

    // Create the GDI+ brush
    Brush* brush;
    if (TextStyle::Hatch == style)  // if the user selected Hatch
    {
      // GDI+ supplies a HatchBrush object.
      // I'm using the built-in "cross hatch" pattern
      brush = new HatchBrush(HatchStyle::Cross, primaryColour, secondaryColour);
    }
    else if (TextStyle::Gradient == style) // if the user selected Gradient
    {
      // Instantiate a LinearGradientBrush object.
      // I'm using the built-in "forward-diagonal" gradient
      RectangleF* rect = __nogc new RectangleF(PointF(0, 0), textSize);
      brush = new LinearGradientBrush(*rect,  primaryColour, secondaryColour,
                                                        LinearGradientMode::ForwardDiagonal);
    }

    // Now that we have a brush, simply draw the text using DrawString!!
    g->DrawString(textToDisplay, font, brush,
                          (picText->Width - textSize.Width) / 2,
                          (picText->Height - textSize.Height) / 2);
  }
}
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 12338862
Use the Graphics from the e.Graphics argument (persistent), instead of the CreateGraphics method (non-persistent).

Bob
0
 
LVL 3

Author Comment

by:tma050898
ID: 12339023
Bingo. Here's what I did (for future searches of this help)

1) I changed my button routine to the following
  * setup some internal display variables
  * call picText->Invalidate();
2) Added an event handler for the PictureBox control's (picText) Paint event
3) Moved the form's paint code (that painted the picText control into the picText::Paint method
4) Per "TheLearnedOne's" advice, I then used the PaintEventArgs::Graphics object

Works perfectly. Thanks much.
0
 
LVL 3

Author Comment

by:tma050898
ID: 12339065
One last question. Should I be disposing of these objects at some pt (via obj->Dispose) ?
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 12343971
To answer your last question:

If YOU create a graphics, brush or pen using CreateGraphics or the NEW keyword, then YOU need to dispose of it.  If you use the supplied graphics in the "e" argument then you DO NOT need need to dispose it.

~IM
0
 
LVL 3

Author Comment

by:tma050898
ID: 12344380
So in this case I should be releasing the Font, then. Thanks!
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question