Sign in to follow this  
Followers 0
DjDeep00

Contextmenu on a Edit Control

26 posts in this topic

I am sure this has been asked before but I couldnt find it searching the forum. Since an edit control has a default contextmenu like (copy,paste,undo..), not sure how you can add a new item to it?

Please advise.

Share this post


Link to post
Share on other sites



Bump....anyone please???

Share this post


Link to post
Share on other sites

Bump....anyone please???

Can someone atleast tell if this is possible or not?

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Hi,

Here is a litle bit changed example from help file:

#include <GuiConstants.au3>
$Gui = GUICreate("test")

$Edit = GUICtrlCreateEdit("", 20, 20)

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

GUISetState()

While 1
    $CurInfo = GUIGetCursorInfo($Gui)
    $Msg = GUIGetMsg()
    Select
        Case $Msg = -3
            Exit
        Case $CurInfo[3] = 1 And $CurInfo[4] = $Edit
            ShowMenu($Gui, $ContextMenu)
    EndSelect
WEnd

; 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

EDIT: Just changed the code a litle that the menu will be shown only when mouse hiovernig Edit control.

EDIT2: Now the menu shown under the mouse cursor, like it should be :)

Edited by MsCreatoR

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Share this post


Link to post
Share on other sites

Hi,

Here is a litle bit changed example from help file:

#include <GuiConstants.au3>
$Gui = GUICreate("test")

$Edit = GUICtrlCreateEdit("", 20, 20)

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

GUISetState()

While 1
    $CurInfo = GUIGetCursorInfo($Gui)
    $Msg = GUIGetMsg()
    Select
        Case $Msg = -3
            Exit
        Case $CurInfo[3] = 1 And $CurInfo[4] = $Edit
            ShowMenu($Gui, $ContextMenu)
    EndSelect
WEnd

; 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

EDIT: Just changed the code a litle that the menu will be shown only when mouse hiovernig Edit control.

EDIT2: Now the menu shown under the mouse cursor, like it should be :)

Thanks MsCreatoR, I really appreciate it.

Share this post


Link to post
Share on other sites

MsCreatoR,

Is it possible to keep the edit control's default contextmenu and also add your custom menu items?

-DjDeep

Share this post


Link to post
Share on other sites

DjDeep

Is it possible to keep the edit control's default contextmenu and also add your custom menu items?

I think that it would be mutch easyer to make your own such items :)

But combain together two menus, i don't think it is possible :) (i would be glad to be wrong).


 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Share this post


Link to post
Share on other sites

DjDeep

I think that it would be mutch easyer to make your own such items :)

But combain together two menus, i don't think it is possible :) (i would be glad to be wrong).

Ok..kool..I will make my own items and see how it goes...thanx again MsCreatoR.. :)

Share this post


Link to post
Share on other sites

I followed this example and it's helped dramatically. I am having a bit of a problem though. When I right click the edit control I want the menu on, sometimes it will show the windows cut, copy, paste, etc menu and sometime it will show MY menu. The other odd thing is once it DOES show my menu, it wont come back up again until I restart the program. It only shows the windows default menu. I have cut these portions out of my enormous code and not it seems that the menu won't show at all. Any ideas?

#include <GUIConstants.au3>
#include <A3LLibrary.au3>
;Global $sIniLocation = "M:\AdvisorConfig.ini"
Global $varMenuList
Global $varMenuArray

$Form1_1 = GUICreate("Advisor GUI", 998, 800, 10, 9, BitOR($WS_MINIMIZEBOX, $WS_SYSMENU, $WS_CAPTION, $WS_POPUPWINDOW, $WS_GROUP, $WS_HSCROLL, $WS_VSCROLL, $WS_BORDER, $WS_CLIPSIBLINGS, $WS_OVERLAPPEDWINDOW))
GUISetOnEvent($GUI_EVENT_MOUSEMOVE, "MouseWasMoved")
$InputValidationPositives = GUICtrlCreateInput("", 200, 72, 463, 21)
GUISetState(@SW_SHOW)

while 1
    sleep(100)
WEnd

#region PopUp Template Menus
Func MouseWasMoved()
    $CurInfo = GUIGetCursorInfo($Form1_1)
    Select
        Case $CurInfo[3] = 1 And $CurInfo[4] = $InputValidationPositives
                        #cs
                        ;I have commented out the following section to not rely on my external .ini file at the top.
                        ;and replaced it with the code following the comments.
                        Global $ContextMenu = GUICtrlCreateContextMenu()
                        Global $varMenuList = IniReadSection($sIniLocation, "ValidationPositives")
                        $varMenuLength = $varMenuList[0][0]
                        Global $varMenuArray[$varMenuLength+1]
                        $varMenuArray[0] = $varMenuList[0][0]

                        For $i = 1 to ubound($varMenuList) - 1
                            $varMenuID = GUICtrlCreateMenuItem ($varMenuList[$i][0], $ContextMenu)
                            $varMenuArray[$i] = $varMenuID
                            GUICtrlSetOnEvent($varMenuID,"InsertTemplateSelection")
                        Next
                        ShowMenu($Form1_1, $ContextMenu)
                        #ce
                        Global $ContextMenu = GUICtrlCreateContextMenu()
                        $varMenuID1 = GUICtrlCreateMenuItem ("item1", $ContextMenu)
                        $varMenuID2 = GUICtrlCreateMenuItem ("item2", $ContextMenu)
                        $varMenuID3 = GUICtrlCreateMenuItem ("item3", $ContextMenu)
                        $varMenuID4 = GUICtrlCreateMenuItem ("item4", $ContextMenu)
                        $varMenuID5 = GUICtrlCreateMenuItem ("item5", $ContextMenu)
                        ShowMenu($Form1_1, $ContextMenu)
    EndSelect
EndFunc

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 InsertTemplateSelection()

    $menuitem = @GUI_CTRLID
    $menunum = $menuitem - $varMenuArray[1] + 1
    ControlSend("Advisor GUI", "", _GuiCtrlGetFocus($Form1_1), String($varMenuList[$menunum][1]))
EndFunc



#endregion Popup Template Menus

Func _GuiCtrlGetFocus($GuiRef)
    $hwnd = _API_GetFocus()
    Local $result = DllCall("user32.dll", "int", "GetDlgCtrlID", "hwnd", $hwnd)
    Return $result[0]
EndFunc

Share this post


Link to post
Share on other sites

Solution from MsCreator is interesting but it will not catch Shift+F10.

More elegant is to use GUIRegisterMsg() & WM_CONTEXTMENU

but here is problem that this evemt is sent only from parent window.

In edit with it's default popupmenu it's handled in WndProc so there should be used Subclassing (with callback) 

and WM_CONTEXTMENU handled there.

Here is link with explanation from MSDN: link

Here is concept of catching WM_CONTEXTMENU by GUIRegisterMsg() - it's working only on main window

#include <GuiConstants.au3>

Global Const $WM_CONTEXTMENU = 0x007B

$Gui = GUICreate("test")
$Edit = GUICtrlCreateEdit("", 20, 20)
$hEdit = GUICtrlGetHandle($Edit)
$DummyMenu      = GUICtrlCreateDummy()
$ContextMenu    = GUICtrlCreateContextMenu($DummyMenu)
$CommonMenuItem = GUICtrlCreateMenuItem("Common", $ContextMenu)
$FileMenuItem   = GUICtrlCreateMenuItem("File", $ContextMenu)
GUICtrlCreateMenuItem("", $ContextMenu)
$ExitMenuItem   = GUICtrlCreateMenuItem("Exit", $ContextMenu)
GUISetState()
GUIRegisterMsg($WM_CONTEXTMENU, "MY_WM_CONTEXTMENU")

ConsoleWrite("gui " & $Gui  & @LF)
ConsoleWrite("edit " & $hEdit  & @LF)

While 1
;~     $CurInfo = GUIGetCursorInfo($Gui)
    $Msg = GUIGetMsg()
    Select
        Case $Msg = -3
            Exit
;~         Case $CurInfo[3] = 1 And $CurInfo[4] = $Edit
;~             ShowMenu($Gui, $ContextMenu)
    EndSelect
WEnd

Func MY_WM_CONTEXTMENU($hWnd, $Msg, $wParam, $lParam)
;~  ConsoleWrite("hwnd " & $hWnd & @LF)
;~  ConsoleWrite("wparam " & $wParam & @LF)
    If $hWnd = $Gui Then
        ShowMenu($Gui, $ContextMenu)
        Return
    EndIf
    Return $GUI_RUNDEFMSG
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
1 person likes this

Share this post


Link to post
Share on other sites

Solution from MsCreator is interesting but it will not catch Shift+F10.

More elegant is to use GUIRegisterMsg() & WM_CONTEXTMENU

but here is problem that this evemt is sent only from parent window.

In edit with it's default popupmenu it's handled in WndProc so there should be used Subclassing (with callback) 

and WM_CONTEXTMENU handled there.

Here is link with explanation from MSDN: link

Here is concept of catching WM_CONTEXTMENU by GUIRegisterMsg() - it's working only on main window

I'm not quite understanding...

I tried this before finding the one I am currently using and posted above. The problem was the same, that I could not attach it to a control, only the window.

The part I'm not quite getting is " so there should be used Subclassing (with callback) and WM_CONTEXTMENU handled there."

Not sure what WndProc is, or what you're meaning by subclassing. The SHIFT-F10 issue is not really an issue that I'm concerned about. I will take either approach as long as it works as should be expected, on right click in edit: present my menu.

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

I'm not quite understanding...

I tried this before finding the one I am currently using and posted above. The problem was the same, that I could not attach it to a control, only the window.

The part I'm not quite getting is " so there should be used Subclassing (with callback) and WM_CONTEXTMENU handled there."

Not sure what WndProc is, or what you're meaning by subclassing. The SHIFT-F10 issue is not really an issue that I'm concerned about. I will take either approach as long as it works as should be expected, on right click in edit: present my menu.

Look at Callback UDF

there is example for subclassing editbox

Edited by Zedna

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

I don't now how to do sub classing of a context menu.

But here is a very nasty way of making your own context menu for an edit box. It's not a pretty sight.

#include <GuiConstants.au3>
#include "DllCallBack.au3"
#include <misc.au3>

Global Const $WM_CONTEXTMENU = 0x007B
Global $showing = False
$Gui = GUICreate("test")
$Edit = GUICtrlCreateEdit("", 20, 20)
$hEdit = GUICtrlGetHandle($Edit)
$DummyMenu    = GUICtrlCreateDummy()
$ContextMenu    = GUICtrlCreateContextMenu($DummyMenu)
$CommonMenuItem = GUICtrlCreateMenuItem("Common", $ContextMenu)
$FileMenuItem   = GUICtrlCreateMenuItem("File", $ContextMenu)
GUICtrlCreateMenuItem("", $ContextMenu)
$ExitMenuItem   = GUICtrlCreateMenuItem("Exit", $ContextMenu)
GUISetState()
GUIRegisterMsg($WM_CONTEXTMENU, "MY_WM_CONTEXTMENU")

ConsoleWrite("gui " & $Gui  & @LF)
ConsoleWrite("edit " & $hEdit  & @LF)
$pTimerProc = _DllCallBack ("_TimerProc", "hwnd;uint;uint;dword")
$uiTimer = DllCall("user32.dll", "uint", "SetTimer", "hwnd", 0, "uint", 0, "int", 50, "ptr", $pTimerProc)
$uiTimer = $uiTimer[0]
Global $mp

While 1

    $Msg = GUIGetMsg()
    Select
        Case $Msg = -3
            _DllCallBack_Free ($pTimerProc)
            DllCall("user32.dll", "int", "KillTimer", "hwnd", 0, "uint", $uiTimer)
            Exit
    EndSelect
    If $showing Then
        DoFalseContextThing()
        
        $showing = False
    EndIf
    
WEnd
;need to add some code to detect clicking outside of this popup then delete the popup
Func DofalseContextThing()
;$mp = MouseGetPos()
    $choice = 0
    $cm = GUICreate("",10,10,$mp[0],$mp[1],$WS_POPUP,$WS_EX_TOPMOST)
    GUISetBkColor(0xfff666,$cm)
    $contextmenu    = GUICtrlCreateContextMenu ()

    $fileitem      = GUICtrlCreateMenuitem ("Open", $contextmenu)
    $saveitem      = GUICtrlCreateMenuitem ("Save", $contextmenu)
    
    $lone = GUICtrlCreateLabel("one",20,20)
    $ltwo = GUICtrlCreateLabel("two",20,50)
    GUISetState()
    MouseClick("right")
    While 1
        $cmsg = GUIGetMsg()
        If $cmsg = $fileitem Then
            $choice = 1
            ExitLoop
        EndIf
        If $cmsg = $saveitem Then
            $choice = 2
            ExitLoop
        EndIf
        If _IsPressed("01") Then ExitLoop
        WEnd
        GUIDelete($cm)
    MsgBox(0,"you chose",$choice)
    
EndFunc


Func MY_WM_CONTEXTMENU($hWnd, $Msg, $wParam, $lParam)
;~  ConsoleWrite("hwnd " & $hWnd & @LF)
    ConsoleWrite("wparam " & $wParam & @LF)
    If $hWnd = $Gui Then
        ShowMenu($Gui, $ContextMenu)
        Return
    EndIf
    Return $GUI_RUNDEFMSG
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 _TimerProc($hWnd, $uiMsg, $idEvent, $dwTime)
    If $showing Then Return
    If WinExists("[CLASS:#32768]") Then
        
        $CurInfo = GUIGetCursorInfo($Gui)
        If $CurInfo[4] = $Edit Then
            $mp = MouseGetPos()
    MouseMove($mp[0]-5,$mp[1] - 20,0)
    MouseClick("left");,$mp[0]-5,$mp[1] - 20)
    MouseMove($mp[0],$mp[1],0)
            
            $showing = True;
        ;ShowMenu($Gui, $ContextMenu)
            Return
        endif
    EndIf
EndFunc

EDIT: Changed to code which actually brings up a context menu.

Edited by martin

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

I don't now how to do sub classing of a context menu.

But here is a very nasty way of making your own context menu for an edit box. It's not a pretty sight.

EDIT: Changed to code which actually brings up a context menu.

I couldn't find out why, but there's a glitch with that :) Otherwise it's doing what I want. The glitch is that the user can't right click again in the edit ctrl

I tried doing this, but there's lots of problems (obviously, this is just a copy&paste adjustment)

#include <GuiConstants.au3>
#include <DllCallBack.au3>

Global Const $WM_COMMAND = 0x0111
Global Const $EN_CHANGE = 0x300
Global Const $EN_SETFOCUS = 0x100
Global Const $EN_KILLFOCUS = 0x200
Global Const $DebugIt = 1

Global $DEFAULTINPUTDATA = "Click and type something here..."
Global $NONEAACTIVECOLOR = 0x989898
Global $Input

_Main()

Func _Main()
    Global $pTimerProc = _DllCallBack ("_TimerProc", "hwnd;uint;uint;dword")
    Global $uiTimer = DllCall("user32.dll", "uint", "SetTimer", "hwnd", 0, "uint", 0, "int", 50, "ptr", $pTimerProc)
    Global $GUI
    Local $ExitButton, $y = 20
    $GUI = GUICreate("Check Inputs Demo", 280, 150)

    For $x = 0 To 2
        If IsArray($Input) Then
            ReDim $Input[UBound($Input) + 1]
        Else
            Dim $Input[1]
        EndIf
        $Input[UBound($Input) - 1] = GUICtrlCreateInput($DEFAULTINPUTDATA, 15, $y, 250, 20)
        GUICtrlSetColor(-1, $NONEAACTIVECOLOR)
        $y += 40
    Next

    $ExitButton = GUICtrlCreateButton("Exit", 110, 125, 60, 20)
    GUICtrlSetState(-1, $GUI_DEFBUTTON)

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

    GUISetState()
    GUIRegisterMsg($WM_COMMAND, "MY_WM_COMMAND")

    While 1
        Switch GUIGetMsg()
            Case $ExitButton, $GUI_EVENT_CLOSE
                ExitLoop
        EndSwitch
    WEnd
EndFunc  ;==>_Main

Func _Input_Changed($hControl)
   ;----------------------------------------------------------------------------------------------
    If $DebugIt Then _DebugPrint("Input Changed:" & GUICtrlRead($hControl))
   ;----------------------------------------------------------------------------------------------
EndFunc  ;==>_Input_Changed

Func _Input_SetFocus($hControl)
   ;----------------------------------------------------------------------------------------------
    If $DebugIt Then _DebugPrint("Input SetFocus")
   ;----------------------------------------------------------------------------------------------
    If GUICtrlRead($hControl) = $DEFAULTINPUTDATA Then GUICtrlSetData($hControl, "")
    GUICtrlSetColor($hControl, 0x000000)
EndFunc  ;==>_Input_SetFocus

Func _Input_KillFocus($hControl)
   ;----------------------------------------------------------------------------------------------
    If $DebugIt Then _DebugPrint("Input KillFocus")
   ;----------------------------------------------------------------------------------------------
    If Not StringLen(GUICtrlRead($hControl)) Then GUICtrlSetData($hControl, $DEFAULTINPUTDATA)
    GUICtrlSetColor($hControl, $NONEAACTIVECOLOR)
EndFunc  ;==>_Input_KillFocus

Func MY_WM_COMMAND($hWnd, $msg, $wParam, $lParam)
    Local $nNotifyCode = BitShift($wParam, 16); high word
    Local $nID = BitAND($wParam, 0xFFFF); low word
    Local $hCtrl = $lParam
   
    For $x = 0 To UBound($Input) - 1
        Switch $nID
            Case $Input[$x]
                Switch $nNotifyCode
                    Case $EN_CHANGE
                        _Input_Changed($nID)
                    Case $EN_SETFOCUS
                        ShowMenu($GUI,$nID, $ContextMenu)
                        _Input_SetFocus($nID)
                    Case $EN_KILLFOCUS
                        _Input_KillFocus($nID)
                EndSwitch
                ExitLoop
        EndSwitch
    Next
   ; Proceed the default Autoit3 internal message commands.
   ; You also can complete let the line out.
   ; !!! But only 'Return' (without any value) will not proceed
   ; the default Autoit3-message in the future !!!
    Return $GUI_RUNDEFMSG
EndFunc  ;==>MY_WM_COMMAND

Func _DebugPrint($s_Text, $line = @ScriptLineNumber, $file = @ScriptName)
    ConsoleWrite( _
        "!===========================================================" & @LF & _
        "+======================================================" & @LF & _
        "-->Line(" & StringFormat("%04d", $line) & "):" & @TAB & $s_Text & @LF & _
        "+======================================================" & @LF)
EndFunc  ;==>_DebugPrint

    

    
; Show a menu in a given GUI window which belongs to a given GUI ctrl
Func ShowMenu($hWnd, $CtrlID, $nContextID)
    Local $hMenu = GUICtrlGetHandle($nContextID)
    
    $arPos = ControlGetPos($hWnd, "", $CtrlID)
    
    Local $x = $arPos[0]
    Local $y = $arPos[1] + $arPos[3]
    
    ClientToScreen($hWnd, $x, $y)
    TrackPopupMenu($hWnd, $hMenu, $x, $y)
EndFunc


; Convert the client (GUI) coordinates to screen (desktop) coordinates
Func ClientToScreen($hWnd, ByRef $x, ByRef $y)
    Local $stPoint = DllStructCreate("int;int")
    
    DllStructSetData($stPoint, 1, $x)
    DllStructSetData($stPoint, 2, $y)

    DllCall("user32.dll", "int", "ClientToScreen", "hwnd", $hWnd, "ptr", DllStructGetPtr($stPoint))
    
    $x = DllStructGetData($stPoint, 1)
    $y = DllStructGetData($stPoint, 2)
   ; release Struct not really needed as it is a local 
    $stPoint = 0
EndFunc


; Show at the given coordinates (x, y) the popup menu (hMenu) which belongs to a given GUI window (hWnd)
Func TrackPopupMenu($hWnd, $hMenu, $x, $y)
    DllCall("user32.dll", "int", "TrackPopupMenuEx", "hwnd", $hMenu, "int", 0, "int", $x, "int", $y, "hwnd", $hWnd, "ptr", 0)
EndFunc

original: #391271 by GaryFrost


A decision is a powerful thing

Share this post


Link to post
Share on other sites

Ok, here is working solution using subclasing (you will need the DllCallback.au3 library from here):

#include <GuiConstants.au3>
#include <DllCallBack.au3>

Global Const $WM_CONTEXTMENU = 0x007B

$Gui = GUICreate("Disable Context Menu Demo")

$Edit = GUICtrlCreateEdit("", 20, 20)

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

GUISetState()

$WindowProcCallBack = _DllCallBack("_WindowProc", "hwnd;uint;long;ptr", 4)
$OriginalWindowProc = _WinSubclass(GUICtrlGetHandle($Edit), $WindowProcCallBack)

While 1
    $Msg = GUIGetMsg()
    Select
        Case $Msg = -3
            _DllCallBack_Free($OriginalWindowProc)
            Exit
    EndSelect
WEnd

; 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 _WindowProc($hWnd, $uiMsg, $wParam, $lParam)
    ; Disable the context menu and show your own menu
    If $uiMsg = $WM_CONTEXTMENU Then
        ShowMenu($Gui, $ContextMenu)
        Return 0
    EndIf
    
    ; Returning a Pointer to the original WindowProc means: We did not process this message.
    ; Do not call CallWindowProc() api yourself, the stub will do that for you!
    Return $OriginalWindowProc
EndFunc

Func _WinSubclass($hWnd, $lpNewWindowProc)
    ;#define GWL_WNDPROC (-4)
    Local $aTmp = DllCall("user32.dll", "ptr", "SetWindowLong", "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

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Share this post


Link to post
Share on other sites

Ok, here is working solution using subclasing (you will need the DllCallback.au3 library from here):

Goodness that's it!!!! Thank you thank you thank you :) and it's really easier than I thought!


A decision is a powerful thing

Share this post


Link to post
Share on other sites

Ok, here is working solution using subclasing (you will need the DllCallback.au3 library from here):

thanks again! this is really cool! I'm having fun with it and it's shown me how to use subclassing! ... haha I think


A decision is a powerful thing

Share this post


Link to post
Share on other sites

#18 ·  Posted (edited)

Ok, here is working solution using subclasing (you will need the DllCallback.au3 library from here):

Very nice example MsCreatoR!

Thanks for sharing :-)

Maybe send this to piccaso so he can use it as reference example number two for subclassing with his CallBack UDF

Edited by Zedna

Share this post


Link to post
Share on other sites

Very nice example MsCreatoR!

Thanks for sharing :-)

Maybe send this to piccaso so he can use it as reference example number two for subclassing with his CallBack UDF

I second/echo Zedna's suggestion and gratefulness :)


A decision is a powerful thing

Share this post


Link to post
Share on other sites

Zedna

Very nice example MsCreatoR!

Thanks.

Maybe send this to piccaso so he can use it as reference example number two for subclassing with his CallBack UDF

I have post a suggestion about editing the current example by changing the line with return check (first line in function _WindowProc()... but maybe i should tell him about this example to...

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

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