
Get position of image after stretching it
Quote:
> Im loading a picture and setting it as a stdPicture Object and displaying it
> as a background picture on the Form. This picture is then stretched using
> Paintpicture when the Form is resized (Im trying to make this app adaptable
> for most screen resolutions). Several controls are placed on this Form and
> they are placed in particular positions that fit with the background
> picture. For example, think of a coloured rectangle in this background
> picture. A picture control exactly the same size as this rectangle is then
> positioned and placed on top of that rectangle. The question: How can I
> determine the exact size and position of that rectangle when the Form has
> been resized/stretched since its position and size will change when the Form
> is resized. I cant use getpixel because there are other coloured rectangles
> on the Form. Ive tried holding the oldsize and oldXYpositions of the
> rectangle and simply adding or subtracrting the amount of Form resize but it
> doesnt seem to work out that way. Any pointers on how to approach this.
One simple way of doing that is to adjust the scale of the form so that it
it always matches the original image size. Then you can place your controls
where they belong with respect to the image, and because the scale changes
during resize, re-setting those controls to their original position puts them in
the correct place on the form.
A bit harder to explain, perhaps an example will help....
Add a picturebox and a command button to a new form and paste in
the code below. Note that as you resize the form, the command button
remains inside the drawn box. Just as the drawn image is stretched and
shrunk as you change the form size, the command button follows suit
because the form's scale has been changed. Note however that the
command button is set to the exact same size as it was in Form_Load.
Its the changing scale that makes up the difference....
HTH
LFS
Option Explicit
Private IMG As StdPicture
Private IMGX As Long, IMGY As Long
Private Sub Form_Load()
' Original image size
IMGX = Screen.Width / 10
IMGY = Screen.Height / 10
' Init Picturebox
Picture1.Move 0, 0, IMGX, IMGY
Picture1.BorderStyle = vbBSNone
Picture1.BackColor = vbButtonFace
Picture1.AutoRedraw = True
Picture1.Visible = False
' Draw image
Picture1.Line (300, 300)-Step(300, 150), vbBlack, B
' Save to StdPicture
Set IMG = Picture1.Image
' Set control(s)
Command1.Move 330, 330, 260, 110
End Sub
Private Sub Form_Paint()
PaintPicture IMG, 0, 0, ScaleWidth, ScaleHeight
End Sub
Private Sub Form_Resize()
' Change scale / stretch image
Scale (0, 0)-(IMGX, IMGY)
PaintPicture IMG, 0, 0, ScaleWidth, ScaleHeight
' Re-set control(s)
Command1.Move 330, 330, 260, 110
End Sub