'加载图片 End SubPrivate Sub Form_Resize() If Me.Width > 0 And Me.Height > 0 Then '只有大于0改变背景及设置背景 Image1.Width = Me.Width Image1.Height = Me.Height End If End Sub
一般说来图片框不会随图片大小改变,但也不是绝对的. 下边的代码可让图片框随图片大小改变而改变: Option Explicit Private Declare Function GetObject Lib "gdi32" Alias "GetObjectA" (ByVal hObject As Long, ByVal nCount As Long, lpObject As Any) As Long Private Type BITMAP bmType As Long bmWidth As Long bmHeight As Long bmWidthBytes As Long bmPlanes As Integer bmBitsPixel As Integer bmBits As Long End Type Dim bm As BITMAP Dim hBmp As Long Private Sub Command1_Click() Picture1.Picture = LoadPicture(App.Path & "\" & "124.jpg") hBmp = Picture1.Picture.Handle GetObject hBmp, LenB(bm), bm Picture1.Width = bm.bmWidth * Screen.TwipsPerPixelX Picture1.Height = bm.bmHeight * Screen.TwipsPerPixelY Picture1.Move (Form1.Width - Picture1.Width) / 2, (Form1.Height - Picture1.Height) / 2 End Sub Private Sub Command2_Click() Picture1.Picture = LoadPicture(App.Path & "\" & "123.jpg") hBmp = Picture1.Picture.Handle GetObject hBmp, LenB(bm), bm Picture1.Width = bm.bmWidth * Screen.TwipsPerPixelX Picture1.Height = bm.bmHeight * Screen.TwipsPerPixelY Picture1.Move (Form1.Width - Picture1.Width) / 2, (Form1.Height - Picture1.Height) / 2 End Sub
VB codeOption Explicit 'Form1上添加1个图片框picture1 Private Declare Function StretchBlt Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal nSrcWidth As Long, ByVal nSrcHeight As Long, ByVal dwRop As Long) As Long Private Declare Function SetStretchBltMode Lib "gdi32" (ByVal hdc As Long, ByVal nStretchMode As Long) As Long Private Const HALFTONE = 4 Private Const SRCCOPY = &HCC0020 ' (DWORD) dest = sourcePrivate Sub Form_Load() Picture1.Picture = LoadPicture("C:\images.jpg") '这时加入背景图,使其一运行就充满窗体 Picture1.Visible = False Picture1.AutoRedraw = True Me.AutoRedraw = True Call Form_Resize Me.Refresh Me.AutoRedraw = False End SubPrivate Sub Form_Resize() Dim Rtn As Long Dim hDC1 As Long, hDC2 As Long hDC1 = Picture1.hdc hDC2 = Me.hdc Call SetStretchBltMode(hDC2, HALFTONE) Rtn = StretchBlt(hDC2, 0, 0, Me.ScaleWidth, Me.ScaleHeight, hDC1, 0, 0, Picture1.ScaleWidth, Picture1.ScaleHeight, SRCCOPY) Me.AutoRedraw = True Me.Refresh End Sub
你的问题应该是铺满整个窗口吧?因为IMAGE本身有个Strech属性支持图片缩放啊,此外还可以使用Render和PaintPicture进行缩放。 铺满整个窗口循环用PaintPicture方法就可以了,代码如下: Private Sub Form_Paint() Dim i As Long, j As Long, w As Long, h As Long
Me.ScaleMode = vbPixels Me.Image1.Visible = False w = ScaleX(Me.Image1.Picture.Width, vbHimetric, vbPixels) h = ScaleY(Me.Image1.Picture.Height, vbHimetric, vbPixels) For i = 0 To Me.ScaleWidth \ w + 1 For j = 0 To Me.ScaleHeight \ h + 1 Me.PaintPicture Me.Image1.Picture, i * w, j * h Next Next End Sub 需要注意两点,一是隐藏了image控件,二是获得图片大小时不能直接取image的width和height。
这个方法我认为很好,在MDI中添加控件picturebox 或者 Image1 再添加图片即可
你的代码基本上是正确的,不过在MDI中基本行不通。
你的代码基本上是正确的,不过在MDI中基本行不通。
原来是图省事,没用MDIFORM测试,果然如此,为此,重新写了一段代码,完全可以满足楼主的要求:Dim WithEvents picBackground As PictureBoxPrivate Sub MDIForm_Load() Set picBackground = Me.Controls.Add("VB.PictureBox", "picBackground") picBackground.Appearance = 0 picBackground.BorderStyle = 0 picBackground.Align = 0 picBackground.ScaleMode = vbPixels picBackground.AutoRedraw = False Set picBackground.Picture = LoadPicture("g:\me.jpg") picBackground.Visible = True End SubPrivate Sub MDIForm_Resize() picBackground.Move 0, 0, Me.Width, Me.Height End SubPrivate Sub MDIForm_Unload(Cancel As Integer) Me.Controls.Remove "picBackground" End SubPrivate Sub picBackground_Paint() Dim i As Long, j As Long, w As Long, h As Long w = picBackground.ScaleX(picBackground.Picture.Width, vbHimetric, vbPixels) h = picBackground.ScaleY(picBackground.Picture.Height, vbHimetric, vbPixels) For i = 0 To Me.Width \ 15 \ w + 1 For j = 0 To Me.Width \ 15 \ h + 1 picBackground.PaintPicture picBackground.Picture, i * w, j * h Next Next End Sub
'image1 加载图片
Image1.Left = 0
Image1.Top = 0
'位置
Image1.Width = Me.Width
Image1.Height = Me.Height
'大小
Image1.Stretch = True
'图片随控件而改变大小
Image1.Picture = LoadPicture("C:\WINDOWS\Web\Wallpaper\Bliss.bmp")
'加载图片
End SubPrivate Sub Form_Resize()
If Me.Width > 0 And Me.Height > 0 Then '只有大于0改变背景及设置背景
Image1.Width = Me.Width
Image1.Height = Me.Height
End If
End Sub
下边的代码可让图片框随图片大小改变而改变:
Option Explicit
Private Declare Function GetObject Lib "gdi32" Alias "GetObjectA" (ByVal hObject As Long, ByVal nCount As Long, lpObject As Any) As Long
Private Type BITMAP
bmType As Long
bmWidth As Long
bmHeight As Long
bmWidthBytes As Long
bmPlanes As Integer
bmBitsPixel As Integer
bmBits As Long
End Type
Dim bm As BITMAP
Dim hBmp As Long Private Sub Command1_Click()
Picture1.Picture = LoadPicture(App.Path & "\" & "124.jpg")
hBmp = Picture1.Picture.Handle
GetObject hBmp, LenB(bm), bm
Picture1.Width = bm.bmWidth * Screen.TwipsPerPixelX
Picture1.Height = bm.bmHeight * Screen.TwipsPerPixelY
Picture1.Move (Form1.Width - Picture1.Width) / 2, (Form1.Height - Picture1.Height) / 2
End Sub Private Sub Command2_Click()
Picture1.Picture = LoadPicture(App.Path & "\" & "123.jpg")
hBmp = Picture1.Picture.Handle
GetObject hBmp, LenB(bm), bm
Picture1.Width = bm.bmWidth * Screen.TwipsPerPixelX
Picture1.Height = bm.bmHeight * Screen.TwipsPerPixelY
Picture1.Move (Form1.Width - Picture1.Width) / 2, (Form1.Height - Picture1.Height) / 2
End Sub
'Form1上添加1个图片框picture1
Private Declare Function StretchBlt Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal nSrcWidth As Long, ByVal nSrcHeight As Long, ByVal dwRop As Long) As Long
Private Declare Function SetStretchBltMode Lib "gdi32" (ByVal hdc As Long, ByVal nStretchMode As Long) As Long
Private Const HALFTONE = 4
Private Const SRCCOPY = &HCC0020 ' (DWORD) dest = sourcePrivate Sub Form_Load()
Picture1.Picture = LoadPicture("C:\images.jpg") '这时加入背景图,使其一运行就充满窗体
Picture1.Visible = False
Picture1.AutoRedraw = True
Me.AutoRedraw = True
Call Form_Resize
Me.Refresh
Me.AutoRedraw = False
End SubPrivate Sub Form_Resize()
Dim Rtn As Long
Dim hDC1 As Long, hDC2 As Long
hDC1 = Picture1.hdc
hDC2 = Me.hdc
Call SetStretchBltMode(hDC2, HALFTONE)
Rtn = StretchBlt(hDC2, 0, 0, Me.ScaleWidth, Me.ScaleHeight, hDC1, 0, 0, Picture1.ScaleWidth, Picture1.ScaleHeight, SRCCOPY)
Me.AutoRedraw = True
Me.Refresh
End Sub
铺满整个窗口循环用PaintPicture方法就可以了,代码如下:
Private Sub Form_Paint()
Dim i As Long, j As Long, w As Long, h As Long
Me.ScaleMode = vbPixels
Me.Image1.Visible = False
w = ScaleX(Me.Image1.Picture.Width, vbHimetric, vbPixels)
h = ScaleY(Me.Image1.Picture.Height, vbHimetric, vbPixels)
For i = 0 To Me.ScaleWidth \ w + 1
For j = 0 To Me.ScaleHeight \ h + 1
Me.PaintPicture Me.Image1.Picture, i * w, j * h
Next
Next
End Sub
需要注意两点,一是隐藏了image控件,二是获得图片大小时不能直接取image的width和height。
再添加图片即可
你的代码基本上是正确的,不过在MDI中基本行不通。
Set picBackground = Me.Controls.Add("VB.PictureBox", "picBackground")
picBackground.Appearance = 0
picBackground.BorderStyle = 0
picBackground.Align = 0
picBackground.ScaleMode = vbPixels
picBackground.AutoRedraw = False
Set picBackground.Picture = LoadPicture("g:\me.jpg")
picBackground.Visible = True
End SubPrivate Sub MDIForm_Resize()
picBackground.Move 0, 0, Me.Width, Me.Height
End SubPrivate Sub MDIForm_Unload(Cancel As Integer)
Me.Controls.Remove "picBackground"
End SubPrivate Sub picBackground_Paint()
Dim i As Long, j As Long, w As Long, h As Long w = picBackground.ScaleX(picBackground.Picture.Width, vbHimetric, vbPixels)
h = picBackground.ScaleY(picBackground.Picture.Height, vbHimetric, vbPixels)
For i = 0 To Me.Width \ 15 \ w + 1
For j = 0 To Me.Width \ 15 \ h + 1
picBackground.PaintPicture picBackground.Picture, i * w, j * h
Next
Next
End Sub