|
|
![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Visual Basic 6 Black Book
Thats it for the overview of connecting to Windows and Visual C++. Weve seen how the process works in overview; now its time to turn to our Immediate Solutions. Immediate SolutionsGetting 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
Heres the parameter for GetDC:
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:
This function, CreateDC, may seem a little abstract, but it has one very powerful useyou 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).
Drawing Lines In A Device ContextNow 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
Heres what the arguments to MoveToEx mean:
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
Heres what the arguments mean:
Both MoveToEx and LineTo return non-zero values if successful. Lets see an example. Here, well 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 forms (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 were drawing lines with the Windows API.
|
|
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. |