1. Trang chủ
  2. » Công Nghệ Thông Tin

Visual Basic 6 Black Book phần 4 docx

112 353 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 112
Dung lượng 3,37 MB

Nội dung

Accessing Individual Pixels In A Picture Box The Testing Department is calling. Wouldnt it be better to let users select new colors in your SuperDuperTextPro program by just clicking the new color they want in a picture box instead of asking them to type in new color values? Hmm, you think, how do you do that? You can use the Point method to determine the color of a pixel in a picture box. This method returns the red, green, and blue colors in one Long integer. Lets see an example to make this clear. Here, well let the user click one picture box, Picture1, to set the color in another, Picture2, using the MouseDown event: Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, _ X As Single, Y As Single) End Sub When the user clicks a pixel in Picture1, well set the background color of Picture2 to the same color, and we get that color using the Point method: Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, _ X As Single, Y As Single) Picture2.BackColor = Picture1.Point(X, Y) End Sub The result of this code appears in Figure 10.12. When the user clicks a point in the top picture box, the program sets the background color of the bottom picture box to the same color. Figure 10.12 Using the Point method to get a points color. TIP: Besides getting a pixel with the Point method, you can also set individual pixels with the PSet method. See Drawing Lines And Circles In A Picture Box earlier in this chapter. Copying Pictures To And Pasting Pictures From The Clipboard The users love your new graphics program, SuperDuperGraphics4U, but would like to export the images they create to other programs. How can you do that? You can copy the images to the Clipboard, letting the user paste them into other programs. To place data Visual Basic 6 Black Book:Picture Boxes And Image Controls http://24.19.55.56:8080/temp/ch10\331-334.html (1 of 3) [3/14/2001 1:42:12 AM] Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com in the Clipboard, you use SetData(), and to retrieve data from the Clipboard, you use GetData(). An example will make this clearer. Here, well paste a picture from Picture1 to Picture2 using two buttons: Command1 and Command2. When users click Command1, well copy the picture from Picture1 to the Clipboard; when they click Command2, well paste the picture to Picture2. To place the image in Picture1 into the Clipboard, we use SetData(): Clipboard.SetData data, [ format] Here are the possible values for the format parameter for images: " vbCFBitmap2; bitmap (.bmp) file " vbCFMetafile3; metafile (.wmf) file " vbCFDIB8; device-independent bitmap (.dib) file " vbCFPalette9; color palette If you omit the format parameter, Visual Basic will determine the correct format, so well just copy the picture from Picture1.Picture to the Clipboard this way: Private Sub Command1_Click() Clipboard.SetData Picture1.Picture End Sub To paste the picture, use GetData(): Clipboard.GetData ([ format]) The format parameter here is the same as for SetData(), and as before, if you dont specify the format, Visual Basic will determine it. So when the user clicks the second button, we paste the image into Picture2 this way: Private Sub Command2_Click() Picture2.Picture = Clipboard.GetData() End Sub Thats all it takes. When you run the program and click the Copy and then the Paste button, the image is copied to the Clipboard and then pasted into the second picture box, as shown in Figure 10.13. The program is a success. Now were using the Clipboard with picture boxes. Figure 10.13 Copying a picture to and pasting it from the Clipboard. Visual Basic 6 Black Book:Picture Boxes And Image Controls http://24.19.55.56:8080/temp/ch10\331-334.html (2 of 3) [3/14/2001 1:42:12 AM] Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Stretching And Flipping Images In A Picture Box You can gain a lot more control over how images are displayed in picture boxes using the PaintPicture method: PictureBox.PaintPicture picture, x1, y1, [width1, height1, [x2, y2, _ [width2, height2, [opcode]]]] Using this method, you can stretch or flip images in a picture box. Heres what the arguments passed to PaintPicture mean: " picture The source of the graphic to be drawn onto the object; should be a Picture property. " 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. 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. " opcode Long 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. Visual Basic 6 Black Book:Picture Boxes And Image Controls http://24.19.55.56:8080/temp/ch10\331-334.html (3 of 3) [3/14/2001 1:42:12 AM] Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com 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. Visual Basic 6 Black Book:Picture Boxes And Image Controls http://24.19.55.56:8080/temp/ch10\334-337.html (1 of 4) [3/14/2001 1:42:23 AM] Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com " 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. " opcode Long 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 Visual Basic 6 Black Book:Picture Boxes And Image Controls http://24.19.55.56:8080/temp/ch10\334-337.html (2 of 4) [3/14/2001 1:42:23 AM] Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com 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 Visual Basic 6 Black Book:Picture Boxes And Image Controls http://24.19.55.56:8080/temp/ch10\334-337.html (3 of 4) [3/14/2001 1:42:23 AM] Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Visual Basic 6 Black Book:Picture Boxes And Image Controls http://24.19.55.56:8080/temp/ch10\334-337.html (4 of 4) [3/14/2001 1:42:23 AM] Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Then we could use pixel dimensions in the MouseDown event: Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As _ Single, Y As Single) If X > 16 And X < 83 And Y > 11 And Y < 36 Then MsgBox "You clicked the word ""Picture""" End If If X > 83 And X < 125 And Y > 11 And Y < 36 Then MsgBox "You clicked the word ""Box""" End If End Sub If you set the scale mode to vbUser, you can define your own units by setting the dimensions of the picture box using the ScaleLeft, ScaleTop, ScaleWidth, and ScaleHeight properties. This can be very useful if you re plotting points and want to use a picture box as a graph. TIP: The ScaleWidth and ScaleHeight properties of a picture box hold the images actual dimensions (in units determined by the ScaleMode property), not the Width and Height properties, which hold the controls width and height (including the border). Saving Pictures To Disk We already know you can load pictures into a picture box with the LoadPicture function. Can you save them to disk? Yes, you can, using SavePicture. Heres how that statement works: SavePicture picture, stringexpression Heres what the parameters for SavePicture mean: " picturePicture or image control from which the graphics file is to be created " stringexpressionFile name of the graphics file to save SavePicture only saves images in BMP, WMF, and ICO formats (depending on the file type the image came from originally); if the image came from a GIF or JPEG file, its saved in BMP format. Graphics in an Image property are always saved as bitmap (.bmp) files no matter what their original format. Heres an example where we save the image from Picture1 to a file, C:\image.bmp, when the user clicks a button: Visual Basic 6 Black Book:Picture Boxes And Image Controls http://24.19.55.56:8080/temp/ch10\337-340.html (1 of 3) [3/14/2001 1:42:34 AM] Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Private Sub Command1_Click() SavePicture Picture1.Picture, "c:\image.bmp" End Sub Adding An Image Control To A Form Youve got 200 picture boxes in your program, and suddenly the Testing Department is on the line: your program is causing users computers to run out of memory. No problem here, you say. They say, thats because not everyone has 128MB of RAM like you doits time to decrease your programs memory consumption. One way of using fewer system resources is to use fewer picture boxes. As weve seen in this chapter, picture boxes are powerful controlsand with that power comes lots of overhead. If youre just going to be displaying images, use image controls instead. The image control uses fewer system resources and repaints faster than a picture box (however, it supports only a subset of the picture box properties, events, and methods). To install an image control, just use the Image Control tool in the toolbox. After adding the image control to your form, just set its Picture property to the image file you want to display. By default, image controls shape themselves to the image you display; if you want to stretch the image to fit the image control and not the other way around, set the image controls Stretch property to True (the default is False). As an example, weve placed an (unstretched) image in the image control in Figure 10.15. Figure 10.15 Using an image control. Stretching An Image In An Image Control You can stretch (or flip) an image in a picture box using the PaintPicture method, but you cant use PaintPicture with image controls. Is there still some way of producing interesting graphics effects in an image control? You can use the image controls Stretch property. By default, image controls shape themselves to fit the images inside them (after all, their primary purpose is to display images), but if you set the Stretch property to True (the default is False), the image control will stretch the image to fit the control. As an example, were stretching an image in the image control in Figure 10.16. Figure 10.16 Stretching an image in an image control. You can also stretch an image in an image control by resizing the control (using its Width and Height properties) at runtime as long as the controls Stretch property is True. Visual Basic 6 Black Book:Picture Boxes And Image Controls http://24.19.55.56:8080/temp/ch10\337-340.html (2 of 3) [3/14/2001 1:42:34 AM] Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Visual Basic 6 Black Book:Picture Boxes And Image Controls http://24.19.55.56:8080/temp/ch10\337-340.html (3 of 3) [3/14/2001 1:42:34 AM] Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com [...]... and all files (*.*) http:// 24. 19.55. 56: 8080/temp/ch11\3 54- 358.html (3 of 4) [3/ 14/ 2001 1 :43 :23 AM] Visual Basic 6 Black Book: Windows Common Dialogs Figure 11.8 Setting file extension types in dialog boxes Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com http:// 24. 19.55. 56: 8080/temp/ch11\3 54- 358.html (4 of 4) [3/ 14/ 2001 1 :43 :23 AM] Visual Basic 6 Black Book: Windows Common Dialogs... produces the same result http:// 24. 19.55. 56: 8080/temp/ch11\350-353.html (3 of 4) [3/ 14/ 2001 1 :43 :18 AM] Visual Basic 6 Black Book: Windows Common Dialogs Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com http:// 24. 19.55. 56: 8080/temp/ch11\350-353.html (4 of 4) [3/ 14/ 2001 1 :43 :18 AM] Visual Basic 6 Black Book: Windows Common Dialogs Simpo PDF Merge and Split Unregistered Version -... action " 1Displays the Open dialog box " 2Displays the Save As dialog box " 3Displays the Color dialog box " 4Displays the Font dialog box " 5Displays the Print dialog box " 6Runs winhelp32.exe http:// 24. 19.55. 56: 8080/temp/ch11\ 341 -3 46 . html (4 of 4) [3/ 14/ 2001 1 :42 :59 AM] Visual Basic 6 Black Book: Windows Common Dialogs Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Now that... Print Setting Page Orientation Showing Windows Help From A Visual Basic Program In Depth In this chapter, were going to examine the Windows Common Dialogs, which provide a powerful and professional set of dialog boxes for interacting with the user http:// 24. 19.55. 56: 8080/temp/ch11\ 341 -3 46 . html (1 of 4) [3/ 14/ 2001 1 :42 :59 AM] Visual Basic 6 Black Book: Windows Common Dialogs Microsoft created the Common... = 24 Finally, we show the Font dialog box, and then make use of the newly set font size: Private Sub Command1_Click() On Error GoTo Cancel CommonDialog1.Flags = cdlCFBoth Or cdlCFLimitSize CommonDialog1.Min = 12 CommonDialog1.Max = 24 CommonDialog1.ShowFont Text1.FontName = CommonDialog1.FontSize http:// 24. 19.55. 56: 8080/temp/ch11\ 361 - 365 .html (1 of 4) [3/ 14/ 2001 1 :43 :49 AM] Visual Basic 6 Black Book: Windows... to print http:// 24. 19.55. 56: 8080/temp/ch11\ 361 - 365 .html (2 of 4) [3/ 14/ 2001 1 :43 :49 AM] Visual Basic 6 Black Book: Windows Common Dialogs " FromPageThe page to start printing Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com " ToPageThe page to stop printing " hDCThe device context for the selected printer Lets see an example In this case, well use the Visual Basic PrintForm method... intLoopIndex Cancel: http:// 24. 19.55. 56: 8080/temp/ch11\ 361 - 365 .html (3 of 4) [3/ 14/ 2001 1 :43 :49 AM] Visual Basic 6 Black Book: Windows Common Dialogs End Sub Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Thats itwhen the user clicks Command1, the program displays the Print dialog box; the user can set the number of copies to print and when they click on OK, Visual Basic displays a dialog... and the print job starts Our Print dialog box example is a successthe code for this program is located in the printerdialog folder on this books accompanying CD-ROM http:// 24. 19.55. 56: 8080/temp/ch11\ 361 - 365 .html (4 of 4) [3/ 14/ 2001 1 :43 :49 AM] Visual Basic 6 Black Book: Windows Common Dialogs Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Setting Print Dialog Flags You can set... example, when the user clicks Cancel in a File Open dialog box, the FileName property returns an empty string, However, Visual Basic provides a more systematic way of checking which button was http:// 24. 19.55. 56: 8080/temp/ch11\3 46 - 350.html (1 of 4) [3/ 14/ 2001 1 :43 :08 AM] Visual Basic 6 Black Book: Windows Common Dialogs clicked You can set the Common Dialog CancelError property to True to create a special,Simpo... the button, the Color dialog box appears, as in Figure 11 .4 Figure 11 .4 The Color dialog box When the user selects a color and clicks on OK, the program sets the text boxs background color to the newly selected color, as shown in Figure 11.5 http:// 24. 19.55. 56: 8080/temp/ch11\3 46 - 350.html (3 of 4) [3/ 14/ 2001 1 :43 :08 AM] Visual Basic 6 Black Book: Windows Common Dialogs Figure 11.5 Setting a controls . boxes for interacting with the user. Visual Basic 6 Black Book: Windows Common Dialogs http:// 24. 19.55. 56: 8080/temp/ch11 341 -3 46 . html (1 of 4) [3/ 14/ 2001 1 :42 :59 AM] Simpo PDF Merge and Split Unregistered. command in the displayed context menu. Visual Basic 6 Black Book: Windows Common Dialogs http:// 24. 19.55. 56: 8080/temp/ch11 341 -3 46 . html (2 of 4) [3/ 14/ 2001 1 :42 :59 AM] Simpo PDF Merge and Split Unregistered. CommonDialog1.ShowColor): " ShowOpenShow Open dialog box Visual Basic 6 Black Book: Windows Common Dialogs http:// 24. 19.55. 56: 8080/temp/ch11 341 -3 46 . html (3 of 4) [3/ 14/ 2001 1 :42 :59 AM] Simpo PDF Merge and Split Unregistered

Ngày đăng: 14/08/2014, 01:20

TỪ KHÓA LIÊN QUAN

w