Microsoft KB Archive/255744

= How To Obtain a Device Context Handle for a Print Device =

Article ID: 255744

Article Last Modified on 7/1/2004

-

APPLIES TO


 * Microsoft Visual FoxPro 3.0 Standard Edition
 * Microsoft Visual FoxPro 3.0b Standard Edition
 * Microsoft Visual FoxPro 5.0 Standard Edition
 * Microsoft Visual FoxPro 5.0a
 * Microsoft Visual FoxPro 6.0 Professional Edition

-



This article was previously published under Q255744



SUMMARY
When attempting to send output directly to a print device using the StartDoc API function, it is necessary to obtain a device context handle for the print device. This article explains how to obtain a device context handle for a print device.



MORE INFORMATION
An application requires a display device context handle (hdc) before it can begin drawing in the client area of a window. Similarly, a print device requires a special printer hdc before it can begin sending output to a printer. A printer hdc is an internal data structure that defines a set of graphic objects and their associated attributes, and specifies the graphic modes that affect output. The graphic objects include a pen (for line drawing), a brush (for painting and filling), and a font (for text output).

For a display device, the GetDC, GetDCEx, and GetWindowDC functions can be used to return an hdc, because those device contexts are owned by the windows management component. Unlike a display device context, printer device contexts are not owned by the windows management component, and cannot be obtained by calls to the GetDC, GetDCEx, or GetWindowDC functions. Instead, to obtain a handle to a printer device context, an application must call either the CreateDC or the PrintDlg function.

The CreateDC function accepts the following parameters:

The following code illustrates creating a device context handle for a print device.

Please note that this code is somewhat complex, and engineers may require some time to familiarize themselves with the contents of the example. * Start of Code DECLARE LONG CreateDC IN gdi32.DLL ; STRING @cDriver, STRING @cDevice, STRING cOutput, ; STRING cInitData DECLARE LONG DeleteDC IN gdi32.DLL ; LONG nDC DECLARE INTEGER TextOut IN gdi32.DLL INTEGER hDC, INTEGER xStart, INTEGER yStart, ; STRING @PrintString, INTEGER cbString DECLARE INTEGER StartDoc IN gdi32.DLL INTEGER hDC, STRING @DocInfo DECLARE INTEGER StartPage IN gdi32.DLL INTEGER hDC DECLARE INTEGER EndPage IN gdi32.DLL INTEGER hDC DECLARE INTEGER EndDoc IN gdi32.DLL INTEGER hDC DECLARE INTEGER GetLastError IN kernel32.DLL lcBuffer = SPACE(255) IF "5.0"$OS *!* If the OS is 5.0, the driver is either DISPLAY or WINSPOOL lcDriver="WINSPOOL" ELSE *!* If the OS is Windows 9x, the driver is NULL lcDriver=NULL ENDIF lcPrinter=GETPRINTER hDC = CreateDC(lcDriver, lcPrinter, NULL, NULL) IF hDC = 0 MESSAGEBOX("Unable to create device context" + CHR(13) + ;     "Error Code : " + ALLTRIM(STR(GetLastError)),48) RETURN ENDIF lcDocInfo = Long2Str(20) + REPLICATE(CHR(0), 16) retCode = StartDoc(hDC, @lcDocInfo) IF retCode <= 0 MESSAGEBOX("Error calling StartDoc" + CHR(13) + ;     " Error code : " + ALLTRIM(STR(GetLastError)),48) RETURN ENDIF retCode = StartPage(hDC) IF retCode <= 0 MESSAGEBOX("Error calling StartPage" + CHR(13) + ;     " Error code : " + ALLTRIM(STR(GetLastError)),48) RETURN ENDIF myString = "Using API functions for printing" myLength = LEN(myString) retCode = TextOut(hDC, 10, 10, @myString, myLength) IF retCode <= 0 MESSAGEBOX("Error calling TextOut" + CHR(13) + ;     " Error code : " + ALLTRIM(STR(GetLastError)),48) RETURN ENDIF retCode = EndPage(hDC) IF retCode <= 0 MESSAGEBOX("Error calling EndPage" + CHR(13) + ;     " Error code : " + ALLTRIM(STR(GetLastError)),48) RETURN ENDIF retCode = EndDoc(hDC) IF retCode <= 0 MESSAGEBOX("Error calling EndDoc" + CHR(13) + ;     " Error code : " + ALLTRIM(STR(GetLastError)),48) RETURN ENDIF CLEAR DLLS
 * !* Declare the CreateDC function
 * !* Declare the DeleteDC function
 * !* Declare the TextOut function
 * !* Declare the StartDoc function
 * !* Declare the StartPage function
 * !* Declare the EndPage function
 * !* Declare the EndDoc function
 * !* Declare the GetLastError function
 * !* Allocate a buffer
 * !* Get the name of the printer for which we want an HDC
 * !* Create a DOCINFO structure to pass to StartDoc
 * !* Call StartDoc
 * !* Call StartPage
 * !* Define a string to send to the print device
 * !* Call TextOut, first parameter is the device context handle
 * !* second parameter is x coordinate
 * !* third parameter is y coordinate
 * !* fourth parameter is the string, which is passed by reference
 * !* fifth parameter is the length of the string being passed
 * !* Done printing a page, so call EndPage
 * !* Done printing a document, so call EndDoc
 * !* Clear the DLLs from memory

FUNCTION Long2Str PARAMETERS m.longval PRIVATE i, m.retstr m.retstr = "" FOR i = 24 TO 0 STEP -8 m.retstr = CHR(INT(m.longval/(2^i))) + m.retstr m.longval = MOD(m.longval, (2^i)) NEXT RETURN m.retstr (c) Microsoft Corporation 2000, All Rights Reserved. Contributions by John Desch, Microsoft Corporation.
 * End of Code

