Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Visual Basic 6 Black Book
(Publisher: The Coriolis Group)
Author(s): Steven Holzner
ISBN: 1576102831
Publication Date: 08/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


That’s it for the overview of connecting to Windows and Visual C++. We’ve seen how the process works in overview; now it’s time to turn to our Immediate Solutions.

Immediate Solutions

Getting Or Creating A Device Context (Including The Whole Screen)

Before you can draw with the Windows API graphics functions, you need a device context. Visual Basic controls like picture boxes have an hDC property, which holds their device context, but you can also get device contexts for any window.

To get a device context for a window, you can use GetDC, which returns a handle to a device context, or 0 if unsuccessful:

Declare Function GetDC Lib "user32" Alias "GetDC" (ByVal        hwnd As Long)_
    As Long

Here’s the parameter for GetDC:

  hwnd—Handle to the window you want a device context for.

You can also get a device context for a particular device with the CreateDC function, which returns a handle to a device context, or 0 if unsuccessful:

Declare Function CreateDC Lib "gdi32" Alias "CreateDCA" (ByVal _
    lpszDriverName As String, ByVal       lpszDeviceName As String, ByVal    _
    lpszOutput As String,    lpInitData As DEVMODE) As Long

Here are the parameters for CreateDC:

  lpszDriverName— String that specifies the file name (without extension) of the device driver (for example, “EPSON”).
  lpszDeviceName—String that specifies the name of the specific device to be supported (for example, “EPSON LQ-80”). The lpszDeviceName parameter is used if the module supports more than one device.
  lpszOutput—String that specifies the file or device name for the physical output medium (file or output port).
  lpInitData—A DEVMODE structure containing device-specific initialization data for the device driver. The Windows DocumentProperties function retrieves this structure filled in for a given device. The lpInitData parameter must be NULL if the device driver is to use the default initialization (if any) specified by the user through the Control Panel.

This function, CreateDC, may seem a little abstract, but it has one very powerful use—you can get a device context for the entire screen using this function, which means you can then use Windows functions to draw anywhere in the screen. To get a device context for the screen, you declare the DEVMODE type, as declared in win32api.txt, in a module:

Type DEVMODE
        dmDeviceName As String * CCHDEVICENAME
        dmSpecVersion As Integer
        dmDriverVersion As Integer
        dmSize As Integer
        dmDriverExtra As Integer
        dmFields As Long
        dmOrientation As Integer
        dmPaperSize As Integer
        dmPaperLength As Integer
        dmPaperWidth As Integer
        dmScale As Integer
        dmCopies As Integer
        dmDefaultSource As Integer
        dmPrintQuality As Integer
        dmColor As Integer
        dmDuplex As Integer
        dmYResolution As Integer
        dmTTOption As Integer
        dmCollate As Integer
        dmFormName As String * CCHFORMNAME
        dmUnusedPadding As Integer
        dmBitsPerPel As Integer
        dmPelsWidth As Long
        dmPelsHeight As Long
        dmDisplayFlags As Long
        dmDisplayFrequency As Long
End Type

Next, you pass the string “DISPLAY” as the device type to CreateDC to get a device context for the entire screen, as we do here when a form loads:

Private Sub Form_Load()
    Dim devDevMode As DEVMODE
    Dim intHandleDisplay As Integer

    intHandleDisplay = CreateDC("DISPLAY", 0&, 0&, devDevMode)
End Sub

Now you can draw anywhere in the screen, using the screen device context and the Windows API drawing functions (see the next topics in this chapter).


TIP:  When you’re done with a device context, you can delete it and reclaim its memory with the DeleteDC function.

Drawing Lines In A Device Context

Now that you have a device context (see the previous topic), how do you draw in it? There are many, many drawing functions in the Windows API. For example, you can use the LineTo function to draw lines. This function draws a line from the current drawing position to the position you specify. You set the current drawing position with the MoveToEx function:

Declare Function MoveToEx Lib "gdi32" Alias "MoveToEx" (ByVal     hdc As _
    Long, ByVal    x As Long, ByVal     y As Long,   lpPoint As POINTAPI) As Long

Here’s what the arguments to MoveToEx mean:

  hdc—The device context to draw in
  x—The x-coordinate of the new position
  y—The y-coordinate of the new position
  lpPointPOINTAPI variable which will be filled with the old location

After setting the current drawing position, you use LineTo to draw a line to a new position:

Declare Function LineTo Lib "gdi32" (ByVal     hdc As Long, ByVal    x As _
    Long, ByVal    y As Long) As Long

Here’s what the arguments mean:

  hdc—The device context to draw in
  x—The x-coordinate of the end of the line
  y—The y-coordinate of the end of the line

Both MoveToEx and LineTo return non-zero values if successful.

Let’s see an example. Here, we’ll draw a line in a picture box. We start by adding a module to a program and declaring the POINTAPI type that MoveToEx needs:

Type POINTAPI
    x As Long
    y As Long
End Type

Next, we declare MoveToEx and LineTo as Private in the form’s (General) section:

Private Declare Function MoveToEx Lib "gdi32" (ByVal hdc As Long, _
    ByVal x As Long, ByVal y As Long, lpPoint As POINTAPI) As Long

Private Declare Function LineTo Lib "gdi32" (ByVal hdc As Long, _
    ByVal x As Long, ByVal y As Long) As Long

Add a picture box to the form now, Picture1, and a command button, Command1 ; when the user clicks the command button, we can draw a line:

Private Sub Command1_Click()
    Dim ptPoint As POINTAPI

    retval = MoveToEx(Picture1.hdc, 20, 20, ptPoint)
    retval = LineTo(Picture1.hdc, 100, 50)
End Sub

The result of this code appears in Figure 23.2. Now we’re drawing lines with the Windows API.


Figure 23.2  Drawing lines with the Windows API.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited.