|
|
![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Visual Basic 6 Black Book
Accessing Individual Pixels In A Picture BoxThe Testing Department is calling. Wouldnt 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. Lets see an example to make this clear. Here, well 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, well 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.
Copying Pictures To And Pasting Pictures From The ClipboardThe 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 in the Clipboard, you use SetData(), and to retrieve data from the Clipboard, you use GetData(). An example will make this clearer. Here, well paste a picture from Picture1 to Picture2 using two buttons: Command1 and Command2. When users click Command1, well copy the picture from Picture1 to the Clipboard; when they click Command2, well 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:
If you omit the format parameter, Visual Basic will determine the correct format, so well 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 dont 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
Thats 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 were using the Clipboard with picture boxes.
Stretching And Flipping Images In A Picture BoxYou 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. Heres what the arguments passed to PaintPicture mean:
|
|
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. |