本人刚学vb,还有很多地方不懂,特来请教大虾们
     问题是这样:我有一个form1的窗体,里面有一个image控件,我是想使在form2中显示image中的图片,并使其根据图片的大小自动更改窗体的大小。
     我写的部分代码如下:
    form2.Picture = form1.Image1.Picture
    form2.Height = Image1.Picture.Height
    form2.Width = Image1.Picture.Width
    我尝试过使用ScaleHeight也不能准确调整窗体,相信是图片单位的处理没有做好。还有想问一下,Image1.Picture的单位是twip,还是像素?我已把窗体的scaleMode设置成像素。

解决方案 »

  1.   

    VB6 / xpsp2 pass 
    Private Sub Command2_Click()
    With Form2
            .Image1.Picture = Form1.Image1.Picture
            .Image1.Stretch = True
           .Width = .Image1.Width + 120
            .Height = .Image1.Height + 500
            .Show
        End With
    End Sub
      

  2.   

    ScaleMode 统一用 twips。
    窗体大小应该加上4边宽度。
    form2.Height = Image1.Picture.Height + (form2.height - form2.scaleheight)
    form2.Width = Image1.Picture.Width + (from2.width - form2.scalewidth)
      

  3.   

    按楼上的方法去做,好像不行,还是我没看懂?比图片实际大小要大。
    1楼的方法可以,但是觉得不是很精确。width+130效果可能会好点。但是+120和+500这两个数字如何确定?有没有其他简单点的方法?
      

  4.   

    form2.height 是窗体宽度,form2.scaleheight 是窗体 ClientArea 的宽度,相减就是边框的宽度和。高度同理。
    这样无论窗体式样和桌面主题如何变化,都是正确的。
      

  5.   

    按你的方法,我这样写:
    form2.Picture = Form1.Image1.Picture
    form2.Height = Form1.Image1.Picture.Height + (form2.Height - form2.ScaleHeight)
    form2.Width = Form1.Image1.Picture.Width + (form2.Width - form2.ScaleWidth)
    form2.Show
    这样调试的效果还是一样,多出很多空白,是否我其他设置有错?
    不管怎样,我都很感激你的积极回答!谢谢!
      

  6.   

    既然设置
    Form2.Picture = Form1.Image1.Picture
    那么当
    form2.Height = Form1.Image1.Picture.Height 
    所谓form2.Height与图片本身的Height相等。
      

  7.   

    lertulo的方法基本可以实现我的效果,但也感谢Tiger_Zhao 的回答,尽管我没有用到Tiger_Zhao 的方法。谢谢你们!结帖~~~