MakeCaption - Justify Captions

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




Search WWW


As of October, 2016, Embarcadero is offering a free release of Delphi (Delphi 10.1 Berlin Starter Edition ).     There are a few restrictions, but it is a welcome step toward making more programmers aware of the joys of Delphi.  They do say "Offer may be withdrawn at any time", so don't delay if you want to check it out.  Please use the feedback link to let me know if the link stops working.


Support DFF - Shop

 If you shop at Amazon anyway,  consider using this link. 


We receive a few cents from each purchase.  Thanks


Support DFF - Donate

 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.

Mensa Daily Puzzlers

For over 15 years Mensa Page-A-Day calendars have provided several puzzles a year for my programming pleasure.  Coding "solvers" is most fun, but many programs also allow user solving, convenient for "fill in the blanks" type.  Below are Amazon  links to the two most recent years.

Mensa 365 Puzzlers  Calendar 2017

Mensa 365 Puzzlers Calendar 2018

(Hint: If you can wait, current year calendars are usually on sale in January.)


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

Search 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 05/12/2018 08:57

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