Jump to content
Sign in to follow this  
rohit

Bring up its context menu by left click of the button

Recommended Posts

Can i bring up the context menu [or in any way a context] of a button by the primary click of a the button ??? :)

Edited by rohit

Share this post


Link to post
Share on other sites

Can i bring up the context menu [or in any way a context] of a button by the primary click of a the button ??? :)

As far as I know it is not possible to bring up a context menu using the primary button click. Well not without intercepting the click and substituting a command to display the context menu.

You can use either of these methods display a context menu if one has been defined for the application

MouseClick("secondary")oÝ÷ ÚÚºÚ"µÍÙ[
    ][ÝÞÐTÒÑV_I][ÝÊ

"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to build bigger and better idiots. So far, the universe is winning."- Rick Cook

Share this post


Link to post
Share on other sites

Ya....I just figured it how... Not exactly context menu .... but something similar and more customizable :)

#include <GUIConstants.au3>
Dim $menu[3] = ["Resouce Hacker","Koda Designer","VB 6"]
Dim $controlheight = 20
Dim $BgColor = 0x293029 ; Background color
Dim $EdgeColor = 0x6B6963 ; Edge color
Dim $ActiveColor = 0x948639 ; Active menu color
Dim $FontColor = 0xFFFFFF

Dim $Width = 180

Dim $height = UBound($menu)*$controlheight
Dim $colored,$label[UBound($menu)]

Dim $context = GUICreate("context",$width+3,$height+4,-1,-1,$ws_popup,$ws_ex_toolwindow)
GUISetFont(9,300,0,"verdana",$context)
GUISetBkColor($edgecolor)

for $x = 0 to UBound($menu)-1
    $label[$x] = GUICtrlCreateLabel("  " & $menu[$x],2,($x*$controlheight)+2,$width-1,$controlheight)
    GUICtrlSetBkColor(-1,$bgcolor)
    GUICtrlSetColor(-1,$fontcolor)
Next
WinSetOnTop($context,"",1)

#Region
$Form1 = GUICreate("Form1", 633, 447, 193, 125)
$Button1 = GUICtrlCreateButton("Button1", 112, 160, $width, 33, 0)
GUISetState(@SW_SHOW,$Form1)
#EndRegion

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            $pos = MouseGetPos()
            WinMove($context,"",$pos[0]-($width/2),$pos[1])
            GUISetState(@sw_show,$context)
            WinActivate($context)
        EndSwitch
    If BitAND(WinGetState($context), 2) Then
        $Pos = MouseGetPos()
        $wPos = WinGetPos($context)
        If ($Pos[0] >= $wPos[0] And $Pos[0] <= $wPos[0] + $Width) And _
            ($Pos[1] >= $wPos[1] And $Pos[1] <= $wPos[1] + $Height) Then
            $Mouse = GUIGetCursorInfo() ; Color the active tray items
            If not @error Then
                If $Mouse[4] <> $Colored Then
                    GUICtrlSetBkColor($Colored,$BgColor)
                    GUICtrlSetBkColor($Mouse[4],$ActiveColor)
                    $Colored = $Mouse[4] ;remember which was colored so we can uncolor it later
                EndIf
            EndIf
        Else
            GUISetState(@SW_HIDE,$context)
        EndIf
    EndIf
WEnd

[based on custom tray item example :rolleyes: ]

Share this post


Link to post
Share on other sites

Can i bring up the context menu [or in any way a context] of a button by the primary click of a the button

Sure, there is example in the help file, but here is a little(?) modified example:

#include <GuiConstants.au3>

$Gui = GuiCreate("Context Menu on Button")

$Button = GUICtrlCreateButton("Show Menu", 20, 40, 80, 20)

$DummyMenu = GUICtrlCreateDummy()
$My_ContextMenu = GUICtrlCreateContextMenu($DummyMenu)
$My_Item = GUICtrlCreateMenuItem("Some Item", $My_ContextMenu)
$Exit_Item = GUICtrlCreateMenuItem("Exit", $My_ContextMenu)

GUISetState()

While 1
    $Msg = GUIGetMsg()
    Switch $Msg
        Case $GUI_EVENT_CLOSE, $Exit_Item
            Exit
        Case $Button
            ShowMenu($Gui, $My_ContextMenu, $Button)
    EndSwitch
WEnd

; Show a menu in a given GUI window which belongs to a given GUI ctrl
Func ShowMenu($hWnd, $nContextID, $nContextControlID, $iMouse=0)
    Local $hMenu = GUICtrlGetHandle($nContextID)
    Local $iCtrlPos = ControlGetPos($hWnd, "", $nContextControlID)
    
    Local $X = $iCtrlPos[0]
    Local $Y = $iCtrlPos[1] + $iCtrlPos[3]
    
    ClientToScreen($hWnd, $X, $Y)
    
    If $iMouse Then
        $X = MouseGetPos(0)
        $Y = MouseGetPos(1)
    EndIf
    
    DllCall("user32.dll", "int", "TrackPopupMenuEx", "hwnd", $hMenu, "int", 0, "int", $X, "int", $Y, "hwnd", $hWnd, "ptr", 0)
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
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

With standard menu + GUIMenu UDF:

#include <GUIConstants.au3>
#include <GuiMenu.au3>

$hGUI = GUICreate("GUI")
$hDummy = GUiCtrlCreateDummy()
$hButton = GUICtrlCreateButton("Click me", 80, 56, 75, 25, 0)
$hContextMenu = GUICtrlCreateContextMenu($hDummy)
$MenuItem1 = GUICtrlCreateMenuItem("MenuItem1", $hContextMenu)
$MenuItem2 = GUICtrlCreateMenuItem("MenuItem2", $hContextMenu)
$MenuItem3 = GUICtrlCreateMenuItem("MenuItem3", $hContextMenu)
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $hButton
            _GUICtrlMenu_TrackPopupMenu(GUICtrlGetHandle($hContextMenu), $hGUI)
        Case $MenuItem1
            MsgBox (0, "", "First item clicked")
    EndSwitch
WEnd

MsCreatoR You was faster :)

Edited by Lazycat

Share this post


Link to post
Share on other sites

Wow... that seems... needlessly complex. ** This was aimed at post #3.

#include <GUIConstantsEx.au3>

$gui = GUICreate('', 200, 200)
$bt = GUICtrlCreateButton('Button!', 50, 50, 100, 100)
$cm = GUICtrlCreateContextMenu($bt)
GUICtrlCreateMenuItem('One', $cm)
GUICtrlCreateMenuItem('Two', $cm)
GUICtrlCreateMenuItem('Three', $cm)
$cmsub = GUICtrlCreateMenu('...', $cm)
GUICtrlCreateMenuItem('Four', $cmsub)
GUICtrlCreateMenuItem('Five', $cmsub)
GUICtrlCreateMenuItem('Six', $cmsub)
GUISetState()

While 1
    $gm = GUIGetMsg()
    Switch $gm
        Case $GUI_EVENT_CLOSE
            ExitLoop
        Case $bt
            ControlClick($gui, '', $bt, 'right')
    EndSwitch
WEnd

*Edit: Damnit, I just don't learn, I really need to start refreshing topics before replying to them.

Edited by Saunders

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  

×
×
  • Create New...