Recent

Author Topic: TGroupBox WMPaint ...  (Read 1168 times)

Espectr0

  • Full Member
  • ***
  • Posts: 218
TGroupBox WMPaint ...
« on: March 20, 2023, 10:46:52 pm »
Hola!

a question, why when I override WMPaint msg of the TGroupBox when painting (fillrect for example) not occupy all the real space?
In the image I show on the left with override and on the right without.

how can I solve it?

Thanks

jamie

  • Hero Member
  • *****
  • Posts: 6128
Re: TGroupBox WMPaint ...
« Reply #1 on: March 21, 2023, 03:17:04 am »
may have something to do with the widget that currently paints the area to fix some issue of colors not being correct for the caption of the Group Box.

 Also, there could be a ClipRect attached to the control.
etc.

 Hard to say. Maybe you should show a short piece of your code where you are doing this, you may need to allow the inherited paint first then you can over paint that.
The only true wisdom is knowing you know nothing

Espectr0

  • Full Member
  • ***
  • Posts: 218
Re: TGroupBox WMPaint ...
« Reply #2 on: March 21, 2023, 08:48:48 am »
I tried leaving the "inherited" but it doesn't look complete either, just the "client" part let's say...
I put basic code to show:

Code: Pascal  [Select][+][-]
  1. procedure TMyGroupBox.WMPaint(var Msg: TLMPaint);
  2. var
  3.   ACanvas: TCanvas;
  4. begin
  5.   inherited;
  6.  
  7.   ACanvas := TCanvas.Create;
  8.   try
  9.     ACanvas.Handle := Msg.DC;
  10.     ACanvas.Brush.Color := clRed;
  11.     ACanvas.FillRect(0, 0, self.width, self.height);
  12.   finally
  13.     ReleaseDC(0, ACanvas.Handle);
  14.     ACanvas.Free;
  15.   end;
  16. end;
  17.  

« Last Edit: March 21, 2023, 08:52:56 am by Espectr0 »

Thaddy

  • Hero Member
  • *****
  • Posts: 14364
  • Sensorship about opinions does not belong here.
Re: TGroupBox WMPaint ...
« Reply #3 on: March 21, 2023, 02:03:18 pm »
Assuming you are on Windows, you must also implement WM_NCPAINT.
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2050
  • Fifty shades of code.
    • Delphi & FreePascal
Re: TGroupBox WMPaint ...
« Reply #4 on: March 21, 2023, 02:12:20 pm »
Not tested but my guess is, that than you have same result as just setting color the normal way (upper bevel will not be the actual clientrect that he wants to color)
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

lainz

  • Hero Member
  • *****
  • Posts: 4468
    • https://lainz.github.io/
Re: TGroupBox WMPaint ...
« Reply #5 on: March 21, 2023, 02:52:09 pm »
Use a TPanel and play with ChildSizing to get the same behaviour as GroupBox. Tpanel can be fully painted.

Espectr0

  • Full Member
  • ***
  • Posts: 218
Re: TGroupBox WMPaint ...
« Reply #6 on: March 21, 2023, 09:52:37 pm »
Added the message @Thaddy recommended but i can't get it to work :S
« Last Edit: March 22, 2023, 06:45:11 pm by Espectr0 »

Espectr0

  • Full Member
  • ***
  • Posts: 218
Re: TGroupBox WMPaint ...
« Reply #7 on: March 22, 2023, 06:52:13 pm »
Using the Subclass technique I was able to paint as I want:

Code: Pascal  [Select][+][-]
  1. unit UWGroupBox;
  2.  
  3. //------------------------------------------------------------------------------
  4.  
  5. {$mode ObjFPC}{$H+}
  6.  
  7. interface
  8.  
  9. uses
  10.   Classes, Controls, SysUtils, Graphics, StdCtrls, LCLType, LMessages, LCLIntf,
  11.   LazarusPackageIntf, Types;
  12.  
  13. type
  14.  
  15.   { TUWGroupBox }
  16.  
  17.   TUWGroupBox = class(TGroupBox)
  18.   {$IFDEF WINDOWS}
  19.   private
  20.     FOwnerDraw: Boolean;
  21.     procedure SetOwnerDraw(const AValue: Boolean);
  22.   public
  23.     constructor Create(TheOwner: TComponent); override;
  24.   {$ENDIF}
  25.   published
  26.     {$IFDEF WINDOWS}
  27.     property OwnerDraw: Boolean read FOwnerDraw write SetOwnerDraw;
  28.     {$ENDIF}
  29.     property Align;
  30.     property Anchors;
  31.     property AutoSize;
  32.     property BidiMode;
  33.     property BorderSpacing;
  34.     property Caption;
  35.     property ChildSizing;
  36.     property ClientHeight;
  37.     property ClientWidth;
  38.     property Color;
  39.     property Constraints;
  40.     property DockSite;
  41.     property DoubleBuffered;
  42.     property DragCursor;
  43.     property DragKind;
  44.     property DragMode;
  45.     property Enabled;
  46.     property Font;
  47.     property ParentBackground;
  48.     property ParentBidiMode;
  49.     property ParentColor;
  50.     property ParentDoubleBuffered;
  51.     property ParentFont;
  52.     property ParentShowHint;
  53.     property PopupMenu;
  54.     property ShowHint;
  55.     property TabOrder;
  56.     property TabStop;
  57.     property Visible;
  58.     property OnChangeBounds;
  59.     property OnClick;
  60.     property OnContextPopup;
  61.     property OnDblClick;
  62.     property OnDragDrop;
  63.     property OnDockDrop;
  64.     property OnDockOver;
  65.     property OnDragOver;
  66.     property OnEndDock;
  67.     property OnEndDrag;
  68.     property OnEnter;
  69.     property OnExit;
  70.     property OnGetSiteInfo;
  71.     property OnKeyDown;
  72.     property OnKeyPress;
  73.     property OnKeyUp;
  74.     property OnMouseDown;
  75.     property OnMouseEnter;
  76.     property OnMouseLeave;
  77.     property OnMouseMove;
  78.     property OnMouseUp;
  79.     property OnMouseWheel;
  80.     property OnMouseWheelDown;
  81.     property OnMouseWheelUp;
  82.     property OnResize;
  83.     property OnStartDock;
  84.     property OnStartDrag;
  85.     property OnUnDock;
  86.     property OnUTF8KeyPress;
  87.   end;
  88.  
  89. // -----------------------------------------------------------------------------
  90.  
  91. implementation
  92.  
  93. {$IFDEF WINDOWS}
  94. uses
  95.   Windows, CommCtrl, Themes;
  96.  
  97. const
  98.   ID_UWGROUPBOX = 1;
  99.   TEXT_MARGIN   = 8;
  100.  
  101. //------------------------------------------------------------------------------
  102.  
  103. { UWGroupBox }
  104.  
  105. //------------------------------------------------------------------------------
  106.  
  107. function NewWndProc(Wnd: HWND; uMsg: UINT; wParam: WPARAM; lParam: LPARAM; uISubClass: UINT_PTR; dwRefData: DWORD_PTR): LRESULT; stdcall;
  108. var
  109.   AGroupBox: TUWGroupBox absolute dwRefData;
  110.   ACanvas: TCanvas;
  111.   DC: HDC;
  112.   PS: PAINTSTRUCT;
  113.   R: TRect;
  114.   CaptionRect,
  115.   OuterRect: TRect;
  116.   Box: TThemedButton;
  117.   Details: TThemedElementDetails;
  118.   ATextStyle: TTextStyle;
  119.   fw, fh: Integer;
  120. begin
  121.   case uMsg of
  122.     //WM_NCPAINT,
  123.     WM_PAINT:
  124.     begin
  125.       DC := BeginPaint(Wnd, @PS);
  126.       try
  127.         ACanvas := TCanvas.Create;
  128.         try
  129.           with ACanvas do
  130.           begin
  131.             Handle      := DC;
  132.             Brush.Style := bsClear;
  133.             Font        := AGroupBox.Font;
  134.             Brush.Color := AGroupBox.Color;
  135.             FillRect(PS.rcPaint);
  136.             Font.GetTextSize(AGroupBox.Caption, fw, fh);
  137.  
  138.             if AGroupBox.Caption <> '' then
  139.             begin
  140.               CaptionRect := Types.Rect(0, 0, fw, fh);
  141.               OffsetRect(CaptionRect, TEXT_MARGIN, 0)
  142.             end
  143.             else
  144.               CaptionRect := Types.Rect(0, 0, 0, 0);
  145.  
  146.             OuterRect     := PS.rcPaint;
  147.             OuterRect.Top := (CaptionRect.Bottom-CaptionRect.Top) div 2;
  148.  
  149.             with CaptionRect do
  150.               ExcludeClipRect(Handle, Left, Top, Right, Bottom);
  151.  
  152.             if AGroupBox.Enabled then
  153.               Box := tbGroupBoxNormal
  154.             else
  155.               Box := tbGroupBoxDisabled;
  156.  
  157.             // Draw themed frame
  158.             Details := ThemeServices.GetElementDetails(Box);
  159.             ThemeServices.DrawElement(Handle, Details, OuterRect);
  160.             SelectClipRgn(Handle, 0);
  161.             // Draw caption
  162.             if AGroupBox.Caption <> '' then
  163.             begin
  164.               R := Types.Rect(TEXT_MARGIN, 0, fw+TEXT_MARGIN, fh);
  165.               FillByte(ATextStyle, SizeOf(TTextStyle), 0);
  166.               with ATextStyle do
  167.               begin
  168.                 SingleLine  := True;
  169.                 Clipping    := True;
  170.               end;
  171.               ACanvas.TextRect(R, R.Left, R.Top, AGroupBox.Caption, ATextStyle);
  172.             end;
  173.           end
  174.         finally
  175.           ReleaseDC(Wnd, ACanvas.Handle);
  176.           ACanvas.Free;
  177.         end;
  178.       finally
  179.         EndPaint(Wnd, @PS);
  180.       end;
  181.       Result:= 0;
  182.     end;
  183.   else
  184.     Result := DefSubclassProc(Wnd, uMsg, wParam, lParam);
  185.   end
  186. end;
  187.  
  188. //------------------------------------------------------------------------------
  189.  
  190. constructor TUWGroupBox.Create(TheOwner: TComponent);
  191. begin
  192.   inherited Create(TheOwner);
  193.   FOwnerDraw := False;
  194. end;
  195.  
  196. //------------------------------------------------------------------------------
  197.  
  198. procedure TUWGroupBox.SetOwnerDraw(const AValue: Boolean);
  199. begin
  200.   if FOwnerDraw <> AValue then
  201.   begin
  202.     FOwnerDraw := AValue;
  203.  
  204.     if FOwnerDraw then
  205.       SetWindowSubclass(Handle, @NewWndProc, ID_UWGROUPBOX, DWORD_PTR(Self))
  206.     else
  207.       RemoveWindowSubclass(Handle, @NewWndProc, ID_UWGROUPBOX);
  208.  
  209.     Invalidate;
  210.   end;
  211. end;
  212. {$ENDIF}
  213.  
  214. // -----------------------------------------------------------------------------
  215.  

but the problem I have now is that the controls inside the group type "TGraphicControl" (like TLabel) overlap them when painting (disappear) %)

any idea what i'm doing wrong?
« Last Edit: March 22, 2023, 10:04:05 pm by Espectr0 »

 

TinyPortal © 2005-2018