Jump to content

Case issue


Recommended Posts

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

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

Link to comment
Share on other sites

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

  • Moderators

StungStang,

This is how you do it: ;)

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

EndFunc

Now to explain how it works: :idiot:

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

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • Moderators

StungStang,

for you what is the best way to do this

If 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

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • Moderators

StungStang,

For multiple toolbar buttons you can do this - look for the <<<<<<<<<<<<< lines: :idiot:

#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

EndFunc

Are 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

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...