Declare Function CreateDC Lib "gdi32.dll" Alias "CreateDCA" (ByVal lpszDriver As String, ByVal lpszDevice As String, ByVal lpszOutput As Long, lpInitData As Any) As Long
CreateDC creates a device context to a given object. The object is identified by its name. When you are finished using the device context in your program, use the DeleteDC function to destroy it. Do not use ReleaseDC with device contexts created by this function!
If an error occured, the function returns 0 (Windows NT, 2000: use GetLastError to get the error code). If successful, the function returns a handle to the newly created device context.
When passing 0 for lpInitData, the expression ByVal CLng(0) must be used.
Example:
' Print out a page with an ellipse drawn with a thickened black
' pen on it. The page is printed on the computer's default printer.
' The following are special declarations needed to allow string
' manipulation functions to use pointers to strings.
Declare Function lstrcpy Lib "kernel32.dll" Alias "lstrcpyA" (ByVal lpString1 As String, ByVal lpString2 As Long) As Long
Declare Function lstrlen Lib "kernel32.dll" Alias "lstrlenA" (ByVal lpString As Long) As Long
' Variable declarations
Dim hPrintDC As Long ' handle to the printer's device context
Dim di As DOCINFO ' information about the document to print
Dim hPen As Long ' handle to the pen to draw the ellipse with
Dim hOldPen As Long ' handle to the printer's previously selected pen
Dim buffer(0 To 3076 / 4) As Long ' 3076-byte buffer
Dim pi2 As PRINTER_INFO_2 ' receives info about the default printer
Dim printret As Long ' receives the number of printers returned from EnumPrinters
Dim spaceneeded As Long ' receives space requires for EnumPrinters
Dim retval As Long ' return value
' Get the device and driver names of the default printer. For a more detailed
' description of the semi-confusing code below, consult the
' EnumPrinters page.
retval = EnumPrinters(PRINTER_ENUM_DEFAULT, "", 2, buffer(0), 3076, spaceneeded, printret)
If retval = 0 Then
Debug.Print "No default printer is configured."
End ' abort the program
End If
' Copy the device and driver names to the structure. All the
' other information retrieved is not needed and is omitted here.
pi2.pPrinterName = Space(lstrlen(buffer(1)))
retval = lstrcpy(pi2.pPrinterName, buffer(1))
pi2.pDriverName = Space(lstrlen(buffer(4)))
retval = lstrcpy(pi2.pDriverName, buffer(4))
' Create a device context to the printer, using its default initialization.
hPrintDC = CreateDC("", pi2.pPrinterName, 0, ByVal CLng(0))
' Create a solid black brush with a thickness of 5.
hPen = CreatePen(PS_SOLID, 5, RGB(0, 0, 0))
' Load information about the document to print into the structure.
di.cbSize = Len(di) ' size of structure
di.lpszDocName = "Printer API Demonstration" ' name of document
di.lpszOutput = 0 ' do not print to a file
di.lpszDatatype = "" ' data type of file doesn't apply
di.fwType = 0 ' no additional information
' Begin the print job.
retval = StartDoc(hPrintDC, di)
' Begin the first and only page to print.
retval = StartPage(hPrintDC)
' Select the pen for use with the printer.
hOldPen = SelectObject(hPrintDC, hPen)
' Draw an ellipse with bounding rectangle corners (1000,1500)-(2000,3000)
retval = Ellipse(hPrintDC, 1000, 1500, 2000, 3000)
' Restore the printer's previously selected pen.
retval = SelectObject(hPrintDC, hOldPen)
' End information about the first and only page.
retval = EndPage(hPrintDC)
' End information about the document.
retval = EndDoc(hPrintDC)
' The printer will now begin printing the document.
' Delete the pen created for drawing.
retval = DeleteObject(hPen)
' Delete the device context to the printer.
retval = DeleteDC(hPrintDC)
Go back to the alphabetical Function listing.
Go back to the Reference section index.
Last Modified: November 6, 1999
This page is copyright © 1999 Paul Kuliniewicz.
Copyright Information Revised October 29, 2000
Go back to the Windows API Guide home page.
E-mail: vbapi@vbapi.com Send Encrypted E-Mail
This page is at http://www.vbapi.com/ref/c/createdc.html