Hey,
While evaluating Xojo, I noticed that the “SetWindowIcon.xojo_binary_project” example did not worked as expected.
The problem was the use of the API SetClassLongW() instead of the SetClassLongPtrW() .
SetClassLongW() could be used only on 32 bit code. SetClassLongPtrW() will work with both 32 and 64bit applications.
There was also some 32-64bit confusion on parameters.
I changed GetModuleHandleA to GetModuleHandleW to be more coherent with the rest of the code.
I used GetTickCount() to get kind-of random icon selection from Shell32.dll.
I wanted to send this to Xojo directly but didn’t find any mail for that purpose.
Here it is…
#If TargetWindows Then
'From [Choose a Project/Examples/Platform-Specific/Windows/SetWindowIcon.xojo_binary_project]
'[Window1/Controls/SetIconButton/Action]
Soft Declare Function GetTickCount LIB "Kernel32.dll" () AS UInt32
'>> 32 or 64bit Window handle , 32bit long, 32bit long ) AS 32bit long
Soft Declare Function SetClassLongPtrW Lib "User32" (hwnd As Integer, index As Int32, newLong As Int32) As Int32
'Soft Declare Function SetClassLongW Lib "User32" (hwnd As Integer, index As Integer, newLong As Integer) As Integer
Soft Declare Function ExtractIconW Lib "Shell32" (hinstance As Integer, absPath As WString, index As Int32) As Integer
Declare Function GetModuleHandleW Lib "Kernel32" (path As CString) As Integer
// Get a handle to the current application's HINSTANCE
Dim moduleHandle As Integer
moduleHandle = GetModuleHandleW("")
// Now, extract a random icon from Shell32.dll
Dim hIcon As Integer
'hIcon = ExtractIconW(moduleHandle, SpecialFolder.System.Child("Shell32.dll").NativePath, -151 )
hIcon = ExtractIconW(moduleHandle, SpecialFolder.System.Child("Shell32.dll").NativePath, GetTickCount() AND 255)
'>> GetTickCount() AND 255 simulate a random number, there is more tnan 300 icons in Shell32.dll
'>> The menu need some cleanup to remoove invalid entrys.
If hIcon <> 0 Then
// Set the icon for the window only if we were
// able to pull an icon from the dll
'Call SetClassLongW(Self.Handle, -14, icon)
Call SetClassLongPtrW(Self.Handle, -34, hIcon) // -34 is GCL_HICONSM (Small)
Call SetClassLongPtrW(Self.Handle, -14, hIcon) // -14 is GCL_HICON
'>> Call DestroyIcon(hIcon) on WM_DESTROY
End If
#EndIf