The code will automatically adjust for Hi DPI displays that need scaling of the Metafile to the Main Monitor (Monitor 0).
Once the Preview is rendered on a form the form may be moved from one screen to another, just scaling needed on creation.
The other option is to use a Manifest as described in this forum, but that requires changes to existing code to handle high dpi issues.
I found hints of this method online for other programs. Tested on Delphi 7 Windows 10. Should be ok on other versions.
One other issue is mousewheel scrolling in the PrintPreview ScrollBox, WMMouseWheel, in high dpi monitor situation will have invalid Pt values. Seems you could ignore the Pt value as this is always from the ScrollBox.
I added header footer to the demo as shown in the screenshot below.
Comment if you have problems with this suggestion. Hope this helps.
Great and very useful VCL, thanks for providing it and your other units to the community.
- Code: Select all
{ Changes to handle Hi DPI scaling mods }
procedure TPrintPreview.CreateMetafileCanvas(out AMetafile: TMetafile; out ACanvas: TCanvas);
{ Scale to Main Monitor }
function ScaleToMainMontRes(ValI: Integer): Integer;
var TempH: HDC;
begin
TempH := GetDC(0); { Scale to DF (Main) Monitor 0 }
try
Result := Round((ValI * GetDeviceCaps(TempH, VERTRES)) / GetDeviceCaps(TempH, DESKTOPVERTRES));
finally
ReleaseDC(0, TempH);
end;
end;
begin
AMetafile := TMetafile.Create;
try
AMetafile.Width := ScaleToMainMontRes(FDeviceExt.X);
AMetafile.Height := ScaleToMainMontRes(FDeviceExt.Y);
ACanvas := TMetafileCanvas.CreateWithComment(AMetafile, 0,
Format('%s - http://www.delphiarea.com', [ClassName]), PrintJobTitle);
if ACanvas.Handle = 0 then
begin
ACanvas.Free;
ACanvas := nil;
RaiseOutOfMemory;
end;
except
AMetafile.Free;
AMetafile := nil;
raise;
end;
ACanvas.Font.Assign(Font);
ScaleCanvas(ACanvas);
SetBkColor(ACanvas.Handle, RGB(255, 255, 255));
SetBkMode(ACanvas.Handle, TRANSPARENT);
end;
Here is a screenshot of the General Demo on a high DPI system with the main monitor at 175%, normally the Preview would be shrunk in size.