PDA

Click to See Complete Forum and Search --> : A More Interesting StretchBlt() Example


plenderj
Apr 2nd, 2002, 06:55 AM
Option Explicit

'' By Jamie Plenderleith
'' http://www.coolground.com/plenderj
'' plenderj@tcd.ie
''
'' This code will give a form a 'background' picture, and make it stretch
'' when the form is resized
'' Add a PictureBox to a form called Picture1
'' Change the picDc = GeneradeDC(..) line to point to an appropriate graphic
''

Private picDc As Long, picWidth As Long, picHeight As Long
Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As Long
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject 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


Public Function GenerateDC(ByVal FileName As String) As Long
If Dir(FileName) = "" Then
MsgBox "Invalid filename", vbCritical Or vbOKOnly
End
End If
Dim DC As Long, picTemp As IPictureDisp
DC = CreateCompatibleDC(0)
If DC < 1 Then Exit Function
Set picTemp = LoadPicture(FileName)
picWidth = ScaleX(picTemp.Width)
picHeight = ScaleY(picTemp.Height)
SelectObject DC, picTemp
DeleteObject picTemp
Set picTemp = Nothing
GenerateDC = DC
End Function

Private Sub Form_Load()
ScaleMode = 3
Do Until picDc <> 0
picDc = GenerateDC("c:\jamie\formulae.bmp")
Loop
With Picture1
.AutoRedraw = True
.Move 0, 0, ScaleWidth, ScaleHeight
.ScaleMode = 3
End With
doStretch
End Sub

Private Function doStretch()
StretchBlt Picture1.hdc, 0, 0, Picture1.ScaleWidth, Picture1.ScaleHeight, picDc, 0, 0, picWidth, picHeight, vbSrcCopy
Picture1.Refresh
End Function

Private Sub Form_Resize()
Picture1.Move 0, 0, ScaleWidth, ScaleHeight
doStretch
End Sub

plenderj
Oct 21st, 2004, 09:47 AM
* 21-October-2004 - Moved to CodeBank *