12. oktober 2005 - 15:48
Der er
1 løsning
96 DPI vs 120 DPI
Hej!
Kan jeg ikke lave en Delphi form hvor en label altid har samme højde i pixels på skærmen.
Når jeg skifter fra 96 til 120 DPI på skærmen så ændrer alle mine componenter også størrelse på indholdet ihvertilfælde.
Jeg har prøvet at slå scale til/fra men det virker ikke.
--nop
12. oktober 2005 - 18:22
#1
Fundet selv:
procedure FixDPI(f: TForm; DesignDPI: integer);
(*
Small/Large system font solution
Have a TForm's properties Scaled and AutoScroll set to false
and after creating your form call this procedure like:
FixDPI (TForm1, 96) // If you designed in small fonts
Author: Tomislav Kardas, Zagreb, Croatia, Europe
Platform: Delphi 4 C/S
*)
var
ScaleM, ScaleD: integer;
function Scale(x: longint): longint;
begin
result := (x*ScaleM + ScaleD div 2) div ScaleD;
end;
procedure FixControl(c: TControl);
var
x: integer;
p: TWinControl;
fixheight: boolean;
begin
p := c.Parent;
if c.Align <> alNone then begin
// Fixing width
if ((c.Align = alLeft) and (akRight in c.Anchors)) or
((c.Align = alRight) and (akLeft in c.Anchors)) then
c.Width := p.ClientWidth - Scale(p.ClientWidth - c.Width)
else if (c.Align = alLeft) or (c.Align = alRight) then
c.Width := Scale(c.Width);
// Fixing height
if ((c.Align = alTop) and (akBottom in c.Anchors)) or
((c.Align = alBottom) and (akTop in c.Anchors)) then
c.Height := p.ClientHeight - Scale(p.ClientHeight - c.Height)
else if (c.Align = alTop) or (c.Align = alBottom) then
c.Height := Scale(c.Height);
end
else begin
// Fixing width
x := p.ClientWidth - c.Width - c.Left;
if akLeft in c.Anchors then begin
c.Left := Scale(c.Left);
if akRight in c.Anchors then
c.Width := p.ClientWidth - c.Left - Scale(x)
else
c.Width := Scale(c.Width);
end
else if akRight in c.Anchors then begin
c.Width := Scale(c.Width);
c.Left := p.ClientWidth - c.Width - Scale(x);
end
else begin
c.Left := Scale(c.Left);
c.Width := Scale(c.Width);
end;
// Fixing height
fixheight := true;
if (c is TCustomEdit) and not (c is TCustomMemo) then
fixheight := false;
x := p.ClientHeight - c.Height - c.Top;
if akTop in c.Anchors then begin
c.Top := Scale(c.Top);
if fixheight then begin
if akBottom in c.Anchors then
c.Height:= p.ClientHeight - c.Top - Scale(x)
else
c.Height:= Scale(c.Height);
end;
end
else if akBottom in c.Anchors then begin
if fixheight then
c.Height := Scale(c.Height);
c.Top := p.ClientHeight - c.Height - Scale(x);
end
else begin
c.Top := Scale(c.Top);
if fixheight then
c.Height := Scale(c.Height);
end;
end;
end;
procedure FixControls(c: TWinControl);
var
i: integer;
begin
for i := 0 to c.ControlCount - 1 do
if c.Controls[i].Owner = f then begin
FixControl(c.Controls[i]);
if c.Controls[i] is TWinControl then
FixControls(TWinControl(c.Controls[i]));
end;
end;
begin
if DesignDPI <> Screen.PixelsPerInch then begin
ScaleM := Screen.PixelsPerInch;
ScaleD := DesignDPI;
f.ClientWidth := Scale(f.ClientWidth);
f.ClientHeight := Scale(f.ClientHeight);
FixControls(f);
end;
end;