Image and PictureBox control in the hottest VB

2022-09-28
  • Detail

Some skills of using image and PictureBox controls in VB

some students who are beginners of VB also know some properties and methods of some controls, but when they really want to use them to solve some actual problems of precision screw pair transmission, they can't find an effective way Often, I have a good idea in my mind, but when I implement it with programs, I encounter problems of one kind or another This is really depressing. I also encountered many such problems in the process of learning, so I want to give some tips for students' reference, hoping to ask those fledgling students to take fewer detours

I encountered some difficulties when using image and PictureBox controls. After my classmates' guidance and my own research, I got a little harvest. I want to discuss it with you

I think if you want to use these two controls flexibly, you must understand the characteristics, common properties and methods of these two controls If you are a basic user of VB, it should be said that the following paragraph is necessary. If you are already familiar with these, you can skip it

both image and PictureBox are used to display graphics and can be displayed bmp;. ico;. wmf; . jpg;. GIF and other types of files The picture box can not only display images, but also serve as a container for other controls, so the PictureBox control is more powerful than the image control

some attributes:

(1) picture attribute: used to set the image file name that is important to display in the image box and the picture box Ko is designed to be set by attributes or by calling the loadpicture function at runtime

for example: texture=loadpicture ("d:picg")

(2) align attribute

align attribute is used to set the display mode of the picture box in the form 0 (default) has no special display, 1 is as wide as the form and is at the top of the form; 2 is the same width as the form and is located at the bottom of the form; 3 is as high as the window and is located at the left end of the window; 4 is as high as the form and is located at the right end of the form

(3) the stretch attribute of image

stretch attribute is used to determine how the image frame fits the picture True means that the image will adapt to the size of the image box, false means that the image box will adapt to the size of the image

(4) the autosize attribute of the picture box

autosize attribute is used to determine how the picture box fits into the image False to maintain the original size, when the figure is larger than the picture box, the excess part is truncated. True is that the picture box is automatically adjusted according to the size of the figure, that is to say, the picture box becomes as large as the picture is

next, I will introduce two very simple skills based on these two controls:

(I) how to realize the proportional reduction of too large pictures

some students may ask to reduce a picture by setting the stretch attribute of image to true Let's say so, but think about it carefully. It's impossible to reduce a picture to the same scale Because the size of the image frame cannot be guaranteed to be proportional to the picture So what is the way to solve this problem? It should be solved by moving

in fact, the move method can be used in many controls, even in database controls But I will only introduce his application in this problem The move method can be used in both image controls and PictureBox controls His usage is:

object Move left, top, width, hi. In addition, the AD converter. If the number of bits of the AD converter is low, that is, the resolution, ght

where "object" is the name of the control to be used, here it can be image1 or picturebox1 "Left, top" are the coordinates of the upper left corner of the object in the container, and "width, high" are the width and height of the object Now that you know this method, it's very simple to realize the proportional reduction of the image We can put the picture in an image or PictureBox, and then use the move statement to set the position of the picture and the scale of reduction But it still needs a step. You have to calculate the width and height of the picture to reduce it according to your specified proportion To find the width and height of an image, use the following statement: DTH, ight They can be found Now that it's done, the picture can be reduced The following statements can be used for shrinking:

ve 4320240, Val (DTH)/3, Val (GHT)/3

'4320240 is the upper left coordinate of the picture Reduce the picture by 1/3

(II) how to drag pictures

the above example is actually very simple, but it's not so easy if you can't think of moving The following techniques are also very simple, but MouseDown has to be used to drag pictures. Many key contradictions of mouse's questions have exposed the three events of up and MouseMove Now let's analyze the process of image dragging First of all, you need to drag a picture with the mouse. When you press the left mouse button, this triggers the MouseDown event MouseMove event is triggered when you drag and move the mouse, and mouseup event is triggered when you release the left button after dragging

let me first introduce these three events:

mouseup is used the same as MouseDown

MouseDown (button as integer, shift as integer, X as single, y as single)

mouseup (button as integer, shift as integer, X as single, y as single)

variable button determines which mouse button is pressed

1 - press the left mouse button 2 - the mouse is convenient for users; Right click 4 -- middle mouse button press

variable shift provides the code of shift, Ctrl and ALT when the mouse button is pressed:

no key pressed0shift 1

shift+ctrl3shift+alt 5

alt 4alt+ctrl6

ctrl2shift+ctrl+alt7

x, y: the current position coordinates of the control or form -- horizontal and vertical positions triggered by MouseDown and mouseup events

mousemove usage

mousemove (button as integer, shift as integer, X as single, y as single)

button, the shift parameter is similar to the previous one, and will not be repeated here 10. Y is the horizontal and vertical position of the mouse when MouseMove is triggered Here is a routine to illustrate how to use these three elements to drag pictures:

Copyright © 2011 JIN SHI