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


You can flip a bitmap horizontally or vertically by using negative values for the destination height (height1) and/or the destination width (width1). For example, here’s how we flip the image in Picture1 horizontally and display it in Picture2 (keep in mind that to draw from the Form_Load event, you have to set the form’s AutoRedraw property to True):

Private Sub Form_Load()
    Picture2.PaintPicture Picture1.Picture, Picture1.ScaleWidth, 0, _
        -1 * Picture1.ScaleWidth, Picture1.ScaleHeight
    Picture2.Height = Picture1.Height
End Sub

The results of the preceding code appear in Figure 10.14. Now we’re flipping images in picture boxes.


Figure 10.14  Flipping an image in a picture box.

Printing A Picture

Can you print the image in a picture box out on the printer? You sure can, using the PaintPicture method. To print on the printer, you just use the Visual Basic Printer object this way with PaintPicture:

Printer.PaintPicture picture, x1, y1, [width1, height1, [x2, y2, _
    [width2, height2, [opcode]]]]

Here’s what the arguments passed to PaintPicture mean:

  picture—The source of the graphic to be drawn onto the object (for example, Picture1.Picture).
  x1, y1—Single-precision values indicating the destination coordinates (x-axis and y-axis) on the object for the picture to be drawn. The ScaleMode property of the object determines the unit of measure used.
  width1—Single-precision value indicating the destination width of the picture. The ScaleMode property of the object determines the unit of measure used. If the destination width is larger or smaller than the source width (width2), the picture is stretched or compressed to fit. If omitted, the source width is used.
  height1—Single-precision value indicating the destination height of the picture. The ScaleMode property of the object determines the unit of measure used. If the destination height is larger or smaller than the source height (height2), the picture is stretched or compressed to fit. If omitted, the source height is used.
  x2, y2—Single-precision values indicating the coordinates (x-axis and y-axis) of a clipping region within the picture (drawing operations outside the clipping region are ignored). The ScaleMode property of the object determines the unit of measure used. If omitted, 0 is assumed.
  width2—Single-precision value indicating the source width of a clipping region within the picture. The ScaleMode property of the object determines the unit of measure used. If omitted, the entire source width is used.
  height2—Single-precision value indicating the source height of a clipping region within the picture. The ScaleMode property of the object determines the unit of measure used. If omitted, the entire source height is used.
  opcodeLong value or code that is used only with bitmaps. It defines a bit-wise operation (such as vbMergeCopy) that is performed on the picture as it is drawn on the object.

For example, here’s how to print the picture in Picture1 on the printer:

Private Sub Command1_Click()
    Printer.PaintPicture Picture1.Picture, 0, 0
End Sub

That’s all there is to it—the PaintPicture method is extraordinarily powerful. Note that before printing a picture, you may want to display a Print dialog box (see the next chapter).

Using Picture Box Handles

You can gain even more control over what’s going on in a picture box by using the various Windows handles available for that control together with direct Windows API calls. Here are the picture box handle properties:

  hDC—Handle to the picture box’s device context
  hWnd—Handle to the picture box’s window
  Image—Handle to the picture box’s bitmap
  Handle—Different handle types depending on the picture’s Type property (for example, Picture1.Picture.Type) as follows:
  Type = 1—An HBITMAP handle
  Type = 2—An HMETAFILE handle
  Type = 3—An HICON or an HCURSOR handle
  Type = 4—An HENHMETAFILE handle

For example, here we use the hDC property of a picture box to create a compatible bitmap and device context matching the picture box, using the Windows API functions CreateCompatibleDC() and CreateCompatibleBitmap() (these and all Windows API functions must also be declared in the program, as we’ll see in Chapter 23):

Private Sub Form_Load()
    Picture1.Picture = LoadPicture("image.bmp")

    Dim dcMemory As Long
    Dim hMemoryBitmap As Long
    dcMemory = CreateCompatibleDC(Picture1.hdc)
    hMemoryBitmap = CreateCompatibleBitmap(Picture1.hdc, 60, 30)
End Sub

Setting Measurement Scales In A Picture Box

Picture boxes have a number of scale properties, and perhaps the most popular one is ScaleMode, which sets the units of measurement in a picture box. Here are the possible values for ScaleMode (note that when you set the scale mode of a picture box, all measurements are in those new units, including coordinates passed to your program, like mouse-down locations):

  vbUser—0; indicates that one or more of the ScaleHeight, ScaleWidth, ScaleLeft, and ScaleTop properties are set to custom values
  vbTwips—1(the default); Twip (1440 twips per logical inch; 567 twips per logical centimeter)
  vbPoints—2; point (72 points per logical inch)
  vbPixels—3; pixel (smallest unit of monitor or printer resolution)
  vbCharacters—4; character (horizontal equals 120 twips per unit; vertical equals 240 twips per unit)
  vbInches—5; inch
  vbMillimeters—6; millimeter
  vbCentimeters—7; centimeter
  vbHimetric—8; hiMetric
  vbContainerPosition—9; units used by the control’s container to determine the control’s position
  vbContainerSize—10; units used by the control’s container to determine the control’s size

For example, in our image map example, we set the scale mode to pixels:

Private Sub Form_Load()
    Picture1.ScaleMode = vbPixels
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.