Jump to content
FabDev

Get Minimize, Maximize and Close button width of a Tform

Recommended Posts

Hello,

 

I would like to show a  StayOnTop form on the left of the Minimize, Maximize and Close button (Top right of tittle bar) of a TForm.

image.png.588256d2cdc86c95b2d7f2c086e38646.png

 

To do it I need to compute width of these button, so I have tried this :


 

var

  TitleInfo       : TTitleBarInfo;

  NonClientWidth, ButtonWidth: Integer;


begin

        NonClientWidth := Width - ClientWidth;

         SendMessage(Handle, WM_GETTITLEBARINFOEX, 0,NativeInt(@TitleBarInfoEx));//   LPARAM(@TitleBarInfoEx));

         ButtonWidth := TitleBarInfoEx.rgrect[5].Right - TitleBarInfoEx.rgrect[2].Left + 2 * NonClientWidth - 2 * BorderWidth;

....

Following this :

https://learn.microsoft.com/en-us/windows/win32/api/winuser/ns-winuser-titlebarinfo

 

 

But  TitleBarInfoEx.rgrect[5].Right return me wrong value like not initialized :

image.thumb.png.7e76558ae17b1f3d4922aff08f0048c3.png

 

So how to get it ?

 

 

 

Edited by FabDev

Share this post


Link to post

Inspect Delphi's VCL.Forms.pas -> TTitleBar.GetCaptionButtonsRect -> DwmGetWindowAttribute

Share this post


Link to post
7 hours ago, FabDev said:

To do it I need to compute width of these button, so I have tried this :

...
But  TitleBarInfoEx.rgrect[5].Right return me wrong value like not initialized

Your code snippet is declaring a variable named TitleInfo, but you are sending WM_GETTITLEBARINFOEX with a pointer to a variable named TitleBarInfoEx instead. Also, make sure you initialize the TTitleBarInfo/Ex.cbSize member before sending WM_GETTITLEBARINFOEX.

 

https://learn.microsoft.com/en-us/windows/win32/menurc/wm-gettitlebarinfoex

Quote

lParam

A pointer to a TITLEBARINFOEX structure. The message sender is responsible for allocating memory for this structure. Set the cbSize member of this structure to sizeof(TITLEBARINFOEX) before passing this structure with the message.

https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-gettitlebarinfo

Quote

[in, out] pti

Type: PTITLEBARINFO

A pointer to a TITLEBARINFO structure to receive the information. Note that you must set the cbSize member to sizeof(TITLEBARINFO) before calling this function.

So, try this instead:

var
  TitleInfo : TTitleBarInfoEx;
  ...
begin
  ...
  TitleInfo.cbSize := SizeOf(TitleInfo); // <-- ADD THIS!
  SendMessage(Handle, WM_GETTITLEBARINFOEX, 0, LPARAM(@TitleInfo));
  ...
end;

 

Share this post


Link to post

Beside the WinAPI way, you could make your day easier by following the Vcl way of doing and playing with the Titlebar.

Share this post


Link to post
5 hours ago, KodeZwerg said:

Beside the WinAPI way, you could make your day easier by following the Vcl way of doing and playing with the Titlebar.

Thank you that was a nice idea but I use DevExpress (Bar/Ribbon and Skin) which is not compatible with the native Titlebar.

Share this post


Link to post
23 hours ago, Remy Lebeau said:

So, try this instead:

 

Thank you Remy it's work fine !

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×