EXE・DLLからアイコンを取得する(W)          <TOP>


EXE・DLLからアイコンを取得します。

ExtractIcon ファイルに含まれるアイコンを描画

DrawIcon アイコンを描画

DestroyIcon アイコンのハンドルを解放

GetDC デバイスコンテキストのハンドルを取得

ReleaseDC デバイスコンテキストを解放


例では、システムフォルダにあるc:\windows\system32\shell32.dllに含まれているアイコンを取得しています。

システムフォルダは、明示的に設定しています。(Window2000ではc:\winnt\)

起動直後 → 指定ファイルには238個のアイコンが含まれており60番目のアイコンを表示しています。▲▼で順次呼び出し表示します。

 

 

'================================================================
'= EXE・DLLからアイコンを取得する(W)
'=    (ExtractIcon2.bas)
'================================================================
#include "Windows.bi"

' アイコンを描画
Declare Function Api_DrawIcon& Lib "user32" Alias "DrawIcon" (ByVal hDC&, ByVal x&, ByVal y&, ByVal hIcon&)

' ファイルに含まれるアイコンを描画
Declare Function Api_ExtractIcon& Lib "Shell32" Alias "ExtractIconA" (ByVal hInst&, ByVal lpszExeFileName$, ByVal nIconIndex&)

' アイコンのハンドルを解放
Declare Function Api_DestroyIcon& Lib "user32" Alias "DestroyIcon" (ByVal hIcon&)

' 指定されたウィンドウのクライアント領域または画面全体を表すディスプレイデバイスコンテキストのハンドルを取得
Declare Function Api_GetDC& Lib "user32" Alias "GetDC" (ByVal hWnd&)

' デバイスコンテキストを解放
Declare Function Api_ReleaseDC& Lib "user32" Alias "ReleaseDC" (ByVal hWnd&, ByVal hDC&)

Var Shared Picture1 As Object
Var Shared Text(4) As Object
Var Shared Button(2) As Object

Var Shared sIconFile As String
Var Shared hDC As Long
Var Shared picH As Long
Var Shared picW As Long
Var Shared Value As Integer
Var Shared Min As Integer
Var Shared Max As Integer

'================================================================
'=
'================================================================
Declare Sub Mainform_Start edecl ()
Sub Mainform_Start()
    Picture1.Attach GetDlgItem("Picture1")
    For i = 0 To 4
        Text(i).Attach GetDLgItem("Text" & Trim$(Str$(i+1)))
        Text(i).SetFontSize 14
    Next
    For i = 0 To 2
        Button(i).Attach GetDlgItem("Button" & Trim$(Str$(i+1)))
        Button(i).SetFontSize 14
    Next

    hDC = Api_GetDC(Picture1.GethWnd)
    picW = Picture1.GetWidth
    picH = Picture1.GetHeight

    Button(1).EnableWindow 0
    Button(2).EnableWindow 0
End Sub

'================================================================
'=
'================================================================
Declare Sub Button1_on edecl ()
Sub Button1_on()
    Var hIcon As Long
    Var nIconCount As Long
   
    sIconFile = GetDlgItemText("Text1")

    nIconCount = Api_ExtractIcon(0, sIconFile, -1)
   
    If nIconCount > 0 Then
      
        Min = 0
        Max = nIconCount - 1
        Value = 0
      
        Text(0).SetWindowText sIconFile
        Text(1).SetWindowText Trim$(Str$(nIconCount)) & " Icons"
    End If

    Button(1).EnableWindow -1
    Button(2).EnableWindow -1
End Sub

'================================================================
'= ▲
'================================================================
Declare Sub Button2_on edecl ()
Sub Button2_on()
    Var hIcon As Long
    Var x As Long
    Var y As Long
    Var Ret As Long
   
    x = (picH - 32) / 2
    y = (picW - 32) / 2
 
    If Value <= Max Then
      
        Picture1.cls
        hIcon = Api_ExtractIcon(0, sIconFile, Value)
      
        Ret = Api_DrawIcon(hDC, x, y, hIcon)
        Ret = Api_DestroyIcon(hIcon)
      
        Text(2).SetWindowText "Icon # " & Trim$(Str$(Value))
        Value = value + 1 : If Value > Max Then Value = Max
    End If
End Sub

'================================================================
'= ▼
'================================================================
Declare Sub Button3_on edecl ()
Sub Button3_on()
    Var hIcon As Long
    Var x As Long
    Var y As Long
    Var Ret As Long
   
    x = (picH - 32) / 2
    y = (picW - 32) / 2
 
    If Value >= Min Then
      
        Picture1.cls
        hIcon = Api_ExtractIcon(0, sIconFile, Value)
      
        Ret = Api_DrawIcon(hDC, x, y, hIcon)
        Ret = Api_DestroyIcon(hIcon)
      
        Text(2).SetWindowText "Icon # " & Trim$(Str$(Value))
        Value = value - 1 : If Value < 0 Then Value = 0
    End If
End Sub

'================================================================
'=
'================================================================
While 1
    WaitEvent
Wend
Stop
End