English Sentence Loading...
英语句子加载中...

VB取得exe文件或者是dll文件里的图标(转载)

例子一

1.在窗体上建立三个按钮分别命名为cPrv,cOpen和cNext。

2.添加一个commanddialong控件名字就用默认的。

3.把下面的代码复制到窗体的代码框中就可以了:

Private Declare Function ExtractIcon Lib "shell32.dll" Alias "ExtractIconA" (ByVal hInst As Long, ByVal lpszExeFileName As String, ByVal nIconIndex As Long) As Long
Private Declare Function DrawIcon Lib "user32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal hIcon As Long) As Long

Dim lNum As Long
Dim lCount As Long
Dim astr As String

Private Sub CNext_Click()
    lNum = lNum + 1
    x1 = ExtractIcon(App.hInstance, astr, lNum)
    Form1.Refresh
    x2 = DrawIcon(Form1.hdc, 10, 10, x1)
    cPrv.Enabled = True
    If lNum = lCount Then
      cNext.Enabled = False
    End If
End Sub

Private Sub COpen_Click()
    Dim x1 As Long
    Dim x2 As Long
  
    CommonDialog1.ShowOpen
    lCount = ExtractIcon(App.hInstance, CommonDialog1.FileName, -1)
    If lCount <> 0 Then
      astr = CommonDialog1.FileName
      lNum = 0
      Form1.Caption = astr
      cPrv.Enabled = False
      cNext.Enabled = True
      x1 = ExtractIcon(App.hInstance, astr, lNum)
      Form1.Refresh
      x2 = DrawIcon(Form1.hdc, 10, 10, x1)
    Else
      x1 = MsgBox("File has no icon resource.")
      cPrv.Enabled = False
      cNext.Enabled = False
    End If
End Sub

Private Sub CPrv_Click()
    lNum = lNum - 1
    x1 = ExtractIcon(App.hInstance, astr, lNum)
    Form1.Refresh
    x2 = DrawIcon(Form1.hdc, 10, 10, x1)
    cNext.Enabled = True
    If lNum = 0 Then
      cPrv.Enabled = False
    End If
End Sub


=============================================================================

例子二


新建一个工程,在窗体上加上两个Label: Label1和Label2;以及一个VScroll1和Picture1
示例源代码如下:
Option Explicit

Private Declare Function DrawIcon Lib "user32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal hIcon As Long) As Long
Private Declare Function ExtractIcon Lib "shell32" Alias "ExtractIconA" (ByVal hInst As Long, ByVal lpszExeFileName As String, ByVal nIconIndex As Long) As Long
Private Declare Function DestroyIcon Lib "user32" (ByVal hIcon As Long) As Long

'这个路径根据你的系统改变
Private Const sIconFile As String = "c:\winnt\system32\User32.dll"

Private Sub Form_Load()
    Dim hIcon As Long
    Dim nIconCount As Long
    nIconCount = ExtractIcon(0&, sIconFile, -1)
    If nIconCount > 0 Then
       VScroll1.Min = 0
       VScroll1.Max = nIconCount - 1
       VScroll1.Value = 0
       Label1.Caption = nIconCount & " 个图标"
       Call VScroll1_Change
    End If
End Sub

Private Sub VScroll1_Change()
    Dim index As Long
    Dim hIcon As Long
    index = VScroll1.Value
    If index <= VScroll1.Max Then
       Picture1.Cls
       hIcon = ExtractIcon(0&, sIconFile, index)
       Call DrawIcon(Picture1.hdc, 0, 0, hIcon)
       Call DestroyIcon(hIcon)
       Label2.Caption = "序号: " & index
    End If
End Sub

Private Sub VScroll1_Scroll()
    Dim index As Long
    Dim hIcon As Long
    index = VScroll1.Value
    If index <= VScroll1.Max Then
       Picture1.Cls
       hIcon = ExtractIcon(0&, sIconFile, index)
       Call DrawIcon(Picture1.hdc, 0, 0, hIcon)
       Call DestroyIcon(hIcon)
       Label2.Caption = "序号: " & index
    End If
End Sub
文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags:
相关日志:
评论: 0 | 引用: 0 | 查看次数: 1788