I'vew been running into problems getting and setting correct font sizes.
Since I'm new to this list, I searched the archives, but could find no
reference. What I did find was a problem with GetTextExtentPoint32( STRING,
[FONT] ) reported to always deliver values that were too large. I didn't
investigate, but it might be related to what I'm seeing.
I don't know if it's platform dependent (I'm working on Win2000 SP1) but I hope
not: if so, my workaround would not work on other platfoms (Win95/98/ME?).
The first thing I noticed was then when I create a Textfield and set the font
size for it, the fiont size ended up actually smaller than what I specified.
Other weird things happened too.
So, making a variation on the example program found with the package, I started
to experiment and found a number of interlocking problems. Here's a summary:
- To ceate a font, there are both size and height attributes. Both should set
the font size in points (I think).
- When using a font size for a control, it turns out too small
- When using the Font dialog, both size and height are returned as output; but
both are incorrect: size is size as specified in the dialog - only 10 times
larger; height is repirted as a negative number, but even the absolute value is
too large.
It took a bit of trial and error to figure out what "too small" and "too large"
actually meant but looking at the numbers I got a sudden hunch: screens are
often assumed to be 72 pixels per inch - but for PCs that is actually (supposed
to be) 96 pixels per inch. Guess what: the proportion 96/72 fit exactly the too
large/too small values I was experiencing.
My workarounds:
- To specify a font size for a control, multiply by 96/72 and round up to the
nearest integer
- If output from a Font dialog is to be used to change the font size for a
control, *delete* the size attribute (it's 10 x too large) and take the
absolute value of the reported height attribute (still too large inreal terms,
but that has the 96/72 "correction" already built in).
Examples using the workarounds:
#create a Font to use in a control:
$fontsize = 8; # intended size
$fontsize = int($fontsize*(96/72)+0.5); # workaround for specifying size
$fontname = "Lucida console";
#print "fudged font size: $fontsize \n";
$dispFont = new Win32::GUI::Font(
-name => $fontname,
-size => $fontsize,
);
#now use it:
$W->AddTextfield(
-name => "dispDtdFile",
-multiline => 1,
-vscroll => 1,
-hscroll => 1,
-top => $titleheight+2*$pad,
-left => $lblleft,
-width => $w-2*$pad,
-height => $th-2*$btnvspace,
-font => $dispFont,
-background => $txtback,
-foreground => $txtfore, # try if we can color it
);
#Change font size for the control (a button "Font..." calls up the Font dialog):
sub btnFont_Click {
my $key = ();
$W->Disable(); # disable window before showing
font dialog
# --- current font ----------------------
my $hcurFont = $W->dispDtdFile->GetFont();
my %fontdetails = Win32::GUI::Font::Info($hcurFont);
# --- choose font -----------------------
my @newFont = Win32::GUI::ChooseFont(%fontdetails);
%fontdetails = @newFont;
# --- create new font -----------------------
# make -height positive and delete -size to create "corrected" input for
creating new font
$fontdetails{"-height"} = abs($fontdetails{"-height"});
delete $fontdetails{"-size"};
# --- apply font -----------------------
# create new font object with these parameters
my $newFontObj = new Win32::GUI::Font(%fontdetails);
$dispFont = $newFontObj; # redefine global font object;
$newFontObj gets destroyed
$W->dispDtdFile->SetFont($dispFont); # undocumented method!!!
$W->dispDtdFile->Hide(); # force repaint
$W->Enable();
$W->SetForegroundWindow();
show_controls();
}
Cheers,
Marjolein Katsma
HomeSite Help - http://hshelp.com/ - Extensions, Tips and Tools