![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
To access the contents, click the chapter and section titles.
Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6!
Yes, I know that this sounds complicated. Lets take a look at the syntax of the PaintPicture method object.PaintPicture source, x1, y1, w1, h1, x2, y2, w2, h2, opcode where the arguments are as follows:
Think of the source picture as being printed on a rubber sheet. You can cut out any rectangular part of the picture you want, stretch it vertically and/or horizontally, and stick it down anywhere on the destination picture. Sound useful? You bet it is. The operation of PaintPicture is illustrated in Figure 12.6. The opcode argument can be a bit confusingwith good reason. How many ways can you copy a picture from one location to another? Well, you would be surpriseddozens, at least. Heres how it works: In the destination object, where the copied picture is going to be placed, an image already exists. A bitmap picture may be displayed there already, but even if the destination is blank, something is thereperhaps just white pixels. Over the extent of the copied region, therefore, each pixel in the copied image has a corresponding pixel at the same location in the destination. How will these pixels be combined? This is what the opcode argument determines.
The simplestand perhaps most commonoperation is to have each new pixel replace the original pixel. The opcode argument for this is the predefined constant vbSrcCopy. Two other useful opcode arguments are vbBlackness and vbWhiteness, which turn the destination rectangle all black or all white. This really isnt a copy of the source picture, of course; you still have to specify a source in the PaintPicture statement. Other opcode arguments perform a variety of logical combinations of the source picture with the destination picture. You can use them to create certain types of visual effects, but I wont go into them here. You can explore the topic further in the Visual Basic Help system.
The best way to get a feel for how PaintPicture works is to see it in action. The example program PAINTPIC demonstrates three common uses for this statement: enlarging a picture, shrinking a picture, and painting areas with black or white. To start the project, place two Picture Box controls on a form. For the first one (Picture1), set the AutoSize property to True, then load the picture GUARD.JPG (provided on the companion CD-ROM) into its Picture property. All of the properties for Picture2 should be left at their default values. Change the forms Caption property to Picture Magnifier. The program requires three constants, placed in the general declarations section of the forms code. Two of the constants specify the vertical and horizontal magnification, expressed as the portion of the original picture that will be blown up. I used 0.3 for both the vertical and horizontal values; you can experiment with other values if you like. The third constant is the opcode vbWhiteness. Unfortunately, this is not predefined by Visual Basicwe have do it ourselves. The declarations code is shown here: Option Explicit Const vbWhiteness = &HFF0062 Const WIDE = 0.3 Const HIGH = 0.3 Adjusting the form and control sizes is accomplished in the Form_Load event procedure. Picture1 will already be the size of the loaded picture, because we set its AutoSize property to True. The following code makes the form size a bit taller than and a bit more than twice as wide as this Picture Box. Then it makes Picture2 the same size as Picture1 and positions them both on the form. The code is shown in Listing 12.4. Listing 12.4 The Picture Magnifiers Form_Load procedure. Private Sub Form_Load() Form1.Width = 2.1 * Picture1.Width Form1.Height = 1.1 * Picture1.Height Picture1.Left = 0 Picture1.Top = 0 Picture2.Width = Picture1.Width Picture2.Height = Picture1.Height Picture2.Top = 0 Picture2.Left = Form1.ScaleWidth - Picture2.Width End Sub The real action will be triggered by the user clicking on the source picture. A left-button click will trigger the display (in the second Picture Box) of a magnified portion of the picture centered on the location where the mouse was clicked. A right-button click will display nine small copies of the entire picture, each surrounded by a black and white border. Lets deal with detecting the click first. While the Click event procedure might seem like the way to go, a quick examination reveals that it provides no way to tell which button was clicked or where the mouse pointer was located. Clearly, we need something else. Scanning through the list of events that the Picture Box control supports, you will notice MouseDown and MouseUp events. The MouseUp event procedure declaration looks like this: Sub Picture1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
|
![]() |
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. Read EarthWeb's privacy statement. |