Solved

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

Posted on 2004-10-17
12
664 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
Certified OpenStack Administrator Course

We just refreshed our COA course based on the Newton exam.  With 14 labs, this course goes over the different OpenStack services that are part of the certification: Dashboard, Identity Service, Image Service, Networking, Compute, Object Storage, Block Storage, and Orchestration.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

623 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