Results 1 to 2 of 2

Thread: A More Interesting StretchBlt() Example

  1. #1

    Thread Starter
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359

    A More Interesting StretchBlt() Example

    VB Code:
    1. Option Explicit
    2.  
    3. '' By Jamie Plenderleith
    4. '' [url]http://www.coolground.com/plenderj[/url]
    5. '' [email][email protected][/email]
    6. ''
    7. '' This code will give a form a 'background' picture, and make it stretch
    8. ''   when the form is resized
    9. '' Add a PictureBox to a form called Picture1
    10. '' Change the picDc = GeneradeDC(..) line to point to an appropriate graphic
    11. ''
    12.  
    13. Private picDc As Long, picWidth As Long, picHeight As Long
    14. Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As Long
    15. Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
    16. Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long
    17. 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
    18.  
    19.  
    20. Public Function GenerateDC(ByVal FileName As String) As Long
    21.     If Dir(FileName) = "" Then
    22.         MsgBox "Invalid filename", vbCritical Or vbOKOnly
    23.         End
    24.     End If
    25.     Dim DC As Long, picTemp As IPictureDisp
    26.     DC = CreateCompatibleDC(0)
    27.     If DC < 1 Then Exit Function
    28.     Set picTemp = LoadPicture(FileName)
    29.     picWidth = ScaleX(picTemp.Width)
    30.     picHeight = ScaleY(picTemp.Height)
    31.     SelectObject DC, picTemp
    32.     DeleteObject picTemp
    33.     Set picTemp = Nothing
    34.     GenerateDC = DC
    35. End Function
    36.  
    37. Private Sub Form_Load()
    38.     ScaleMode = 3
    39.     Do Until picDc <> 0
    40.         picDc = GenerateDC("c:\jamie\formulae.bmp")
    41.     Loop
    42.     With Picture1
    43.         .AutoRedraw = True
    44.         .Move 0, 0, ScaleWidth, ScaleHeight
    45.         .ScaleMode = 3
    46.     End With
    47.     doStretch
    48. End Sub
    49.  
    50. Private Function doStretch()
    51.     StretchBlt Picture1.hdc, 0, 0, Picture1.ScaleWidth, Picture1.ScaleHeight, picDc, 0, 0, picWidth, picHeight, vbSrcCopy
    52.     Picture1.Refresh
    53. End Function
    54.  
    55. Private Sub Form_Resize()
    56.     Picture1.Move 0, 0, ScaleWidth, ScaleHeight
    57.     doStretch
    58. End Sub
    Last edited by plenderj; May 22nd, 2002 at 04:19 AM.
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  2. #2

    Thread Starter
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    * 21-October-2004 - Moved to CodeBank *
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width