delphi - Determine Width of TLabel -
hello stack overflow users
i have tgroupbox
tlabel
in it. tlabel
want display surname , names of candidate. candidates have more 1 name, three, , when happens, tlabel
doesn't fit inside tgroupbox
. when happens, display surname, first name, , rest initials.
in order this, need know whether tlabel
going fit if values assigned it. in other words, need determine width of tlabel
going before assigning values caption
property, bad programming display variable data.
any suggestions?
in vcl, tlabel
uses win32 api drawtext()
function calculate text width, using getdc()
hdc
of screen , selectobject()
select current font
hdc
. have same in own code, eg:
// set label1.autosize false , label1.width // max width ui accept label1 be... function willfitinlabel(label: tlabel; const s: string): boolean; var r: trect; c: tcanvas; dc: hdc; begin r := rect(0, 0, label.width, 0); c := tcanvas.create; try dc := getdc(0); try c.handle := dc; try c.font := label1.font; windows.drawtext(dc, pchar(s), length(s), r, dt_singleline or dt_calcrect); c.handle := 0; end; releasedc(0, dc); end; c.free; end; result := (r.width <= label.width); end; var names: string; begin names := ...; if willfitinlabel(label1, names) label1.caption := names else ... end;
Comments
Post a Comment