Sign in to follow this  
Followers 0

Bring up its context menu by left click of the button

7 posts in this topic

#1 ·  Posted (edited)

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Ý÷ ÚÚºÚ"µÍÙ[

"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)

for $x = 0 to UBound($menu)-1
    $label[$x] = GUICtrlCreateLabel("  " & $menu[$x],2,($x*$controlheight)+2,$width-1,$controlheight)

$Form1 = GUICreate("Form1", 633, 447, 193, 125)
$Button1 = GUICtrlCreateButton("Button1", 112, 160, $width, 33, 0)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
        Case $Button1
            $pos = MouseGetPos()
    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
                    $Colored = $Mouse[4] ;remember which was colored so we can uncolor it later

[based on custom tray item example :rolleyes: ]

Share this post

Link to post
Share on other sites

#4 ·  Posted (edited)

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)


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

; 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)
    DllCall("user32.dll", "int", "TrackPopupMenuEx", "hwnd", $hMenu, "int", 0, "int", $X, "int", $Y, "hwnd", $hWnd, "ptr", 0)

; 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
Edited by MsCreatoR

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

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

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
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 === *


AutoIt is simple, subtle, elegant. © AutoIt Team

Share this post

Link to post
Share on other sites

#5 ·  Posted (edited)

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)

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

MsCreatoR You was faster :)

Edited by Lazycat

Share this post

Link to post
Share on other sites

#6 ·  Posted (edited)

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)

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

*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

OMG...!!!!!! Each of u were gr8......[and of course a lot better than mine...I am newbie :rolleyes: )

Thanx for the support

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