Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Two strings in the Form's title bar


This question is not answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 2 - Last Post: Jan 31, 2018 2:31 PM Last Post By: Carlos Saez
Carlos Saez

Posts: 20
Registered: 12/26/06
Two strings in the Form's title bar  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 29, 2018 11:31 AM
In a Form's title bar I'd need to put 2 strings: one aligned to the left and another aligned to the right. It would be fine to have their fonts in different colors.
How can I do it?
Thank you
Carlos
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Two strings in the Form's title bar  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 29, 2018 1:17 PM   in response to: Carlos Saez in response to: Carlos Saez
Carlos Saez wrote:

In a Form's title bar I'd need to put 2 strings: one aligned to the
left and another aligned to the right.

The only way to do that seemlessly is to intercept the WM_NCPAINT
message and draw the entire title bar manually, then you can draw the
text however you want, such as with DrawText() using the DT_LEFT and
DT_RIGHT flags. But, in modern systems with theming and DWM and such
enabled, this is very difficult to handle correctly, especially if you
want the title bar to otherwise look and act like a standard title bar.

Why do you need this? Why can't you solve your UI issue using a more
standardized UI approach? What are you really trying to accomplish?

At the very least, you could simply use a single title string like
normal, and just put space characters in between the two portions you
want to split up. You would just have to take the Form's font and
width into account to calculate the number of spaces to use, especially
if the window ever resizes.

It would be fine to have their fonts in different colors.

That makes it even harder, especially with themes and gradients to deal
with.

--
Remy Lebeau (TeamB)
Carlos Saez

Posts: 20
Registered: 12/26/06
Re: Two strings in the Form's title bar  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 31, 2018 2:31 PM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Remy Lebeau (TeamB) wrote:

The only way to do that seemlessly is to intercept the WM_NCPAINT
message and draw the entire title bar manually, then you can draw the
text however you want, such as with DrawText() using the DT_LEFT and
DT_RIGHT flags. But, in modern systems with theming and DWM and such
enabled, this is very difficult to handle correctly, especially if you
want the title bar to otherwise look and act like a standard title bar.

Time ago I needed to put the two strings, left aligned and right aligned, on Form's title bar. Somewhere (I don't remember where) I found a piece of code that I costumized. The left string was solved with TForm.caption and the right string (two or more right strings) with the customized code. Well, it don't really put the stings to the right, but the final result does. You could also put it centered or whatever place you want.

I put a call to TwoStringsToTheRight on every Form event that resulted in a form rewrite, to keep the string/s on place. If I remember well I decided not to use this procedure anymore because, for some reason, it uses too much GDI handles and I was not able to know why. I'd appreciate some help on that.

procedure TMyMainForm.TwoStringsToTheRight;
var
captionText : string;
w, w2 : size;
whdc: HDC;
var
canvas: TCanvas;
begin
try
captionText:= 'COMPANY XYZ';
whdc:=GetWindowDC(Handle);
GetTextExtentPoint32(whdc, PAnsiChar(captionText), length(captionText) ,w);
canvas := TCanvas.Create;
canvas.Handle := GetWindowDC(Self.Handle);
with canvas do
begin
Font.Name:='Tahoma';
Font.Style:=[fsBold];
Brush.Style := bsClear;
Font.Color := clMaroon;
TextOut((Self.Width-40) - (w.cx-1), 6, captionText) ;
end;

captionText:= 'Company: ';
GetTextExtentPoint32(whdc, PAnsiChar(captionText), length(captionText) ,w2);
with canvas do
begin
Font.Name:='Tahoma';
Font.Style:=[fsBold];
Brush.Style := bsClear;
Font.Color := clBlack;
TextOut((Self.Width-40)-w.cx - (w2.cx-1), 6, captionText) ;
end;

finally
ReleaseDC(Self.Handle, canvas.Handle) ;
canvas.Free;
end;
end;

Why do you need this? Why can't you solve your UI issue using a more
standardized UI approach? What are you really trying to accomplish?

Nothing special. My first need was just to put the name of the client that bought one application I developed by that time, but I suppose that may exist a lot of reasons to use this space.
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02