1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
|
{%MainUnit ../printersdlgs.pp}
// update from win32wsdialogs.pp
procedure Reposition(ADialogWnd: Handle);
var
Left, Top: Integer;
ABounds, DialogRect: TRect;
begin
// Btw, setting width and height of dialog does not reposition child controls :(
// So no way to set another height and width at least here
if (GetParent(ADialogWnd) = Widgetset.AppHandle) then
begin
if Screen.ActiveCustomForm <> nil then
ABounds := Screen.ActiveCustomForm.Monitor.BoundsRect
else
if Application.MainForm <> nil then
ABounds := Application.MainForm.Monitor.BoundsRect
else
ABounds := Screen.PrimaryMonitor.BoundsRect;
end
else
ABounds := Screen.MonitorFromWindow(GetParent(ADialogWnd)).BoundsRect;
GetWindowRect(ADialogWnd, DialogRect);
Left := (ABounds.Right - DialogRect.Right + DialogRect.Left) div 2;
Top := (ABounds.Bottom - DialogRect.Bottom + DialogRect.Top) div 2;
SetWindowPos(ADialogWnd, HWND_TOP, Left, Top, 0, 0, SWP_NOSIZE);
end;
function PrintHookProc(hdlg: HWND; uiMsg: UINT; {%H-}wParam: WPARAM; lParam: LPARAM): UINT_PTR; stdcall;
var
lpp: PtagPD;
begin
if uiMsg = WM_INITDIALOG then
begin
lpp := PtagPD(lParam);
if (lParam<>0) and (lpp^.lCustData<>0) then
SetWindowTextW(hdlg, pwidechar(lpp^.lCustData));
Reposition(hdlg);
end;
Result := 0;
end;
function PageSetupHookProc(hdlg: HWND; uiMsg: UINT; {%H-}wParam: WPARAM; lParam: LPARAM): PtrUInt; stdcall;
var
lpp : PtagPSD;
begin
if uiMsg = WM_INITDIALOG then
begin
lpp := PtagPSD(lParam);
if (lParam<>0) and (lpp^.lCustData<>0) then
SetWindowTextW(hdlg, pwidechar(lpp^.lCustData));
Reposition(hdlg);
end;
Result := 0;
end;
function GetOwnerHandle(ADialog : TCommonDialog): HWND;
begin
with ADialog do
if Owner is TWinControl then
Result := TWinControl(Owner).Handle
else
Result := WidgetSet.AppHandle;
end;
{ TPageSetupDialog }
function TPageSetupDialog.DoExecute: Boolean;
var
lpp : tagPSD;
PDev : TPrinterDevice;
DeviceMode : THandle;
DevNames : PDevNames;
DevModeW : PDeviceModeW;
StW : PWidechar;
BoolRes: BOOL;
begin
Result := False;
if not Assigned(Printer) then Exit;
if Printer.Printers.Count > 0 then
begin
FillChar(lpp, SizeOf(lpp), 0);
with lpp do
begin
lStructSize := SizeOf(lpp);
hInstance := System.HInstance;
lpfnPageSetupHook := @PageSetupHookProc;
if Title<>'' then
begin
lCustData := LPARAM(pWideChar(UTF8Decode(Title)));
end
else
lCustData := 0;
Flags := PSD_MARGINS or PSD_ENABLEPAGESETUPHOOK;
hWndOwner := GetOwnerHandle(Self);
// TODO: check that no need to div by 1000 values below
rtMargin.Left := MarginLeft;
rtMargin.Top := MarginTop;
rtMargin.Right := MarginRight;
rtMargin.Bottom := MarginBottom;
PDev := TPrinterDevice(Printer.Printers.Objects[Printer.PrinterIndex]);
// Pdev.DevMode has the required size, just copy to the global memory
DeviceMode := GLobalAlloc(GHND, PDev.DevModeSize);
try
DevModeW := PDeviceModeW(GlobalLock(DeviceMode));
try
CopyMemory(DevModeW, PDev.DevModeW, Pdev.DevModeSize);
finally
GlobalUnlock(DeviceMode);
end;
hDevMode := DeviceMode;
BoolRes := PageSetupDlgW(@Lpp);
if BoolRes then
begin
StW := '';
if Lpp.HdevNames <> 0 then
begin
DevNames := PDevNames(GlobalLock(lpp.hDevNames));
try
StW := PWidechar(DEVNames) + DevNames^.wDeviceOffset;
Printer.SetPrinter(UTF8Encode(widestring(StW)));
finally
GlobalUnlock(lpp.hDevNames);
GlobalFree(lpp.hDevNames);
end;
end;
Result:=True;
if (Flags and PSD_INHUNDREDTHSOFMILLIMETERS)>0 then
fUnits := pmMillimeters
else
fUnits := pmInches;
MarginLeft := rtMargin.Left;
MarginTop := rtMargin.Top;
MarginRight := rtMargin.Right;
MarginBottom := rtMargin.Bottom;
if lpp.hDevMode <> 0 then
begin
DevModeW := PDeviceModeW(GlobalLock(lpp.hDevMode));
try
//Set the properties for the selected printer
PDev := TPrinterDevice(Printer.Printers.Objects[Printer.PrinterIndex]);
CopyMemory(PDev.DevModeW, DevModeW, PDev.DevModeSize);
finally
GlobalUnlock(lpp.hDevMode);
end;
end;
end;
finally
GlobalFree(DeviceMode);
end;
end;
end;
end;
{ TPrinterSetupDialog }
function TPrinterSetupDialog.DoExecute: Boolean;
var
lpp : tagPD;
PDev : TPrinterDevice;
DeviceMode : THandle;
DevNames : PDevNames;
DevModeW : PDeviceModeW;
StW : PWidechar;
BoolRes: BOOL;
begin
Result:=False;
if not Assigned(Printer) then Exit;
if Printer.Printers.Count>0 then
begin
FillChar(lpp, SizeOf(lpp), 0);
with lpp do
begin
lStructSize := SizeOf(lpp);
hInstance := System.HInstance;
lpfnSetupHook := @PrintHookProc;
if Title<>'' then
lCustData := LPARAM(pWideChar(UTF8Decode(Title)))
else
lCustData := 0;
Flags := PD_PRINTSETUP or PD_RETURNDC or PD_ENABLESETUPHOOK;
hWndOwner := GetOwnerHandle(self);
PDev := TPrinterDevice(Printer.Printers.Objects[Printer.PrinterIndex]);
// Pdev.DevMode has the required size, just copy to the global memory
DeviceMode := GlobalAlloc(GHND, PDev.DevModeSize);
try
DevModeW := PDeviceModeW(GlobalLock(DeviceMode));
try
CopyMemory(DevModeW, Pdev.DevModeW, Pdev.DevModeSize);
finally
GlobalUnlock(DeviceMode);
end;
hDevMode := DeviceMode;
BoolRes := PrintDlgW(@lpp);
if BoolRes then
begin
StW := '';
//Change Selected printer
if lpp.hDevNames <> 0 then
begin
DevNames := PDevNames(GlobalLock(lpp.hDevNames));
try
StW := PWidechar(DevNames) + DevNames^.wDeviceOffset;
Printer.SetPrinter(UTF8Encode(widestring(StW)));
finally
GlobalUnlock(lpp.hDevNames);
GlobalFree(lpp.hDevNames);
end;
end;
Result := True;
if lpp.hDevMode <> 0 then
begin
DevModeW := PDeviceModeW(GlobalLock(lpp.hDevMode));
try
//Set the properties for the selected printer
PDev := TPrinterDevice(Printer.Printers.Objects[Printer.PrinterIndex]);
CopyMemory(PDev.DevModeW, DevModeW, PDev.DevModeSize);
TWinPrinter(Printer).Handle := hDC;
finally
GlobalUnlock(lpp.hDevMode);
end;
end;
end;
finally
GlobalFree(DeviceMode);
end;
end;
end;
end;
{ TPrintDialog }
function TPrintDialog.DoExecute: Boolean;
var
lpp : tagPD;
PDev : TPrinterDevice;
DeviceMode : THandle;
DevNames : PDevNames;
DevModeW : PDeviceModeW;
StW : PWidechar;
BoolRes: BOOL;
Index: Integer;
begin
Result := False;
if not Assigned(Printer) then Exit;
if Printer.Printers.Count > 0 then
begin
FillChar(lpp, SizeOf(lpp), 0);
with lpp do
begin
lStructSize := SizeOf(lpp);
hInstance := System.HInstance;
lpfnPrintHook := @PrintHookProc;
lpfnSetupHook := @PrintHookProc;
if Title<>'' then
begin
lCustData := LPARAM(pWideChar(UTF8Decode(Title)));
end
else
lCustData := 0;
Flags := PD_ENABLEPRINTHOOK or PD_ENABLESETUPHOOK;
if not Printer.RawMode then
Flags := Flags or PD_RETURNDC;
if Collate then
Flags := Flags or PD_COLLATE;
case PrintRange of
prPageNums: Flags := Flags or PD_PAGENUMS;
prSelection: Flags := Flags or PD_SELECTION;
end;
if PrintToFile then Flags := Flags or PD_PRINTTOFILE;
if not (poPrintToFile in Options) then Flags := Flags or PD_HIDEPRINTTOFILE;
if not (poPageNums in Options) then Flags := Flags or PD_NOPAGENUMS;
if not (poSelection in Options) then Flags := Flags or PD_NOSELECTION;
if (poPrintToFile in Options ) and (poDisablePrintToFile in Options) then Flags := Flags or PD_DISABLEPRINTTOFILE;
if (poHelp in Options) then Flags := Flags or PD_SHOWHELP;
if not (poWarning in Options) then Flags := Flags or PD_NOWARNING;
hWndOwner := GetOwnerHandle(self);
PDev := TPrinterDevice(Printer.Printers.Objects[Printer.PrinterIndex]);
// Pdev.DevMode has the required size, just copy to the global memory
DeviceMode := GlobalAlloc(GHND, PDEV.DevModeSize);
try
DevModeW := PDeviceModeW(GlobalLock(DeviceMode));
try
CopyMemory(DevModeW, PDev.DevModeW, PDev.DevModeSize);
finally
GlobalUnlock(DeviceMode);
end;
hDevMode := DeviceMode;
nCopies := Word(Copies);
nFromPage := Word(FromPage);
nToPage := Word(ToPage);
nMinPage := Word(MinPage);
nMaxPage := Word(MaxPage);
DevModeW^.dmCopies := nCopies;
BoolRes := PrintDlgW(@lpp);
if BoolRes then
begin
StW := '';
//Change Selected printer
if lpp.hDevNames <> 0 then
begin
DevNames := PDevNames(GlobalLock(lpp.hDevNames));
try
StW := PWidechar(DevNames) + DevNames^.wDeviceOffset;
Printer.SetPrinter(UTF8Encode(widestring(stW)));
finally
GlobalUnlock(lpp.hDevNames);
GlobalFree(lpp.hDevNames);
end;
end;
Result:=True;
// printer might have changed, check if new printer
// support extended device modes
PDev:=TPrinterDevice(Printer.Printers.Objects[Printer.PrinterIndex]);
if (lpp.hDevMode<>0) and (Pdev.DevModeW<>nil) then
begin
DevModeW := PDeviceModeW(GlobalLock(lpp.hDevMode));
try
CopyMemory(PDev.DevModeW,DevModeW,PDev.DevModeSize);
Index := Printer.PaperSize.SupportedPapers.IndexOfObject(TObject(ptrint(DevModeW^.dmPaperSize)));
if Index <> -1 then
begin
PDev.DevModeW^.dmPaperSize := DevModeW^.dmPaperSize;
end
else
begin
PDev.DevModeW^.dmPaperSize := PDev.DefaultPaper;
end;
if nCopies=1 then
begin
Copies := DevModeW^.dmCopies;
end
else
Copies := nCopies;
Printer.Copies := Copies;
if not Printer.RawMode then
TWinPrinter(Printer).Handle := hDC;
finally
GlobalUnlock(lpp.hDevMode);
end;
end;
PrintRange := prAllPages;
PrintToFile := false;
Collate := false;
if (Flags and PD_SELECTION)>0 then PrintRange := prSelection;
if (Flags and PD_PAGENUMS)>0 then PrintRange := prPageNums;
if (Flags and PD_PRINTTOFILE)>0 then PrintToFile := true;
if (Flags and PD_COLLATE)>0 then Collate := true;
FromPage := Integer(nFromPage);
ToPage := Integer(nToPage);
MinPage := Integer(nMinPage);
MaxPage := Integer(nMaxPage);
end;
finally
GlobalFree(DeviceMode);
end;
end;
end;
end;
|