procedure GetPrinter(
var DeviceMode, DeviceNames: THandle);
var
Device, Driver, Port:
array[0..1023]
of char;
DevNames: PDevNames;
Offset: PChar;
begin
Printer.GetPrinter(Device, Driver, Port, DeviceMode);
if DeviceMode <> 0
then
begin
DeviceNames := GlobalAlloc(GHND, SizeOf(TDevNames) +
StrLen(Device) + StrLen(Driver) + StrLen(Port) + 3);
DevNames := PDevNames(GlobalLock(DeviceNames));
try
Offset := PChar(DevNames) + SizeOf(TDevnames);
with DevNames^
do
begin
wDriverOffset := Longint(Offset) - Longint(DevNames);
Offset := StrECopy(Offset, Driver) + 1;
wDeviceOffset := Longint(Offset) - Longint(DevNames);
Offset := StrECopy(Offset, Device) + 1;
wOutputOffset := Longint(Offset) - Longint(DevNames);;
StrCopy(Offset, Port);
end;
finally
GlobalUnlock(DeviceNames);
end;
end;
end;
procedure SetPrinter(DeviceMode, DeviceNames: THandle);
var
DevNames: PDevNames;
begin
DevNames := PDevNames(GlobalLock(DeviceNames));
try
with DevNames^
do
Printer.SetPrinter(PChar(DevNames) + wDeviceOffset,
PChar(DevNames) + wDriverOffset,
PChar(DevNames) + wOutputOffset, DeviceMode);
finally
GlobalUnlock(DeviceNames);
GlobalFree(DeviceNames);
end;
end;
function CopyData(
Handle: THandle): THandle;
var
Src, Dest: PChar;
Size: Integer;
begin
if Handle <> 0
then
begin
Size := GlobalSize(
Handle);
Result := GlobalAlloc(GHND, Size);
if Result <> 0
then
try
Src := GlobalLock(
Handle);
Dest := GlobalLock(Result);
if (Src <>
nil)
and (Dest <>
nil)
then Move(Src^, Dest^, Size);
finally
GlobalUnlock(
Handle);
GlobalUnlock(Result);
end
end
else Result := 0;
end;
procedure THauptForm.SetPrinterFromData;
var
PrintDlgRec: TPrintDlg;
DevHandle: THandle;
begin
FillChar(PrintDlgRec, SizeOf(PrintDlgRec), 0);
with PrintDlgRec
do
begin
lStructSize := SizeOf(PrintDlgRec);
hInstance := SysInit.HInstance;
GetPrinter(DevHandle, hDevNames);
hDevMode := CopyData(DevHandle);
Flags := PD_ENABLESETUPHOOK
or PD_PRINTSETUP;
//lpfnSetupHook := DialogHook;
//hWndOwner := Application.Handle;
//Result := TaskModalDialog(@PrintDlg, PrintDlgRec);
SetPrinter(hDevMode, hDevNames)
end;
end;