Board index » delphi » Calculate Tab-Stops in Listboxes

Calculate Tab-Stops in Listboxes

How can I convert the tab-stops in windows listboxes to pixels or other metrics?

I have written a CustomControl DLL for Listboxes that displays a header over (and gray lines between) the rows and you can set with the
wm_SetTabStops Message. The Problem is to calculate the right X-Coordinate.

The wm_settabstops Message is using DialogUnits. These can be converted by dividing the LoWord by 4 (says Help).
But this is based on the System-Font (says Help to). If you have a dialog using Arial, 8 for example you have to divide it by 5.
The procedure to get the DialogUnits has NO parameters (DC or Font) so it can`t work properly for other fonts, I know.

I tried a few things with the values you get back from GetTextMetric but so far I didn`t get it right.

Does anyone know where the ListBox stores its font, tabstops etc.?
So far I trace the messages to keep my own variables updated.

Thanx to everyone.
Georg PAUL
e9125...@stud1.tuwien.ac.at

 

Re:Calculate Tab-Stops in Listboxes


How can I convert the tab-stops in windows listboxes to pixels or other metrics?

I have written a CustomControl DLL for Listboxes that displays a header over (and gray lines between) the rows and you can set with the
wm_SetTabStops Message. The Problem is to calculate the right X-Coordinate.

The wm_settabstops Message is using DialogUnits. These can be converted by dividing the LoWord by 4 (says Help).
But this is based on the System-Font (says Help to). If you have a dialog using Arial, 8 for example you have to divide it by 5.
The procedure to get the DialogUnits has NO parameters (DC or Font) so it can`t work properly for other fonts, I know.

I tried a few things with the values you get back from GetTextMetric but so far I didn`t get it right.

Does anyone know where the ListBox stores its font, tabstops etc.?
So far I trace the messages to keep my own variables updated.

Thanx to everyone.
Georg PAUL
e9125...@stud1.tuwien.ac.at

Re:Calculate Tab-Stops in Listboxes


Quote
Georg PAUL (e9125...@stud1.tuwien.ac.at) wrote:

: How can I convert the tab-stops in windows listboxes to pixels or other metrics?

Welcome to the club! I asked this very question a few weeks ago, and the
consensus reply was "nobody understands this tabbed listbox stuff". I
certainly don't. However, there is a reasonably simple solution: Make
your listbox Owner-Draw, and take care of the tabs yourself. I use
TabbedTextOut in the wm_DrawItem handler, and it works like a charm -
every tab stop exactly where I want it, not the hit-and-miss guesswork
of lb_SetTabStops.

So, this is my advice - go Owner-Draw.

Eyal Doron

Re:Calculate Tab-Stops in Listboxes


In article <31AC7A51.7...@stud1.tuwien.ac.at>,
e9125...@stud1.tuwien.ac.at says...

Quote

>How can I convert the tab-stops in windows listboxes to pixels or other
metrics?

>I have written a CustomControl DLL for Listboxes that displays a header

over (and gray lines between) the rows and you can set with the
Quote
>wm_SetTabStops Message. The Problem is to calculate the right
X-Coordinate.

>The wm_settabstops Message is using DialogUnits. These can be converted

by dividing the LoWord by 4 (says Help).
Quote
>But this is based on the System-Font (says Help to). If you have a

dialog using Arial, 8 for example you have to divide it by 5.
Quote
>The procedure to get the DialogUnits has NO parameters (DC or Font) so

it can`t work properly for other fonts, I know.
Quote

>I tried a few things with the values you get back from GetTextMetric

but so far I didn`t get it right.
Quote

>Does anyone know where the ListBox stores its font, tabstops etc.?
>So far I trace the messages to keep my own variables updated.

>Thanx to everyone.
>Georg PAUL
>e9125...@stud1.tuwien.ac.at

>> Yes its fun!  If you stay with variable width characters then you

have to cope with worst case width, ie a line worth of 'W' or 'M'.
So I agree with Eyal, draw your own listBox and use fixed fonts and
then you just insert blanks to pad out fields or do the math to
position by tabbing.
Martin
Quote

Re:Calculate Tab-Stops in Listboxes


Quote
Georg PAUL wrote:

> How can I convert the tab-stops in windows listboxes to pixels or other metrics?

> I have written a CustomControl DLL for Listboxes that displays a header over (and gray lines between) the rows and you can set with the
> wm_SetTabStops Message. The Problem is to calculate the right X-Coordinate.

OK, I know, that should be lb_SetTabStops !!

Quote
> The wm_settabstops Message is using DialogUnits. These can be converted by dividing the LoWord by 4 (says Help).
> But this is based on the System-Font (says Help to). If you have a dialog using Arial, 8 for example you have to divide it by 5.
> The procedure to get the DialogUnits has NO parameters (DC or Font) so it can`t work properly for other fonts, I know.

> I tried a few things with the values you get back from GetTextMetric but so far I didn`t get it right.

> Does anyone know where the ListBox stores its font, tabstops etc.?
> So far I trace the messages to keep my own variables updated.

> Thanx to everyone.
> Georg PAUL
> e9125...@stud1.tuwien.ac.at

(just for those who are interested)
On Wednesday evening I found following solution it works quite well :-)
But I`m still not sure that it works allways!

     var TextMetric :TextMetric;          
         MyFont     :HFont;     { The font that the control uses set by the dialog or by wm_SetFont }
         HWindow    :HWnd;      { Handle to the control`s window }      
         TextWidth,
         SystemWidth,
         MyFontWidth:Word;
         CalcStops,                     { Tabulator stops in pixel can be used to draw the Header of the Listbox with TabbedTextOut}
         TabStops   :^IntegerArray;     { Tabulator stops in DialogUnits as used by lb_SetTabStops }
         NumberOfTabs :Word;

     begin
        .
        .
        .              
     PaintDC := GetDC (HWindow);
     GetTextMetrics (PaintDC, TextMetric); { assuming System_Font is preset }
     SystemWidth := TextMetric. tmMaxCharWidth;
     OldFont := SelectObject (PaintDC, MyFont);
     GetTextMetrics (PaintDC, TextMetric);
     MyFontWidth := TextMetric. tmMaxCharWidth;
     SelectObject   (PaintDC, OldFont);    { don't delete the control's Font! }
     ReleaseDC      (HWindow, PaintDC);

     TextWidth    := Round (LoWord (GetDialogBaseUnits) * MyFontWidth / (4*SystemWidth));
     for Count := 1 to NumberOfTabs do
       CalcStops^ [count] := TabStops^ [count] * TextWidth -4;   { The Offset of -4 is to draw the borderline a bit left of the text }
        .
        .
        .
    end;

BTW: Thanks to Martin Ireland

Greetings to everybody out there
bye
Georg
e9125...@stud1.tuwien.ac.at

Other Threads