VB 显示PNG图片适合透明窗体和一般窗体

Private Declare Function Gdiplusstartup Lib "gdiplus" (token As Long,inputbuf As GdiplusstartupInput,Optional ByVal outputbuf As Long = 0) As GpStatus
Private Declare Sub GdiplusShutdown Lib "gdiplus" (ByVal token As Long)

Private Declare Function GdipCreateFromHDC Lib "gdiplus" (ByVal hwnd As Long,graphics As Long) As GpStatus
Private Declare Function GdipDeleteGraphics Lib "gdiplus" (ByVal graphics As Long) As GpStatus
Private Declare Function GdipDrawImageRect Lib "gdiplus" (ByVal graphics As Long,ByVal image As Long,ByVal x As Single,ByVal y As Single,ByVal Width As Single,ByVal Height As Single) As GpStatus
Private Declare Function GdipLoadImageFromFile Lib "gdiplus" (ByVal filename As String,image As Long) As GpStatus
Private Declare Function GdipGetimageWidth Lib "gdiplus" (ByVal image As Long,Width As Long) As GpStatus
Private Declare Function GdipGetimageHeight Lib "gdiplus" (ByVal image As Long,Height As Long) As GpStatus
Private Declare Function GdipdisposeImage Lib "gdiplus" (ByVal image As Long) As GpStatus

Private Type GdiplusstartupInput
    Gdiplusversion As Long
    DebugEventCallback As Long
    SuppressBackgroundThread As Long
    SuppressExternalCodecs As Long
End Type

Private Enum GpStatus
    Ok = 0
    GenericError = 1
    InvalidParameter = 2
    OutOfMemory = 3
    ObjectBusy = 4
    InsufficientBuffer = 5
    NotImplemented = 6
    Win32Error = 7
    WrongState = 8
    Aborted = 9
    FileNotFound = 10
    ValueOverflow = 11
    AccessDenied = 12
    UnkNownImageFormat = 13
    FontFamilyNotFound = 14
    FontStyleNotFound = 15
    NottrueTypeFont = 16
    UnsupportedGdiplusversion = 17
    Gdiplusnotinitialized = 18
    PropertyNotFound = 19
    PropertyNotSupported = 20
End Enum


'主要代码就下面这几句而已.

Private Const c_pngPath As String = "1.png"   '要显示图片名称和路径。

Dim m_token As Long

Private Sub Form_paint()
     Dim pImg As Long
     Dim pGraphics As Long
     Dim w As Long,h As Long
    
     Call GdipCreateFromHDC(Me.hDC,pGraphics)
     Call GdipLoadImageFromFile(StrConv(c_pngPath,vbUnicode),pImg)
     Call GdipGetimageWidth(pImg,w)
     Call GdipGetimageHeight(pImg,h)
     Call GdipDrawImageRect(pGraphics,pImg,w,h)
    
     Call GdipdisposeImage(pImg)
     Call GdipDeleteGraphics(pGraphics)
End Sub

Private Sub Form_Load()
     Dim StartupInput As GdiplusstartupInput
     StartupInput.Gdiplusversion = 1
     If Gdiplusstartup(m_token,StartupInput,ByVal 0) Then
             MsgBox "Error initializing GDI+"
             Exit Sub
     End If
End Sub

Private Sub Form_Unload(Cancel As Integer)
     Call GdiplusShutdown(m_token)
End Sub

相关文章

Format[$] ( expr [ , fmt ] ) format 返回变体型 format$ 强...
VB6或者ASP 格式化时间为 MM/dd/yyyy 格式,竟然没有好的办...
在项目中添加如下代码:新建窗口来显示异常信息。 Namespace...
转了这一篇文章,原来一直想用C#做k3的插件开发,vb没有C#用...
Sub 分列() ‘以空格为分隔符,连续空格只算1个。对所选...
  窗体代码 1 Private Sub Text1_OLEDragDrop(Data As Dat...