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


Drawing Ellipses In A Device Context

You 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

Here’s what the arguments mean:

  hdc—The device context to draw in
  X1—The x-coordinate of the upper-left corner of the ellipse’s bounding rectangle
  Y1—The y-coordinate of the upper-left corner of the ellipse’s bounding rectangle
  X2—The x-coordinate of the lower-right corner of the ellipse’s bounding rectangle
  Y2—The y-coordinate of the lower-right corner of the ellipse’s bounding rectangle

Let’s see an example. Here, we can draw an ellipse in a picture box’s 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 we’re drawing ellipses with the Windows API functions.


Figure 23.3  Drawing ellipses with the Windows API.

Drawing Rectangles In A Device Context

You 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

Here’s what the arguments in this function mean:

  hdc—The device context to draw in
  X1—The x-coordinate of the upper-left corner of the rectangle
  Y1—The y-coordinate of the upper-left corner of the rectangle
  X2—The x-coordinate of the lower-right corner of the rectangle
  Y2—The y-coordinate of the lower-right corner of the rectangle

Let’s see an example. Here, we’ll 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 we’re 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 we’re drawing rectangles with the Windows API functions.


Figure 23.4  Drawing rectangles with the Windows API.

Setting Drawing Colors And Styles (Using Pens)

The Aesthetic Design Department is on the phone. The figures you’re 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 it’s 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

Here’s what the arguments to this function mean:

  nPenStyle—The style for the pen. Here are the possibilities: PS_SOLID (0), PS_DASH (1), PS_DOT (2), PS_DASHDOT (3), PS_DASHDOTDOT (4), PS_NUL L (5), PS_INSIDEFRAME (6), PS_USERSTYLE (7), PS_ALTERNATE (8), and PS_STYLE_MASK (&HF).
  nWidth—Specifies the width of the pen. Note that if this is not 1, the pen will draw solid lines only.
  crColor—An RGB color for the pen.

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

Here’s what the arguments to that function mean:

  hdc—The device context to install the object in
  hObject—The handle to the object to install

This function returns the handle of the device context object you’re replacing. Let’s see an example in which we’ll draw that dotted blue ellipse in a picture box, Picture1, when the user clicks a command button, Command1. We add the declarations we’ll need in the program in the form’s (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 pen’s 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)
...


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.