Solved

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

Posted on 2004-10-17
12
658 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

762 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

24 Experts available now in Live!

Get 1:1 Help Now