Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2004-10-17
12
Medium Priority
?
666 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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
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 2000 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

Automating Terraform w Jenkins & AWS CodeCommit

How to configure Jenkins and CodeCommit to allow users to easily create and destroy infrastructure using Terraform code.

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

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