kcvinu
10-04-2021, 23:35
Hi all,
I am trying to subclass a button, But SetWindowSubclass is not working. The window starts and suddenly thinBasic.exe is stop working.
Here is my function declarartions.
Declare Function SetWindowSubclass Lib "Comctl32.dll" Alias "SetWindowSubclass"(ByVal hWnd As DWord,
Byref pfnSubclass As Long,
ByRef uIdSubclass as Dword,
Byval dwRefData Long) As word
Declare Function DefSubclassProc Lib "Comctl32.dll" Alias "DefSubclassProc"(ByVal hWnd As DWord,
Byval uMsg As Dword,
ByVal wParam as Dword,
ByVal lParam Long) As Long
Declare Function RemoveWindowSubclass Lib "Comctl32.dll" Alias "RemoveWindowSubclass"(ByVal hWnd As DWord,
Byref pfnSubclass As Long,
Byval uIdSubclass as Dword) As Boolean
And here is my function calling code.
If Me.mHandle <> 0 Then
Dim fnPtr As Long = CodePtr(ButtonWndProc)
dim myPtr As Long = Varptr(Me) '// My tButton type
Dim rst = SetWindowSubclass(Me.mHandle, fnPtr, globalBtnSubClassID, myPtr)
PrintL("Sublcass result - ", rst) '// This is printing 1. That means, function success
EndIf
Here is my SubclassProc.
function ButtonWndProc(Byval hwnd As Dword, Byval message As Dword, Byval wParam As Dword, Byval lParam As Long,
Byval uIdSubclass As Long, Byref dwRefData As Long) As Long
printl("Button message - ", message)
return DefSubclassProc(hwnd, message, wParam, lParam)
End Function
And i found one weird behaviour of thinbasic interpreter. See this code.
SetWindowSubclass(ByVal hWnd As DWord, Byref pfnSubclass As Long, ByRef uIdSubclass as Dword, Byval dwRefData Long)
The value for "pfnSubclass " is CodePtr(ButtonWndProc). But somehow thinbasic interpreter didn't liked it the way we write the code. It wants us to write like this
Dim fnPtr As Long = CodePtr(ButtonWndProc)
Now, we can use the variable fnPtr inside the function. But to do this, thinbasic will inform us like "Parameter "hWnd" is expected as Byref but it is not passed as Byref"
I suddenly surprised with this error message. But when i remove CodePtr(ButtonWndProc) from the parameter position and placed the "fnPtr" variable then interpreter says "Parameter "uIdSubclass " is expected as Byref but it is not passed as Byref" So i got the trick and i changed the Varptr(Me) from parameter position and use a varibale for it like this
dim myPtr As Long = Varptr(Me)
After doing this the function worked succesfully and give result 1. But thinBasic exe stopped working.
I am trying to subclass a button, But SetWindowSubclass is not working. The window starts and suddenly thinBasic.exe is stop working.
Here is my function declarartions.
Declare Function SetWindowSubclass Lib "Comctl32.dll" Alias "SetWindowSubclass"(ByVal hWnd As DWord,
Byref pfnSubclass As Long,
ByRef uIdSubclass as Dword,
Byval dwRefData Long) As word
Declare Function DefSubclassProc Lib "Comctl32.dll" Alias "DefSubclassProc"(ByVal hWnd As DWord,
Byval uMsg As Dword,
ByVal wParam as Dword,
ByVal lParam Long) As Long
Declare Function RemoveWindowSubclass Lib "Comctl32.dll" Alias "RemoveWindowSubclass"(ByVal hWnd As DWord,
Byref pfnSubclass As Long,
Byval uIdSubclass as Dword) As Boolean
And here is my function calling code.
If Me.mHandle <> 0 Then
Dim fnPtr As Long = CodePtr(ButtonWndProc)
dim myPtr As Long = Varptr(Me) '// My tButton type
Dim rst = SetWindowSubclass(Me.mHandle, fnPtr, globalBtnSubClassID, myPtr)
PrintL("Sublcass result - ", rst) '// This is printing 1. That means, function success
EndIf
Here is my SubclassProc.
function ButtonWndProc(Byval hwnd As Dword, Byval message As Dword, Byval wParam As Dword, Byval lParam As Long,
Byval uIdSubclass As Long, Byref dwRefData As Long) As Long
printl("Button message - ", message)
return DefSubclassProc(hwnd, message, wParam, lParam)
End Function
And i found one weird behaviour of thinbasic interpreter. See this code.
SetWindowSubclass(ByVal hWnd As DWord, Byref pfnSubclass As Long, ByRef uIdSubclass as Dword, Byval dwRefData Long)
The value for "pfnSubclass " is CodePtr(ButtonWndProc). But somehow thinbasic interpreter didn't liked it the way we write the code. It wants us to write like this
Dim fnPtr As Long = CodePtr(ButtonWndProc)
Now, we can use the variable fnPtr inside the function. But to do this, thinbasic will inform us like "Parameter "hWnd" is expected as Byref but it is not passed as Byref"
I suddenly surprised with this error message. But when i remove CodePtr(ButtonWndProc) from the parameter position and placed the "fnPtr" variable then interpreter says "Parameter "uIdSubclass " is expected as Byref but it is not passed as Byref" So i got the trick and i changed the Varptr(Me) from parameter position and use a varibale for it like this
dim myPtr As Long = Varptr(Me)
After doing this the function worked succesfully and give result 1. But thinBasic exe stopped working.