MakeCaption - Justify Captions

[Home]   [Puzzles & Projects]    [Delphi Techniques]   [Math Topics]   [Library]   [Utilities]

Search

 

Search DelphiForFun.org only

Support DFF

 If you benefit from the website,  in terms of knowledge, entertainment value, or something otherwise useful, consider making a donation via PayPal  to help defray the costs.  (No PayPal account necessary to donate via credit card.)  Transaction is secure.

 

If you shop at Amazon anyway,  consider using this link. We receive a few cents from each purchase.   Thanks.

In Association with Amazon.com

 

Contact

Feedback:  Send an e-mail with your comments about this program (or anything else).

 

Search DelphiForFun.org only

 

 

Here's a little routine, MakeCaption,  that solves the problem of justifying titlebar caption information,  I wanted to add a copyright notice to the right side of the titlebar for some simple programs where an About  box seemed like overkill. 

So here's the plan - we'll just determine the width in pixels of the  titlebar caption area, determine the width of the text we want to insert, then calculate how many space characters to stick in between the left and right halves.   Like this:

NbrSpaces:=(CaptionAreaWidth-LeftTextWidth-RightTextWidth) div SpaceWidth;

Caption:=LeftText+StringofChar('  ',NbrSpaces)+RightText;

As usual, things are not so simple.   The TextWidth function will return the width in pixels for a particular canvas.   The value depends on the font characteristics of the canvas.  And  there's the complication.  Titlebar characteristics are uniform for the entire Windows system and are defined by  the Control Panel or by right-clicking on your desktop and then the Appearance tab.     Retrieving this information in the program requires a call to the SystemParametersInfo function with a SPI_GetNonCLientMetrics parameter.    A little messy  - you can view the code for  the details.  

Once we get the width of the borders, the size of the icon in the top left corner and  the size of the max/min/close boxes, we can subtract those from the client rectangle width to get the width of the caption area.    I defined a temporary bitmap just to get a canvas and assigned the titlebar font properties to it.  Then using the TextWidth function for that canvas  gives us the other numbers we need to complete the NbrSpaces calculation.  

And it works!   You can download the source (MakeCaption and a the test project) to view the rest of the details.  

Download source here

 

 

Modified 11/07/2008 21:45


  [Feedback]   [Newsletters (subscribe/view)] [About me]
Copyright © 2000-2009, Gary Darby    All rights reserved.