|
|
![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Visual Basic 6 Black Book
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.
The code for this example is located in the dottedblue folder on this books 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 contexts 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.3take a look at that table to see what kinds of drawing effects you can create.
Well 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
Heres what the arguments to that function mean:
Handling The Mouse Outside Your Programs WindowSometimes 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. Lets see an example. In this case, well capture the mouse when the user clicks a command button, Command1, and let the user drag the mouse anywhere in the screenreporting the mouses current x and y locations in two text boxes, Text1 and Text2. Set the forms ScaleMode to Pixels (3) so that we get our mouse locations in pixels. Well 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 well 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 forms (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. Well need to do that: the mouse coordinates passed to our mouse event handlers are in window coordinates, but well need screen coordinates because were using the mouse with the whole screen. You pass a variable of type POINTAPI to ClientToScreen, so we include that types 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 programs 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, well 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 were 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
|
|
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. |