Mdi窗体本身的Picture属性并不具有随窗体自动缩放的功能
查了资料,通常有以下几个方法
1 先在Mdi窗体上放置Picture,再在Picture上放置Image,将Image的自动适应属性设为真Stretch=True,然后将图片导入Image,在窗体缩放时依据窗体大小调整Image的大小
此方法的缺点是,Picture会掩盖 Mdi子窗体
2 变通方法:建一个子窗体,其上放置Picture,Image,并在Mdi窗体缩放时调整相应的子窗体及对应的Image的大小与Mdi窗体一致
此方法的缺点是,需保证此作为背景的子窗体总是处于底层 ,可通过API函数 来实现,不方便
'窗体置于底层
Call SetWindowPos(Me.hwnd, 1, 0, 0, 0, 0, 3)
现整理一个简单的变通方法,大家探讨
-------------------------------------------------------------
Mdi窗体上放值Picture ,且Visible = False
Picture上放置Image ,ImageBack ,且 Stretch=True
Picture上放置Picture , PictureBack ,且 AutoRedraw = True
窗体Load时装载图片到 ImagePrivate Sub MDIForm_Load()
Imageback.Picture = LoadPicture(App.Path & ""\img\desktopback.jpg"")
End Sub窗体缩放时,调整图片大小,并通过PictureBack中转到 Mdi.PicturePrivate Sub MDIForm_Resize()
Imageback.Width = Me.Width
Imageback.Height = Me.Height
Pictureback.Width = Me.Width
Pictureback.Height = Me.Height
Pictureback.PaintPicture Imageback.Picture, 0, 0, Me.Width, Me.Height
Pictureback.Picture = Pictureback.Image
Me.Picture = Pictureback.Picture
End Sub
查了资料,通常有以下几个方法
1 先在Mdi窗体上放置Picture,再在Picture上放置Image,将Image的自动适应属性设为真Stretch=True,然后将图片导入Image,在窗体缩放时依据窗体大小调整Image的大小
此方法的缺点是,Picture会掩盖 Mdi子窗体
2 变通方法:建一个子窗体,其上放置Picture,Image,并在Mdi窗体缩放时调整相应的子窗体及对应的Image的大小与Mdi窗体一致
此方法的缺点是,需保证此作为背景的子窗体总是处于底层 ,可通过API函数 来实现,不方便
'窗体置于底层
Call SetWindowPos(Me.hwnd, 1, 0, 0, 0, 0, 3)
现整理一个简单的变通方法,大家探讨
-------------------------------------------------------------
Mdi窗体上放值Picture ,且Visible = False
Picture上放置Image ,ImageBack ,且 Stretch=True
Picture上放置Picture , PictureBack ,且 AutoRedraw = True
窗体Load时装载图片到 ImagePrivate Sub MDIForm_Load()
Imageback.Picture = LoadPicture(App.Path & ""\img\desktopback.jpg"")
End Sub窗体缩放时,调整图片大小,并通过PictureBack中转到 Mdi.PicturePrivate Sub MDIForm_Resize()
Imageback.Width = Me.Width
Imageback.Height = Me.Height
Pictureback.Width = Me.Width
Pictureback.Height = Me.Height
Pictureback.PaintPicture Imageback.Picture, 0, 0, Me.Width, Me.Height
Pictureback.Picture = Pictureback.Image
Me.Picture = Pictureback.Picture
End Sub
已在XP,vb6 ,sql2000 环境下测试通过,不会引起子窗体的异常
Imageback.Width = Me.Width
Imageback.Height = Me.Height
Pictureback.Width = Me.Width'这句代码要报错
Pictureback.Height = Me.Height
Pictureback.PaintPicture Imageback.Picture, 0, 0, Me.Width, Me.Height
Pictureback.Picture = Pictureback.Image
Me.Picture = Pictureback.Picture
End Sub
我试过了没错,不知你是什么环境,大家探讨下
我的环境是 XP,vb6 ,sql2000
Me.Picture = Pictureback.Picture
应该为:
Me.Picture = Imageback.Picture
Picture上放置Image 名称为 ImageBack ,且 Stretch=True
Picture上放置Picture 名称为 PictureBack ,且 AutoRedraw = True
LZ的思路是借助Image的自动适应属性并将Image的Picture属性赋值给MDI窗体.
只要是MDI窗体,最终代码应该是:
Private Sub MDIForm_Load()
Pictureback.AutoRedraw = True
Pictureback.Visible = False
End SubPrivate Sub MDIForm_Resize()
Imageback.Width = Me.Width
Imageback.Height = Me.Height
Pictureback.Height = Me.Height
Me.Picture = Imageback.Picture
End Sub
Const NEWFRAME = 1
Private Declare Function Escape Lib "gdi32" (ByVal hdc As Long, ByVal nEscape As Long, ByVal nCount As Long, ByVal lpInData As String, lpOutData As Any) As Long
Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As Long
Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long
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 DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long
Dim hMemoryDC As Long
Private Sub Command1_Click()
'KPD-Team 1999
'URL: http://www.allapi.net/
'E-Mail: [email protected]
'API uses pixels
Picture1.ScaleMode = vbPixels
Printer.ScaleMode = vbPixels
'Take paper
Printer.Print "" 'Create a compatible device context
hMemoryDC = CreateCompatibleDC(Picture1.hdc)
'Select Picture1's picture into our new device context
hOldBitMap = SelectObject(hMemoryDC, Picture1.Picture) 'Stretch our picture to the height and width of the paper
StretchBlt Printer.hdc, 0, 0, Printer.ScaleWidth, Printer.ScaleHeight, hMemoryDC, 0, 0, Picture1.ScaleWidth, Picture1.ScaleHeight, vbSrcCopy 'Select the original bitmap into our DC
hOldBitMap = SelectObject(hMemoryDC, hOldBitMap)
'Delete our memorydc
DeleteDC hMemoryDC 'Access our printer device
Escape Printer.hdc, NEWFRAME, 0, 0&, 0& 'End of document
Printer.EndDoc
End Sub