Cho mình hỏi khi chuyển file sang máy khác mà không có file ico đó thì nó vẫn hiện hay mất đi? Nếu mất thì có cách nào để hiện mà không cần có file ico đó không?Không phải sửa ExtractIcon thành LoadIcon đâu.
Giả sử trên c:\ có tập tin logo.ico.
Code trong UserForm
Mã:Option Explicit Private Const WM_SETICON = &H80 #If VBA7 Then Private Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As LongPtr Private Declare PtrSafe Function ExtractIcon Lib "shell32.dll" Alias "ExtractIconA" (ByVal hInst As LongPtr, ByVal lpszExeFileName As String, ByVal nIconIndex As Long) As LongPtr Private Declare PtrSafe Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As LongPtr, ByVal wMsg As Long, ByVal wParam As LongPtr, ByVal lParam As LongPtr) As LongPtr #Else Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long 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 SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long #End If Private Sub UserForm_Initialize() #If VBA7 Then Dim lngIcon As LongPtr Dim lnghWnd As LongPtr #Else Dim lngIcon As Long Dim lnghWnd As Long #End If If Val(Application.Version) < 9 Then lnghWnd = FindWindow("ThunderXFrame", Caption) 'XL97 Else lnghWnd = FindWindow("ThunderDFrame", Caption) 'XL2000 End If #If VBA7 Then lngIcon = ExtractIcon(Application.HinstancePtr, "c:\logo.ico", 0) #Else lngIcon = ExtractIcon(Application.Hinstance, "c:\logo.ico", 0) #End If SendMessage lnghWnd, WM_SETICON, 0, lngIcon End Sub
Code khi chạy, tức ở Real Time, nó mới lấy ICO từ đĩa cứng nên khi sang máy khác lúc chạy code sẽ không tìm thấy ICO ở máy khác "kia".Cho mình hỏi khi chuyển file sang máy khác mà không có file ico đó thì nó vẫn hiện hay mất đi? Nếu mất thì có cách nào để hiện mà không cần có file ico đó không?
#If VBA7 Then
lngIcon = ExtractIcon(Application.HinstancePtr, ThisWorkbook.Path & "\logo.ico", 0)
#Else
lngIcon = ExtractIcon(Application.Hinstance, ThisWorkbook.Path & "\logo.ico", 0)
#End If
Option Explicit
Private Const WM_SETICON = &H80
#If VBA7 Then
Private Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As LongPtr
Private Declare PtrSafe Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As LongPtr, ByVal wMsg As Long, ByVal wParam As LongPtr, ByVal lParam As LongPtr) As LongPtr
#Else
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
#End If
Private Sub UserForm_Initialize()
#If VBA7 Then
Dim lnghWnd As LongPtr
#Else
Dim lnghWnd As Long
#End If
If Val(Application.Version) < 9 Then
lnghWnd = FindWindow("ThunderXFrame", Caption) 'XL97
Else
lnghWnd = FindWindow("ThunderDFrame", Caption) 'XL2000
End If
SendMessage lnghWnd, WM_SETICON, 0, Image1.Picture.Handle
End Sub