Bonjour à tous !

Dans l'épisode précédent, nous avons vu comment récupérer les informations contenues dans une structure DEVMODE via l'API DocumentProperties (Merci JBO).

Aujourd'hui nous allons essayer de comprendre comment modifier les paramètres d'impression de notre chère imprimante par programmation via la même API.

Tout d'abord, voici la doc MSDN à ce sujet :
To make changes to print settings that are local to an application, an application should follow these steps:

1. Get the number of bytes required for the full DEVMODE structure by calling DocumentProperties and specifying zero in the fMode parameter.
2. Allocate memory for the full DEVMODE structure.
3. Get the current printer settings by calling DocumentProperties. Pass a pointer to the DEVMODE structure allocated in Step 2 as the pDevModeOutput parameter and specify the DM_OUT_BUFFER value.
4. Modify the appropriate members of the returned DEVMODE structure and indicate which members were changed by setting the corresponding bits in the dmFields member of the DEVMODE.
5. Call DocumentProperties and pass the modified DEVMODE structure back as both the pDevModeInput and pDevModeOutput parameters and specify both the DM_IN_BUFFER and DM_OUT_BUFFER values (which are combined using the OR operator).The DEVMODE structure returned by the third call to DocumentProperties can be used as an argument in a call to the CreateDC function.
Tout est clair jusqu'au 4. : Il est dit dans la doc MSDN qu'il faut vérifier que le changement que l'on veut faire est possible en regardant la valeur contenue dans la variable dmFields de la structure DEVMODE.
Source :
Often, an element of the DEVMODE structure pointed to by pDevMode will be modified (instead of an element of PRINTER_INFO_n). When this is the case, the pDevMode->dmFields flags will tell the application which fields can be changed. Because this is given to you by GetPrinter(), you can check the dmFields flag before attempting the change.
Contenu de la variable dmFields :
dmFields

DWORD that specifies whether certain members of the DEVMODE structure have been initialized.

If a member is initialized, its corresponding bit is set, otherwise the bit is clear.

A printer driver supports only those member of the DEVMODE structure that are appropriate for the printer technology.
La valeur de la variable dmFields est donc une combinaison de différentes valeurs en rapport avec les constantes suivantes :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
	//**************************************** DEVMODE dmFields flags *********************************************
	DM_BITSPERPEL=262144//0x00040000L
	DM_COLLATE=32768//0x00008000L
	DM_COLOR=2048//0x00000800L
	DM_COPIES=256//0x00000100L
	DM_COPY=2//2
	DM_DEFAULTSOURCE=512//0x00000200L
	DM_DISPLAYFLAGS=2097152//0x00200000L
	DM_DISPLAYFREQUENCY=4194304//0x00400000L
	DM_DITHERTYPE=268435456//0x10000000L
	DM_DUPLEX=4096//0x00001000L
	DM_FORMNAME=65536//0x00010000L
	DM_GETDEFID=1024//(WM_USER+0)
	DM_ICCMANUFACTURER=536870912//0x20000000L
	DM_ICCMODEL=1073741824//0x40000000L
	DM_ICMINTENT=67108864//0x04000000L
	DM_ICMMETHOD=33554432//0x02000000L
	DM_LOGPIXELS=131072//0x00020000L
	DM_MEDIATYPE=134217728//0x08000000L
	DM_MODIFY=8//8
	DM_ORIENTATION=1//0x00000001L
	DM_OUT_DEFAULT=1//DM_UPDATE
	DM_PANNINGHEIGHT=16777216//0x01000000L
	DM_PANNINGWIDTH=8388608//0x00800000L
	DM_PAPERLENGTH=4//0x00000004L
	DM_PAPERSIZE=2//0x00000002L
	DM_PAPERWIDTH=8//0x00000008L
	DM_PELSHEIGHT=1048576//0x00100000L
	DM_PELSWIDTH=524288//0x00080000L
	DM_PRINTQUALITY=1024//0x00000400L
	DM_PROMPT=4//4
	DM_REPOSITION=1026//(WM_USER+2)
	DM_SCALE=16//0x00000010L
	DM_SETDEFID=1025//(WM_USER+1)
	DM_SPECVERSION=1025//0x0401
	DM_TTOPTION=16384//0x00004000L
	DM_UPDATE=1//1
	DM_YRESOLUTION=8192//0x00002000L




  • Comment savoir à quoi à quelles constantes correspond la valeur de dmFields (Vous pouvez éventuellement utiliser la valeur que j'ai pour mon imprimante pour exemple : 1851155).
    En C (++ ou #, je n'en ai aucune idée ) le bout de code suivant vérifie que le driver supporte le changement d'orientation de l'impression (portrait/paysage) :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
if (!(pi2->pDevMode->dmFields & DM_ORIENTATION))
{
   ...
}
A quel opérateur du W langage correspond l'opérateur " & " ?





  • Dans la foulée, comment combiner plusieurs constantes et les affecter à dmFields ?


En C (++ ou #) la syntaxe est la suivante :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
lpDevMode->dmFields = DM_ORIENTATION | DM_PRINTQUALITY;
A quel opérateur du W langage correspond l'opérateur " | " ?

De la même manière la ligne suivante plante mon application lors de son appel (sans message d'erreur, l'application se ferme) :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
nReturnValue = API("WINSPOOL.DRV", "DocumentPropertiesA", Handle(), nPrinterHandle, &szSDeviceName, &MyPRINTER_INFO_2:pDevMode, &MyPRINTER_INFO_2:pDevMode, DM_IN_BUFFER OU DM_OUT_BUFFER)
L'erreur vient probablement de l'expression "DM_IN_BUFFER OU DM_OUT_BUFFER", problème qui sera résolu lorsque la question précédente aura trouvé une réponse



C'est à peu près tout pour le moment, je reviendrai à la charge avec quelques questions subsidiaires à l'occasion

Merci d'avance !

[Edit] J'utilise WD14 version 01F140030f et WinXP SP3, si cela peut avoir son importance