CreateDC

2.x

  #include <print.h>    

  HDC CreateDC(lpszDriver, lpszDevice, lpszOutput, lpvInitData)    
  LPCSTR lpszDriver; /* address of driver name */
  LPCSTR lpszDevice; /* address of device name */
  LPCSTR lpszOutput; /* address of filename or port name */
  const void FAR* lpvInitData; /* address of initialization data */

The CreateDC function creates a device context for the given device.

Parameters

lpszDriver

Points to a null-terminated string that specifies the MS-DOS filename (without extension) of the device driver (for example, Epson).

lpszDevice

Points to a null-terminated string that specifies the name of the specific device to be supported (for example, Epson FX-80). This parameter is used if the module supports more than one device.

lpszOutput

Points to a null-terminated string that specifies the MS-DOS filename or device name for the physical output medium (file or output port).

lpvInitData

Points to a DEVMODE structure that contains device-specific initialization information for the device driver. The ExtDeviceMode function retrieves this structure already filled in for a given device. The lpvInitData parameter must be NULL if the device driver is to use the default initialization (if any) specified by the user through Windows Control Panel.

The DEVMODE structure has the following form:

#include <print.h>

typedef struct tagDEVMODE {   /* dm */
    char  dmDeviceName[CCHDEVICENAME];
    UINT  dmSpecVersion;
    UINT  dmDriverVersion;
    UINT  dmSize;
    UINT  dmDriverExtra;
    DWORD dmFields;
    int   dmOrientation;
    int   dmPaperSize;
    int   dmPaperLength;
    int   dmPaperWidth;
    int   dmScale;
    int   dmCopies;
    int   dmDefaultSource;
    int   dmPrintQuality;
    int   dmColor;
    int   dmDuplex;
    int   dmYResolution;
    int   dmTTOption;
} DEVMODE;

For a full description of this structure, see the Microsoft Windows Programmer's Reference, Volume 3.

Return Value

The return value is the handle of the device context for the specified device if the function is successful. Otherwise, it is NULL.

Comments

The PRINT.H header file is required if the DEVMODE structure is used.

Device contexts created by using the CreateDC function must be deleted by using the DeleteDC function. All objects selected into the device context after it was created should be selected out and replaced with the original objects before the device context is deleted.

MS-DOS device names follow MS-DOS conventions; an ending colon (:) is recommended, but optional. Windows strips the terminating colon so that a device name ending with a colon is mapped to the same port as the same name without a colon. The driver and port names must not contain leading or trailing spaces.

Example

The following example uses the CreateDC function to create a device context for a printer, using information returned by the PrintDlg function in a PRINTDLG structure:

PRINTDLG   pd;
HDC        hdc;
LPDEVNAMES lpDevNames;
LPSTR      lpszDriverName;
LPSTR      lpszDeviceName;
LPSTR      lpszPortName;

/*
 * PrintDlg displays the common dialog box for printing. The
 * PRINTDLG structure should be initialized with appropriate values.
 */

PrintDlg(&pd);
lpDevNames = (LPDEVNAMES) GlobalLock(pd.hDevNames);
lpszDriverName = (LPSTR) lpDevNames + lpDevNames->wDriverOffset;
lpszDeviceName = (LPSTR) lpDevNames + lpDevNames->wDeviceOffset;
lpszPortName   = (LPSTR) lpDevNames + lpDevNames->wOutputOffset;
GlobalUnlock(pd.hDevNames);
hdc = CreateDC(lpszDriverName, lpszDeviceName, lpszPortName, NULL);

See Also

CreateIC, DeleteDC, ExtDeviceMode, PrintDlg