Solved

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

Posted on 2004-10-17
12
659 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
  • 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
 
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
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 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 85

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

920 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now