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


Finally, we draw the dotted blue ellipse:

Private Sub Command1_Click()
    Dim hPen As Long

    hPen = CreatePen(PS_DOT, 1, RGB(0, 0, 255))
    retval = SelectObject(Picture1.hdc, hPen)
    retval = Ellipse(Picture1.hdc, 10, 10, 160, 70)
End Sub

The result of this code appears in Figure 23.5. Now we can set the color and style in Window API drawing operations.


Figure 23.5  Drawing a dotted blue ellipse with Windows API functions.


TIP:  Besides pens, you can also use brushes to fill figures in when you use a device context. To create a brush, you use the CreateBrush function.

The code for this example is located in the dottedblue folder on this book’s accompanying CD-ROM.

Setting Drawing Modes (ROP2)

Besides setting what color and style to draw with, you can specify how a pen draws in a device context by setting the device context’s binary raster operation, ROP2, mode. For example, if you want to draw using the inverse of the screen color at each point, you can set the ROP2 mode to R2_NOT. The available ROP2 modes appear in Table 23.3—take a look at that table to see what kinds of drawing effects you can create.

Table 23.3 ROP2 modes.
ROP2 Mode Meaning
R2_BLACK (= 1) Pixel is always black
R2_COPYPEN (= 13) Pixel is the pen color
R2_MASKNOTPEN (= 12) Pixel = (NOT pen) AND screen pixel
R2_MASKPEN (=9) Pixel = pen AND screen pixel
R2_MASKPENNOT (=5) Pixel = (NOT screen pixel) AND pen
R2_MERGENOTPEN (=12) Pixel = (NOT pen) OR screen pixel
R2_MERGEPEN (= 15) Pixel = pen OR screen pixel
R2_MERGEPENNOT (= 14) Pixel = (NOT screen pixel) OR pen
R2_NOP (= 11) Pixel remains unchanged
R2_NOT (= 6) Pixel is the inverse of the screen color
R2_NOTCOPYPEN (= 4) Pixel is the inverse of the pen color
R2_NOTMASKPEN (= 8) Pixel = NOT(pen AND screen pixel)
R2_NOTMERGEPEN (= 2) Pixel = NOT(pen OR screen pixel)
R2_NOTXORPEN (= 10) Pixel = NOT(pen XOR screen pixel)
R2_WHITE (= 16) Pixel is always white
R2_XORPEN (= 7) Pixel = (pen XOR screen pixel)

We’ll see more about ROP2 modes in “Capturing Images From The Screen” later in this chapter.

You set the ROP2 mode with the SetROP2 function:

Declare Function SetROP2 Lib "gdi32" Alias "SetROP2" (ByVal         hdc As _
    Long, ByVal nDrawMode As Long) As Long

Here’s what the arguments to that function mean:

  hdc—The device context to set the ROP2 mode in
  nDrawMode—New drawing mode

Handling The Mouse Outside Your Program’s Window

Sometimes you want to work with the mouse no matter where it is on the screen. To do that, you can use the SetCapture and ReleaseCapture functions:

Private Declare Function SetCapture Lib "user32" (ByVal        hwnd As Long) _
    As Long

Private Declare Function ReleaseCapture Lib "user32" () As Long

Here, hwnd is the handle of the window that the mouse should send mouse events to, no matter where it is in the screen. SetCapture captures the mouse, and ReleaseCapture releases it.

Let’s see an example. In this case, we’ll capture the mouse when the user clicks a command button, Command1, and let the user drag the mouse anywhere in the screen—reporting the mouse’s current x and y locations in two text boxes, Text1 and Text2. Set the form’s ScaleMode to Pixels (3) so that we get our mouse locations in pixels.

We’ll use two Boolean flags here: blnStartCapture, which we set to True when the user clicks the command button to capture the mouse, and blnAmCapturing, which we’ll set to True when the user starts dragging the mouse (which means we should start reporting the mouse location in the two text boxes). Along with SetCapture and ReleaseCapture, then, we add all the declarations to the form’s (General) section:

Dim blnStartCapture As Boolean
Dim blnAmCapturing As Boolean

Private Declare Function SetCapture Lib "user32" (ByVal hwnd As Long) _
    As Long
Private Declare Function ClientToScreen Lib "user32" (ByVal hwnd As_
    Long, lpPoint As POINTAPI) As Long
Private Declare Function ReleaseCapture Lib "user32" () As Long

Note also that we include the ClientToScreen function. You use this function to translate coordinates from window coordinates to the whole screen. We’ll need to do that: the mouse coordinates passed to our mouse event handlers are in window coordinates, but we’ll need screen coordinates because we’re using the mouse with the whole screen.

You pass a variable of type POINTAPI to ClientToScreen, so we include that type’s declaration in a new module we add to the program:

Type POINTAPI
    x As Long
    y As Long
End Type

To start the program, we set our Boolean flags to False when the program’s form loads:

Private Sub Form_Load()

    blnStartCapture = False
    blnAmCapturing = False
End Sub

Now when the user clicks the command button, Command1, we capture the mouse with SetCapture and set the blnStartCapture flag to True:

Private Sub Command1_Click()
    blnStartCapture = True
    intRetVal = SetCapture(hwnd)
    Command1.Caption = "Drag the mouse"
End Sub

When the mouse next goes down, we’ll set the blnCapturing flag to True and start reporting the mouse location in the two text boxes, Text1 and Text2:

Private Sub Form_MouseDown(Button As Integer, Shift As Integer, x _
    As Single, y As Single)
    Dim ptPoint As POINTAPI

    If blnStartCapture Then
        ptPoint.x = x
        ptPoint.y = y
        retval = ClientToScreen(hwnd, ptPoint)
        Text1.Text = Str(ptPoint.x)
        Text2.Text = Str(ptPoint.y)
 
        blnAmCapturing = True
    End If
End Sub

Similarly, in the MouseMove event, we check to see if we’re capturing the mouse, and if so, we display the mouse location in the text boxes:

Sub Form_MouseMove(Button As Integer, Shift As Integer, x As Single, _
    y As Single)
    Dim ptPoint As POINTAPI

    If blnAmCapturing Then
        ptPoint.x = x
        ptPoint.y = y
        retval = ClientToScreen(hwnd, ptPoint)
        Text1.Text = Str(ptPoint.x)
        Text2.Text = Str(ptPoint.y)

    End If
End Sub


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.