We help IT Professionals succeed at work.
Get Started

Bug in TPanel - border doesn't get repainted when TPanel repaints

rfwoolf
rfwoolf asked
on
1,583 Views
Last Modified: 2013-11-22
Using Delphi BDS 2006, if I have a TPanel on a form, with BorderStyle =  BsSingle, and BorderWidth = 1,
if you paint on the form's canvas and paint something over this border,
and then I call Panel1.repaint and Form1.repaint;
everything is redrawn except for the border.

To redraw the border, I have to say:
  Panel1.BorderStyle := bsNone;
  Panel1.BorderStyle := bsSingle;
(the reason this works is because this calls panel1.RecreateWnd which "destroys the underlying windows object" and recreates it. You cannot however call panel1.RecreateWnd directly because it is private);

Another solution seems to be:
How can I repaint this border properly?

Attached are more detailed instructions on how to recreate the problem...
On a form, place a TPanel.
Set its Left to 50;
Set its Top to 50;
Set its BorderStyle = bsSingle
Set BorderWidth to 1 // (this doesn't actually matter, it can also be 0).
 
//Paint onto the forms canvas by putting a TButton with this OnClick event:
 
procedure TForm1.Button1Click(Sender: TObject);
var
  C : TCanvas;
  MyHDC : HDC;
begin
  C := TCanvas.Create;
  with C do
  try
    MyHDC := GetWindowDC(self.handle);
      try
        Handle := MyHDC;
        Brush.Style := bsSolid;
        Pen.Width := 8;
        Rectangle(Rect(10,10,100,100));
      finally
        ReleaseDC(self.handle, MyHDC);
    end;
  finally
    Free;
  end;
//NOW REPAINT THE PANEL
  Panel1.Repaint;
//NOW REPAINT THE FORM
  self.repaint;
end;
 
 
//THE SOLUTION:
  panel1.BorderStyle := bsNone;
  panel1.BorderStyle := bsSingle;

Open in new window

Comment
Watch Question
Commented:
This problem has been solved!
Unlock 3 Answers and 34 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE