Microsoft KB Archive/255744: Difference between revisions
m (Text replacement - "<" to "<") |
m (Text replacement - """ to """) |
||
Line 110: | Line 110: | ||
*!* Allocate a buffer | *!* Allocate a buffer | ||
lcBuffer = SPACE(255) | lcBuffer = SPACE(255) | ||
IF | IF "5.0"$OS() | ||
*!* If the OS is 5.0, the driver is either DISPLAY or WINSPOOL | *!* If the OS is 5.0, the driver is either DISPLAY or WINSPOOL | ||
lcDriver= | lcDriver="WINSPOOL" | ||
ELSE | ELSE | ||
*!* If the OS is Windows 9x, the driver is NULL | *!* If the OS is Windows 9x, the driver is NULL | ||
Line 121: | Line 121: | ||
hDC = CreateDC(lcDriver, lcPrinter, NULL, NULL) | hDC = CreateDC(lcDriver, lcPrinter, NULL, NULL) | ||
IF hDC = 0 | IF hDC = 0 | ||
MESSAGEBOX( | MESSAGEBOX("Unable to create device context" + CHR(13) + ; | ||
"Error Code : " + ALLTRIM(STR(GetLastError())),48) | |||
RETURN | RETURN | ||
ENDIF | ENDIF | ||
Line 130: | Line 130: | ||
retCode = StartDoc(hDC, @lcDocInfo) | retCode = StartDoc(hDC, @lcDocInfo) | ||
IF retCode <= 0 | IF retCode <= 0 | ||
MESSAGEBOX( | MESSAGEBOX("Error calling StartDoc" + CHR(13) + ; | ||
" Error code : " + ALLTRIM(STR(GetLastError())),48) | |||
RETURN | RETURN | ||
ENDIF | ENDIF | ||
Line 137: | Line 137: | ||
retCode = StartPage(hDC) | retCode = StartPage(hDC) | ||
IF retCode <= 0 | IF retCode <= 0 | ||
MESSAGEBOX( | MESSAGEBOX("Error calling StartPage" + CHR(13) + ; | ||
" Error code : " + ALLTRIM(STR(GetLastError())),48) | |||
RETURN | RETURN | ||
ENDIF | ENDIF | ||
*!* Define a string to send to the print device | *!* Define a string to send to the print device | ||
myString = | myString = "Using API functions for printing" | ||
myLength = LEN(myString) | myLength = LEN(myString) | ||
*!* Call TextOut, first parameter is the device context handle | *!* Call TextOut, first parameter is the device context handle | ||
Line 151: | Line 151: | ||
retCode = TextOut(hDC, 10, 10, @myString, myLength) | retCode = TextOut(hDC, 10, 10, @myString, myLength) | ||
IF retCode <= 0 | IF retCode <= 0 | ||
MESSAGEBOX( | MESSAGEBOX("Error calling TextOut" + CHR(13) + ; | ||
" Error code : " + ALLTRIM(STR(GetLastError())),48) | |||
RETURN | RETURN | ||
ENDIF | ENDIF | ||
Line 158: | Line 158: | ||
retCode = EndPage(hDC) | retCode = EndPage(hDC) | ||
IF retCode <= 0 | IF retCode <= 0 | ||
MESSAGEBOX( | MESSAGEBOX("Error calling EndPage" + CHR(13) + ; | ||
" Error code : " + ALLTRIM(STR(GetLastError())),48) | |||
RETURN | RETURN | ||
ENDIF | ENDIF | ||
Line 165: | Line 165: | ||
retCode = EndDoc(hDC) | retCode = EndDoc(hDC) | ||
IF retCode <= 0 | IF retCode <= 0 | ||
MESSAGEBOX( | MESSAGEBOX("Error calling EndDoc" + CHR(13) + ; | ||
" Error code : " + ALLTRIM(STR(GetLastError())),48) | |||
RETURN | RETURN | ||
ENDIF | ENDIF | ||
Line 175: | Line 175: | ||
PARAMETERS m.longval | PARAMETERS m.longval | ||
PRIVATE i, m.retstr | PRIVATE i, m.retstr | ||
m.retstr = | m.retstr = "" | ||
FOR i = 24 TO 0 STEP -8 | FOR i = 24 TO 0 STEP -8 | ||
m.retstr = CHR(INT(m.longval/(2^i))) + m.retstr | m.retstr = CHR(INT(m.longval/(2^i))) + m.retstr |
Latest revision as of 13:53, 21 July 2020
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:
Parameter Name | Data Passed with Parameter |
---|---|
lpszDriver | Windows NT: Pointer to a null-terminated character string that specifies either DISPLAY for a display driver, or the name of a printer driver, which is usually WINSPOOL.
|
lpszDevice | Pointer to a null-terminated character string that specifies the name of the specific output device being used, as shown by the Print Manager. The lpszDevice parameter must be used. |
lpszOutput | This parameter is ignored for Win32-based applications and should be set to NULL. |
lpInitData | The lpInitData parameter must be NULL if the device driver is to use the default initialization (if any) specified by the user. |
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 the CreateDC function DECLARE LONG CreateDC IN gdi32.DLL ; STRING @cDriver, STRING @cDevice, STRING cOutput, ; STRING cInitData *!* Declare the DeleteDC function DECLARE LONG DeleteDC IN gdi32.DLL ; LONG nDC *!* Declare the TextOut function DECLARE INTEGER TextOut IN gdi32.DLL INTEGER hDC, INTEGER xStart, INTEGER yStart, ; STRING @PrintString, INTEGER cbString *!* Declare the StartDoc function DECLARE INTEGER StartDoc IN gdi32.DLL INTEGER hDC, STRING @DocInfo *!* Declare the StartPage function DECLARE INTEGER StartPage IN gdi32.DLL INTEGER hDC *!* Declare the EndPage function DECLARE INTEGER EndPage IN gdi32.DLL INTEGER hDC *!* Declare the EndDoc function DECLARE INTEGER EndDoc IN gdi32.DLL INTEGER hDC *!* Declare the GetLastError function DECLARE INTEGER GetLastError IN kernel32.DLL *!* Allocate a buffer 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 *!* Get the name of the printer for which we want an HDC 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 *!* Create a DOCINFO structure to pass to StartDoc lcDocInfo = Long2Str(20) + REPLICATE(CHR(0), 16) *!* Call StartDoc() retCode = StartDoc(hDC, @lcDocInfo) IF retCode <= 0 MESSAGEBOX("Error calling StartDoc" + CHR(13) + ; " Error code : " + ALLTRIM(STR(GetLastError())),48) RETURN ENDIF *!* Call StartPage() retCode = StartPage(hDC) IF retCode <= 0 MESSAGEBOX("Error calling StartPage" + CHR(13) + ; " Error code : " + ALLTRIM(STR(GetLastError())),48) RETURN ENDIF *!* Define a string to send to the print device myString = "Using API functions for printing" myLength = LEN(myString) *!* 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 retCode = TextOut(hDC, 10, 10, @myString, myLength) IF retCode <= 0 MESSAGEBOX("Error calling TextOut" + CHR(13) + ; " Error code : " + ALLTRIM(STR(GetLastError())),48) RETURN ENDIF *!* Done printing a page, so call EndPage() retCode = EndPage(hDC) IF retCode <= 0 MESSAGEBOX("Error calling EndPage" + CHR(13) + ; " Error code : " + ALLTRIM(STR(GetLastError())),48) RETURN ENDIF *!* Done printing a document, so call EndDoc() retCode = EndDoc(hDC) IF retCode <= 0 MESSAGEBOX("Error calling EndDoc" + CHR(13) + ; " Error code : " + ALLTRIM(STR(GetLastError())),48) RETURN ENDIF *!* Clear the DLLs from memory CLEAR DLLS 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 * End of Code
(c) Microsoft Corporation 2000, All Rights Reserved. Contributions by John Desch, Microsoft Corporation.
REFERENCES
MSDN Help, *Platform SDK, Graphics and Multimedia Services, Topics CreateDC, DeleteDC, TextOut, StartDoc, EndDoc, StartPage, EndPage
Keywords: kbhowto kbprint kbapi kbcodesnippet KB255744