Author: Tomas Rutkauskas
Can I change black line color of the TPanel border (BorderStyle = bsSingle) into
i.e. blue line color? I tried to trap the WM_NCPAINT message and to draw over the
border line, but it's not working. The border line color is still black.
Answer:
That color is the COLOR_WINDOWFRAME, so you probably do not want to change it in
general. But the NC paint handler should work. Here's some sample code to draw a
border in red:
1 { ... }2 type3 TMyPanel = class(TPanel)
4 protected5 procedure WM_NCPaint(var Msg: TWMNCPaint); message WM_NCPaint;
6 end;
7 8 procedure TMyPanel.WM_NCPaint(var Msg: TWMNCPaint);
9 var10 DC: HDC;
11 OldBrush: HBRUSH;
12 OldPen: HPEN;
13 begin14 DC := 0;
15 OldBrush := 0;
16 OldPen := 0;
17 try18 {Must use a WindowDC or you can't draw outside the client area}19 DC := GetWindowDC(Handle);
20 {Use a "clear" brush and an appropriately colored pen}21 OldBrush := SelectObject(DC, GetStockObject(NULL_BRUSH));
22 Canvas.Pen.Color := clRed;
23 OldPen := SelectObject(DC, Canvas.Pen.Handle);
24 {Draw the border}25 Rectangle(DC, 0, 0, Width, Height);
26 {Tell Windows you did it}27 Msg.Result := 0;
28 finally29 {Clean up the mess you made}30 if DC <> 0 then31 begin32 if OldPen <> 0 then33 SelectObject(DC, OldPen);
34 if OldBrush <> 0 then35 SelectObject(DC, OldBrush);
36 ReleaseDC(Handle, DC);
37 end;
38 end;
39 end;
40 41 {Dynamic panel creation}42 43 { ... }44 Panel := TMyPanel.Create(Self);
45 with Panel do46 begin47 Parent := Self;
48 Left := 10;
49 Top := 10;
50 {Don't try to do 3D borders or add beveling - keep it simple}51 BevelOuter := bvNone;
52 BorderStyle := bsSingle;
53 Ctl3d := False;
54 end;
55 { ... }