Sign in to follow this  
Followers 0
martin

subclassed context menu background problem

4 posts in this topic

If I set a label to have a cursor using GuiCtrlSetCursor then the first time a subclassed context menu appears its background is black. If you right click a second time to get the context menu redrawn then the background is changed to normal.

Here is an example which is a copy of a post by Siao with 2 lines added. If you comment out the GuiSetCursor for the label then the problem goes. This problem was in a completely different script, and the context menu wasn't for an input, but this is about the simplest example I can make to show what I'm talking about.

;post by Siao 11 Jan 2008
;GuiRegisterMsg lets you work with messages of the main window. 
;To work with messages of controls (such as in your case WM_PASTE to edit control), you need to subclass that control.

#include <GUIConstants.au3>
#Include <GuiEdit.au3>
#include <windowsconstants.au3>
    

$hGui = GUICreate("Type or paste some stuff",400,200,-1,-1,$WS_THICKFRAME,-1)
$cInput = GUICtrlCreateInput("",20,20,360,20)
$cInput2 = GUICtrlCreateInput("",20,50,360,20)

#Region added lines to show problem with context menu
$lab1 = GUICtrlCreateLabel("abcd",1,1,30,18)

;this Next line causes the problem With the context nenu background
GUICtrlSetCursor (-1, 9); other values give same problem 
#EndRegion added lines to show problem

$wProcNew = DllCallbackRegister("_MyWindowProc", "ptr", "hwnd;uint;long;ptr")
$wProcOld = _WinSubclass(GUICtrlGetHandle($cInput), DllCallbackGetPtr($wProcNew))
_WinSubclass(GUICtrlGetHandle($cInput2), DllCallbackGetPtr($wProcNew))
;_WinSubclass(GUICtrlGetHandle($cInput3), DllCallbackGetPtr($wProcNew)) and so on

$DummyMenu    = GUICtrlCreateDummy()
$ContextMenu    = GUICtrlCreateContextMenu($DummyMenu)
$CommonMenuItem = GUICtrlCreateMenuItem("Common", $ContextMenu)
$FileMenuItem   = GUICtrlCreateMenuItem("File", $ContextMenu)
GUICtrlCreateMenuItem("", $ContextMenu)
$ExitMenuItem   = GUICtrlCreateMenuItem("Exit", $ContextMenu)


GUISetState(@SW_SHOW)
While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            ExitLoop
    EndSwitch
WEnd

Func do_clever_stuff_with_clipboard($hWnd)
    $sData = ClipGet()
    If @error Then Return 0;clip data is not text or clip empty
;do whatever
    $sData = StringUpper($sData)
;set text
    GUICtrlSetData(_WinAPI_GetDlgCtrlID($hWnd), $sData);or _GUICtrlEdit_SetText($hWnd, $sData)
    Return 1
EndFunc

; Show a menu in a given GUI window which belongs to a given GUI ctrl
Func ShowMenu($hWnd, $nContextID)
    Local $hMenu = GUICtrlGetHandle($nContextID)
   
    $arPos = MouseGetPos()
   
    Local $x = $arPos[0]
    Local $y = $arPos[1]
   
    DllCall("user32.dll", "int", "TrackPopupMenuEx", "hwnd", $hMenu, "int", 0, "int", $x, "int", $y, "hwnd", $hWnd, "ptr", 0)
EndFunc

Func _MyWindowProc($hWnd, $uiMsg, $wParam, $lParam)
    Switch $uiMsg
        Case $WM_PASTE
            Return do_clever_stuff_with_clipboard($hWnd)
    EndSwitch
    If $uiMsg = $WM_CONTEXTMENU And $hWnd = GUICtrlGetHandle($cInput)Then
    ;ConsoleWrite("cm" & @LF)
        ShowMenu($hGui, $ContextMenu)
        Return 0
    EndIf
;pass the unhandled messages to default WindowProc
    Return _CallWindowProc($wProcOld, $hWnd, $uiMsg, $wParam, $lParam)
EndFunc

Func _CallWindowProc($lpPrevWndFunc, $hWnd, $Msg, $wParam, $lParam)
    Local $aRet = DllCall('user32.dll', 'int', 'CallWindowProc', 'ptr', $lpPrevWndFunc, 'hwnd', $hWnd, 'uint', $Msg, 'wparam', $wParam, 'lparam', $lParam)
    Return $aRet[0]
EndFunc

;-- Wrapper for SetWindowLong API
Func _WinSubclass($hWnd, $lpNewWindowProc)
;#define GWL_WNDPROC (-4)
    Local $aTmp, $sFunc = "SetWindowLongW"
   ;If @Unicode Then $sFunc &= "W"
    $aTmp = DllCall("user32.dll", "ptr", $sFunc, "hwnd", $hWnd, "int", -4, "ptr", $lpNewWindowProc)
    If @error Then Return SetError(1, 0, 0)
    If $aTmp[0] = 0 Then Return SetError(1, 0, 0)
    Return $aTmp[0]
EndFunc ;==>_WinSubclass

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Yea, and if you move the mouse along the input box, there's also this cursor flickering between normal and ibeam...

Not sure if it's proper (because right now I'm not sure what exactly is the root of the problem), but doing this works:

Func _MyWindowProc($hWnd, $uiMsg, $wParam, $lParam)
    Switch $uiMsg
        Case $WM_PASTE
            Return do_clever_stuff_with_clipboard($hWnd)
        Case $WM_CONTEXTMENU
            If $hWnd = GUICtrlGetHandle($cInput) Then
;ConsoleWrite("cm" & @LF)
                ShowMenu($hGui, $ContextMenu)
                Return 0
            EndIf
        Case $WM_SETCURSOR
            GUICtrlSetCursor(_WinAPI_GetDlgCtrlID($hWnd), 5);;set Ibeam cursor
            Return 1;;and don't let default windowproc mess things up
    EndSwitch

;pass the unhandled messages to default WindowProc
    Return _CallWindowProc($wProcOld, $hWnd, $uiMsg, $wParam, $lParam)
EndFunc
Edited by Siao

"be smart, drink your wine"

Share this post


Link to post
Share on other sites

Looks good, but why re-write wrappers for SetWindowLong and TrackPopUPMenu?

CallWindowProc looks like a good candidate to add to _WinAPI_xxxxx.

;post by Siao 11 Jan 2008
;GuiRegisterMsg lets you work with messages of the main window.
;To work with messages of controls (such as in your case WM_PASTE to edit control), you need to subclass that control.

#include <GUIConstantsEx.au3>
#Include <GuiEdit.au3>
#include <windowsconstants.au3>
#include <Constants.au3>
#include <GuiMenu.au3>
#include <WinAPI.au3>
    

$hGui = GUICreate("Type or paste some stuff",400,200,-1,-1,$WS_THICKFRAME,-1)
$cInput = GUICtrlCreateInput("",20,20,360,20)
$cInput2 = GUICtrlCreateInput("",20,50,360,20)

#Region added lines to show problem with context menu
$lab1 = GUICtrlCreateLabel("abcd",1,1,30,18)

;this Next line causes the problem With the context nenu background
GUICtrlSetCursor (-1, 9); other values give same problem
#EndRegion added lines to show problem

$wProcNew = DllCallbackRegister("_MyWindowProc", "ptr", "hwnd;uint;long;ptr")
$wProcOld = _WinAPI_SetWindowLong(GUICtrlGetHandle($cInput), $GWL_WNDPROC, DllCallbackGetPtr($wProcNew))
_WinAPI_SetWindowLong(GUICtrlGetHandle($cInput2), $GWL_WNDPROC, DllCallbackGetPtr($wProcNew))
;_WinAPI_SetWindowLong(GUICtrlGetHandle($cInput3), $GWL_WNDPROC, DllCallbackGetPtr($wProcNew)) and so on

$DummyMenu      = GUICtrlCreateDummy()
$ContextMenu    = GUICtrlCreateContextMenu($DummyMenu)
$CommonMenuItem = GUICtrlCreateMenuItem("Common", $ContextMenu)
$FileMenuItem   = GUICtrlCreateMenuItem("File", $ContextMenu)
GUICtrlCreateMenuItem("", $ContextMenu)
$ExitMenuItem   = GUICtrlCreateMenuItem("Exit", $ContextMenu)


GUISetState(@SW_SHOW)
While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            ExitLoop
    EndSwitch
WEnd

Func do_clever_stuff_with_clipboard($hWnd)
    $sData = ClipGet()
    If @error Then Return 0;clip data is not text or clip empty
;do whatever
    $sData = StringUpper($sData)
;set text
    GUICtrlSetData(_WinAPI_GetDlgCtrlID($hWnd), $sData);or _GUICtrlEdit_SetText($hWnd, $sData)
    Return 1
EndFunc

; Show a menu in a given GUI window which belongs to a given GUI ctrl
Func ShowMenu($hWnd, $nContextID)
    Local $iSelected = _GUICtrlMenu_TrackPopupMenu(GUICtrlGetHandle($nContextID), $hWnd, -1, -1, -1, -1, 2)
    Switch $iSelected
        Case $CommonMenuItem
            ConsoleWrite("Common" & @LF)
        Case $FileMenuItem
            ConsoleWrite("File" & @LF)
        Case $ExitMenuItem
            ConsoleWrite("Exit" & @LF)
    EndSwitch
EndFunc

Func _MyWindowProc($hWnd, $uiMsg, $wParam, $lParam)
    Switch $uiMsg
        Case $WM_PASTE
            Return do_clever_stuff_with_clipboard($hWnd)
        Case $WM_CONTEXTMENU
            If $hWnd = GUICtrlGetHandle($cInput) Then
                ShowMenu($hGui, $ContextMenu)
                Return 0
            EndIf
        Case $WM_SETCURSOR
            GUICtrlSetCursor(_WinAPI_GetDlgCtrlID($hWnd), 5);;set Ibeam cursor
            Return 1;;and don't let default windowproc mess things up
    EndSwitch

;pass the unhandled messages to default WindowProc
    Return _CallWindowProc($wProcOld, $hWnd, $uiMsg, $wParam, $lParam)
 EndFunc
 
Func _CallWindowProc($lpPrevWndFunc, $hWnd, $Msg, $wParam, $lParam)
    Local $aRet = DllCall('user32.dll', 'int', 'CallWindowProc', 'ptr', $lpPrevWndFunc, 'hwnd', $hWnd, 'uint', $Msg, 'wparam', $wParam, 'lparam', $lParam)
    Return $aRet[0]
EndFunc

SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Share this post


Link to post
Share on other sites

Yea, and if you move the mouse along the input box, there's also this cursor flickering between normal and ibeam...

Not sure if it's proper (because right now I'm not sure what exactly is the root of the problem), but doing this works:

Func _MyWindowProc($hWnd, $uiMsg, $wParam, $lParam)
    Switch $uiMsg
        Case $WM_PASTE
            Return do_clever_stuff_with_clipboard($hWnd)
        Case $WM_CONTEXTMENU
            If $hWnd = GUICtrlGetHandle($cInput) Then
;ConsoleWrite("cm" & @LF)
                ShowMenu($hGui, $ContextMenu)
                Return 0
            EndIf
        Case $WM_SETCURSOR
            GUICtrlSetCursor(_WinAPI_GetDlgCtrlID($hWnd), 5);;set Ibeam cursor
            Return 1;;and don't let default windowproc mess things up
    EndSwitch

;pass the unhandled messages to default WindowProc
    Return _CallWindowProc($wProcOld, $hWnd, $uiMsg, $wParam, $lParam)
EndFunc

I hadn't noticed the cursor problem and I don't see why fixing that fixes my problem, but it does.

Thanks Siao :)


Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0