|
|
![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Visual Basic 6 Black Book
Drawing Ellipses In A Device ContextYou draw ellipses in the Windows API with the Ellipse function:
Declare Function Ellipse Lib "gdi32" Alias "Ellipse" (ByVal hdc As _
Long, ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal _
Y2 As Long) As Long
Heres what the arguments mean:
Lets see an example. Here, we can draw an ellipse in a picture boxs device context by declaring Ellipse as Private in a form:
Private Declare Function Ellipse Lib "gdi32" Alias "Ellipse" (ByVal hdc _
As Long, ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, _
ByVal Y2 As Long) As Long
Then we draw the ellipse in a picture box, Picture1, when the user clicks a command button, Command1:
Private Sub Command1_Click()
retval = Ellipse(Picture1.hdc, 10, 10, 160, 70)
End Sub
The result of this code appears in Figure 23.3. Now were drawing ellipses with the Windows API functions.
Drawing Rectangles In A Device ContextYou draw rectangles in the Windows API with the Rectangle function:
Public Declare Function Rectangle Lib "gdi32" Alias "Rectangle" (ByVal _
hdc As Long, ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, _
ByVal Y2 As Long) As Long
Heres what the arguments in this function mean:
Lets see an example. Here, well draw a rectangle in a picture box, Picture1, when the user clicks a command button, Command1. We declare Rectangle in the form as Private:
Private Declare Function Rectangle Lib "gdi32" (ByVal hdc As Long, _
ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As _
Long) As Long
Then were free to draw our rectangle, like this:
Private Sub Command1_Click()
retval = Rectangle(Picture1.hdc, 10, 10, 160, 70)
End Sub
The result of this code appears in Figure 23.4. Now were drawing rectangles with the Windows API functions.
Setting Drawing Colors And Styles (Using Pens)The Aesthetic Design Department is on the phone. The figures youre drawing with the Windows API are nice, but what they really wanted was a dotted blue ellipse. Can you create one? Yes, you can, using device context pens. A pen sets the drawing color and style for the device context its loaded in. You create a pen with CreatePen:
Declare Function CreatePen Lib "gdi32" Alias "CreatePen" (ByVal_
nPenStyle As Long, ByVal nWidth As Long, ByVal crColor As Long) _
As Long
Heres what the arguments to this function mean:
To install the new pen in a device context, you use the SelectObject function, which you use to install graphics objects like pens and brushes in device context:
Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, _
ByVal hObject As Long) As Long
Heres what the arguments to that function mean:
This function returns the handle of the device context object youre replacing. Lets see an example in which well draw that dotted blue ellipse in a picture box, Picture1, when the user clicks a command button, Command1. We add the declarations well need in the program in the forms (General) section:
Private Declare Function CreatePen Lib "gdi32" (ByVal nPenStyle As Long, _
ByVal nWidth As Long, ByVal crColor As Long) As Long
Private Const PS_SOLID = 0
Private Const PS_DASH = 1
Private Const PS_DOT = 2
Private Const PS_DASHDOT = 3
Private Const PS_DASHDOTDOT = 4
Private Const PS_NULL = 5
Private Const PS_INSIDEFRAME = 6
Private Const PS_USERSTYLE = 7
Private Const PS_ALTERNATE = 8
Private Const PS_STYLE_MASK = &HF
Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, _
ByVal hObject As Long) As Long
Private Declare Function Ellipse Lib "gdi32" (ByVal hdc As Long, _
ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As _
Long) As Long
Next, we create the new pen when the user clicks the command button Command1, saving the pens handle as hPen. Note that we specify the pen should be dotted by passing the PS_DOT constant, and we can use the Visual Basic RGB function to set colors:
Private Sub Command1_Click()
Dim hPen As Long
hPen = CreatePen(PS_DOT, 1, RGB(0, 0, 255))
...
Next, we install that pen in the device context of the picture box, Picture1, using SelectObject:
Private Sub Command1_Click()
Dim hPen As Long
hPen = CreatePen(PS_DOT, 1, RGB(0, 0, 255))
retval = SelectObject(Picture1.hdc, hPen)
...
|
|
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. |