Hi,
I have a GUI with an edit control which I need to detect many windows messages. Subclassing seems to be the right way to do it (I tried OnMessage too, but some messages I need are not detected).
Now, I need to hide some messages from the system, especially WM_KEYDOWN and WM_KEYUP.
I thought subclassing would allow me to process then pass or discard any message before the system could process it, but it seems it doesn't do that, as, for example, a WM_KEYDOWN always generates a WM_CHAR, whatever I return for WM_KEYDOWN message.
Am I doing something wrong?
Here is a sample code in which I am trying to discard WM_KEYDOWN/UP messages received by a subclassed edit control
Code:
Gui, 1:Add, Edit, w100 hwndE1, Some text
Gui, 1:Show
If !Subclass(E1, "WinMsg")
msgbox Subclassing failed.
Return
guiclose:
guiescape:
exitapp
WinMsg(hwnd, uMsg, wParam, lParam)
{
critical
if ((uMsg = 0x100) || (uMsg = 0x101)) ; wm_keydown/up messages
{
; Return 0
Return 1
; Hex2Bin("51", w2Param) ;Mimics Q key vk code
; Hex2Bin("3078313030303031", l2Param) ;Mimics Q key (on US kbd layout)
; VarSetCapacity(Name, 100)
; DllCall( "GetKeyNameText", "int", l2param, "str", Name, "int", 100 ) ;Returns Q (or else, depending on the layout)
; Tooltip % Name
; return DllCall("CallWindowProcA", "UInt", A_EventInfo, "UInt", hwnd, "UInt", uMsg, "UInt", w2Param, "UInt", l2Param)
}
return DllCall("CallWindowProcA", "UInt", A_EventInfo, "UInt", hwnd, "UInt", uMsg, "UInt", wParam, "UInt", lParam)
}
;-------------------------------------------------------------------------------------
; Function: Subclass
; Helper function to subclass control
;
; Parameters:
; hCtrl - handle to control
; func - window procedure
; cbOpt - callback options, by default ""
;
Subclass(hCtrl, func, cbOpt="") {
oldProc := DllCall("GetWindowLong", "uint", hCtrl, "uint", -4)
ifEqual, oldProc, 0, return 0
WndProc := RegisterCallback(func, cbOpt, 4, oldProc)
ifEqual, WndProc, , return 0
return DllCall("SetWindowLong", "UInt", hCtrl, "Int", -4, "Int", WndProc, "UInt")
}
Hex2Bin(hex, ByRef bin, len="")
{
If (len="")
len:=(StrLen(hex)+1)//2
VarSetCapacity(bin,len)
Loop % len
NumPut("0x" SubStr(hex,2*A_Index-1,2), bin, A_Index-1, "Char")
}