StungStang Posted February 14, 2011 Share Posted February 14, 2011 Hi to all...I want use a toolbar in my application...all work ok but i have a question... Why i cant use a Case with _GUICtrlToolbar_IsButtonPressed ? I know that i can use the If statement...but the Case would be faster and more convenient. How i can know if a toolbar button was pressed, using a Case? Hi to all Link to comment Share on other sites More sharing options...
Mat Posted February 14, 2011 Share Posted February 14, 2011 Case can apply in two ways... Switch or Select. If you are using select then I see no reawason why it would not work. Select Case _GUICtrlToolbar_IsButtonPressed($hToolbar, $iCmdId) MsgBox(0, "Test", "Button is pressed") EndSelect You'll need to provide a better example of what you are trying to do I'm afraid. Mat AutoIt Project Listing Link to comment Share on other sites More sharing options...
StungStang Posted February 14, 2011 Author Share Posted February 14, 2011 This is my example #include <ButtonConstants.au3> #include <GuiConstantsEx.au3> #include <WindowsConstants.au3> #include <Constants.au3> #include <GuiToolbar.au3> Global Enum $idNew = 1000 #Region ### START Koda GUI section ### Form= $Form1 = GUICreate("Form1", 116, 76, 192, 124) $ToolBar1 = _GUICtrlToolbar_Create($Form1, 0) $Button1 = GUICtrlCreateButton("Button1", 24, 48, 75, 25, $WS_GROUP) GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### _GUICtrlToolbar_AddBitmap ($Toolbar1, 1, -1, $IDB_STD_LARGE_COLOR) _GUICtrlToolbar_AddButton ($ToolBar1 , $idNew, $STD_FILENEW) While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $idNew ;Do what u want EndSwitch WEnd I want that when i press the button $idnew it do something i want...but it dont work =P...how can i know the button pressed with "Case". Hi! Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted February 14, 2011 Moderators Share Posted February 14, 2011 StungStang,This is how you do it: expandcollapse popup#include <ButtonConstants.au3> #include <GuiConstantsEx.au3> #include <WindowsConstants.au3> #include <Constants.au3> #include <GuiToolbar.au3> Global Enum $idNew = 1000 #Region ### START Koda GUI section ### Form= $Form1 = GUICreate("Form1", 116, 76, 192, 124) $ToolBar1 = _GUICtrlToolbar_Create($Form1, 0) $Button1 = GUICtrlCreateButton("Button1", 24, 48, 75, 25, $WS_GROUP) $hDummy = GUICtrlCreateDummy() GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### _GUICtrlToolbar_AddBitmap ($Toolbar1, 1, -1, $IDB_STD_LARGE_COLOR) _GUICtrlToolbar_AddButton ($ToolBar1 , $idNew, $STD_FILENEW) ; Register WM_COMMAND messages GUIRegisterMsg($WM_COMMAND, "My_WM_COMMAND") While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $Button1 MsgBox(0, "Pressed", "Button 1") Case $hDummy ;Do what u want MsgBox(0, "Pressed", "Toolbar Buton") EndSwitch WEnd Func My_WM_COMMAND($hWnd, $iMsg, $wParam, $lParam) ; Check if CmdID is toolbar button If _WinAPI_LoWord($wParam) = $idNew Then GUICtrlSendToDummy($hDummy) EndFuncNow to explain how it works: You need to intercept the message sent when the toolbar button is pressed. AutoIt does this automatically for its native buttons with GUIGetMsg, but for a UDF button you need to do it yourself. You intercept the message by registering the WM_COMMAND mesages sent by your GUI - using GUIRegisterMsg. There is a good tutorial tutorial in the Wiki if you want to know more about this command.GUIRegisterMsg tells AutoIt to run the My_WM_COMMAND function when it gets a WM_COMMAND message - this function is known as a message handler. In this function we test to see if it was the toolbar button which was pressed by looking at the CmdID of the button which sent the message - it is in $wParam but we have to extract it with _WinAPI_LoWord.Now it is a good idea to get out of a message handler as soon as possible and you must absolutely not run any form of blocking function like a MsgBox, so rather than do whatever you want to do in the handler, we use another little trick . I created a dummy control ($hDummy)in your main GUI - it is an invisible control, but AutoIt treats it just like a button. Now when the toolbar button is pressed we use the message handler function to action this dummy control using GUICtrlSendToDummy. Then the normal GUIGetMsg loop detects that the dummy control has been actioned and fires whatever you want to do.All clear? Please ask if you have any questions. M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
StungStang Posted February 14, 2011 Author Share Posted February 14, 2011 All clear...only a question...for you what is the best way to do this...(case,if,ecc...)? Hi! Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted February 14, 2011 Moderators Share Posted February 14, 2011 StungStang,for you what is the best way to do thisIf you mean the GUIGetMsg loop, I prefer to use Switch with Case statements. I find it makes for easy-to-read and easy-to-maintain code. If not, what? M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
StungStang Posted February 14, 2011 Author Share Posted February 14, 2011 I mean the best way to know what toolbar button was pushed...for standard button switch...case is the best..But for the Toolbar? I thinks that if...then is slow and boring...you agree? Hi =) Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted February 14, 2011 Moderators Share Posted February 14, 2011 StungStang,For multiple toolbar buttons you can do this - look for the <<<<<<<<<<<<< lines: expandcollapse popup#include <ButtonConstants.au3> #include <GuiConstantsEx.au3> #include <WindowsConstants.au3> #include <Constants.au3> #include <GuiToolbar.au3> Global Enum $idNew = 1000, $idOpen, $idSave #Region ### START Koda GUI section ### Form= $Form1 = GUICreate("Form1", 116, 76, 192, 124) $ToolBar1 = _GUICtrlToolbar_Create($Form1, 0) $Button1 = GUICtrlCreateButton("Button1", 24, 48, 75, 25, $WS_GROUP) $hDummy = GUICtrlCreateDummy() GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### _GUICtrlToolbar_AddBitmap ($Toolbar1, 1, -1, $IDB_STD_LARGE_COLOR) _GUICtrlToolbar_AddButton ($ToolBar1 , $idNew, $STD_FILENEW) _GUICtrlToolbar_AddButton ($ToolBar1, $idOpen, $STD_FILEOPEN) _GUICtrlToolbar_AddButton ($ToolBar1, $idSave, $STD_FILESAVE) ; Register WM_COMMAND messages GUIRegisterMsg($WM_COMMAND, "My_WM_COMMAND") While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $Button1 MsgBox(0, "Pressed", "Button 1") Case $hDummy ; Read the code sent to the Dummy control $iCmdID = GUICtrlRead($hDummy) ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< MsgBox(0, "Pressed", "Toolbar Button with ID: " & $iCmdID) EndSwitch WEnd Func My_WM_COMMAND($hWnd, $iMsg, $wParam, $lParam) ; Check if CmdID is toolbar button Local $iCmdID = _WinAPI_LoWord($wParam) Switch $iCmdID Case $idNew To $idSave ; Send the CmdID to the Dummy control GUICtrlSendToDummy($hDummy, $iCmdID) ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< EndSwitch EndFuncAre you sure you know what is going on in this script? Are you happy with the GUIRegisterMsg command and the message handler? Please ask if you have any questions. M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
Mat Posted February 14, 2011 Share Posted February 14, 2011 Just to add to Melba's above post, it is then very easy to add a Switch ... Case ... EndSwitch in the message loop where the MsgBox is now. I think thats what you are looking to do. AutoIt Project Listing Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now