Google Groups unterstützt keine neuen Usenet-Beiträge oder ‑Abos mehr. Bisherige Inhalte sind weiterhin sichtbar.

DocumentProperties - please!

19 Aufrufe
Direkt zur ersten ungelesenen Nachricht

Mikael Bak

ungelesen,
28.10.1999, 03:00:0028.10.99
an
Hi,

I've been trying to access the printer-configuration dialog box by using the
'DocumentProperties' WinAPI function, but unfortunately I'm doing something
wrong.

The dialog box is displayed, but if I change any value, and click OK, then I
get an exception.
Can someone please take a look at this code and tell me what I'm doing
wrong.

TIA
Mikael

The code (I have a combo box (cmboPrinter) on the form displaying all
installed printers):

procedure TForm1.Button3Click(Sender: TObject);
var
ADevice, ADriver, APort : array[0..255] of char;
ADeviceMode, PrinterHandle: THandle;
DeviceMode : TDeviceMode;
test : LongInt;
begin
Printer.PrinterIndex := cmboPrinter.ItemIndex;
Printer.GetPrinter(ADevice, ADriver, APort, ADeviceMode);
if OpenPrinter(ADevice, PrinterHandle, nil) then
begin
if DocumentProperties( Self.Handle,
PrinterHandle,
ADevice,
DeviceMode,
DeviceMode,
DM_IN_PROMPT ) = IDOK then
begin
ShowMessage('OK pressed');
end;
end;
end;

Peter Below (TeamB)

ungelesen,
28.10.1999, 03:00:0028.10.99
an
> I've been trying to access the printer-configuration dialog box by using the
> 'DocumentProperties' WinAPI function, but unfortunately I'm doing something
> wrong.
> The dialog box is displayed, but if I change any value, and click OK, then I
> get an exception.
> Can someone please take a look at this code and tell me what I'm doing
> wrong.

TDeviceMode is a tricky beast. Like many of the printer-related data items its
size can vary. Basically it consists of a fixed size part, covered by the
TDeviceMode declaration, and a variable part dependend on the printer driver.
So you cannot simply declare a TDeviceMode variable, it will never have the
correct size. You declare a pDeviceMode and allocate the amount of memory
DocumentProperties tells you you need. To get the size you call it once with
Nil for the fourth paramter.

The problem is compounded by the fact that the declaration for
DocumentProperties is plain wrong, it defines the first pDevmode as Const
TDeviceMode and the second as Var. It can be made to work this way but is
logically wrong and makes it hard to pass Nil, which is sometimes needed.
So redeclare it:

// redeclare the DocumentProperties function, the declaration in WinSpool
// is unusuable.
function DocumentProperties(hWnd: HWND; hPrinter: THandle; pDeviceName: PChar;
pDevModeOutput: PDeviceMode; pDevModeInput: PDeviceMode;
fMode: DWORD): Longint; stdcall;
external 'winspool.dll' name 'DocumentPropertiesA';


procedure TForm1.Button2Click(Sender: TObject);
var
hPrinter: THandle;
Device : array[0..255] of char;
Driver : array[0..255] of char;
Port : array[0..255] of char;
hDeviceMode: THandle;
pDevmodeOut: PDeviceMode;
bytesNeeded: DWORD;
begin
Printer.PrinterIndex := combobox1.itemindex;
Printer.GetPrinter(Device, Driver, Port, hDeviceMode);
if WinSpool.OpenPrinter(@Device, hPrinter, nil) then
try
bytesNeeded := DocumentProperties(
handle, hPrinter, Device, Nil, Nil, 0 );
pDevmodeOut := AllocMem( bytesNeeded );
If IDOK = DocumentProperties(
Handle, hPrinter, Device, pDevModeOut, Nil,
DM_OUT_BUFFER or DM_IN_PROMPT )
Then
; // do something intelligent here
FreeMem( pDevModeOut );
finally
WinSpool.ClosePrinter( hPrinter );
end;
end;


Peter Below (TeamB) 10011...@compuserve.com)
No replies in private e-mail, please, unless explicitly requested!


Mikael Bak

ungelesen,
29.10.1999, 03:00:0029.10.99
an
Peter,
Thank you very much for the help. I'll try this after having a large cup of
coffee <g>

/Mikael

Peter Below (TeamB) <10011...@compuXXserve.com> wrote in message
news:VA.00003f34.01070d54@noname...
>
> TDeviceMode is a tricky beast...

0 neue Nachrichten