Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

MsgBox Generator v1.3


  • Please log in to reply
60 replies to this topic
r0lZ
  • Members
  • 192 posts
  • Last active: Feb 11 2012 11:19 PM
  • Joined: 21 Apr 2007
I wrote this simple script because I'm tired to have to consult the AutoHotkey doc and draw my calculator each time I need to build a nice-looking MsgBox.

The script opens a small GUI where you can select the options you want for your MsgBox, then test it and copy the generated code to the clipboard or insert it directly in your text editor with Control-Shift-M.

I think it might be useful to others, so I've posted it here.

Here is the GUI, and the code generated by the script for that settings:
Posted Image
MsgBox, 0x44131, %A_ScriptName%, Simple test.`nKeep cool!, 10
IfMsgBox, OK
{

}
IfMsgBox, Cancel
{

}
IfMsgBox, Timeout
{

}

The MsgBoxGenerator.ahk script:
; MsgBoxGenerator.ahk: AutoHotkey script by r0lZ, September 2011
; Generates the AutoHotkey code to build easily a nice-looking MsgBox.
; Developed vith AutoHotkey_L v1.1.02.01 Unicode X64 under Win7 X64 SP1
; Discussion thread: http://www.autohotkey.com/forum/topic76419.html
;
; Version history:
; v1.0:
; - First release
; v1.1:
; - Bug: The default button numbers that cannot be selected due to the current
;   buttons type selection are now disabled.
; - Added the currently selected icon in the status bar.
; - Test button: When the "Help" button option is selected, clicking the Help
;   button of the messagebox displays now a dummy help message.
; - Added the possibility to input an options number to modify the GUI accordingly.
; - Added the Right-To-Left alignment option.
; - Added the "Default Desktop" (undocumented?) window type option.
; v1.2:
; - Bug: Right-to-left was "Left-to-right"
; - Some modifications inspired by the mod version released by Drugwash:
; - Changed the Icon listbox to a listview to show the icons in the GUI.
; - The Timeout value combobox is now disabled when the Timeout checkbox is not ticked.
; - The option value is now in hexadecimal format.
; - GUI somewhat reorganised.
; v1.3:
; - Fixed a cosmetic bug under Win XP: the "Icon" listview was too small.
; - The Default Desktop window option is now a checkbox and can be combined with another window option.

VERSION = 1.3

#NoTrayIcon
#NoEnv
#SingleInstance force
#UseHook off
#Persistent
DetectHiddenWindows, on

SplitPath, A_ScriptName, , , , APPTITLE

IncludeBrackets = 1

Menu, OptionsMenu, Add, Always on top, ToggleAlwaysOnTop
Menu, OptionsMenu, add, Show tray tips, ToggleTrayTips
Menu, OptionsMenu, Add, Enable Ctrl-Shift-M hotkey, ToggleSuspend
Menu, OptionsMenu, Check, Enable Ctrl-Shift-M hotkey
Menu, OptionsMenu, Add, Include brackets in code, ToggleBrackets
Menu, OptionsMenu, Check, Include brackets in code

Menu, Tray, NoStandard
Menu, Tray, Add, Open GUI, OpenMainGui
Menu, Tray, Default, Open GUI
Menu, Tray, Add
Menu, Tray, Add, Options, :OptionsMenu
Menu, Tray, Add
Menu, Tray, Add, Exit, Exit
Menu, Tray, Click, 1
Menu, Tray, Tip, AHK MsgBox generator`nBy r0lZ`, September 2011

if (! A_IsCompiled) {
    tmp = %A_ScriptDir%\%APPTITLE%.ico
    if (FileExist(tmp))
        Menu, Tray, Icon, %tmp%
    else
        Menu, Tray, Icon
    tmp =
} else
    Menu, Tray, Icon

BuildMainGUI()

OnExit, CleanExit


Return


Exit:
    OnExit
CleanExit:
    FileDelete, %A_Temp%\MsgBoxTest.ahk
    ExitApp
return


; **************************** Main GUI ***********************
OpenMainGui:
    Gui, Show
return

BuildMainGUI()
{
    Global VERSION
    Global ButtonsVal, DefaultButtonVal, HelpButton, IconVal, WindowVal, DefaultDesktop, RightJustify, RightToLeft, Title, Text, TimeoutOn, Timeout, TimeoutS

    Menu, GuiMenu, add, Exit, Exit
    Menu, GuiMenu, Add, Options, :OptionsMenu
    Menu, GuiMenu, add, Input#, InputNumber
    Gui, Menu, GuiMenu

    Gui, Margin, 4, 4
    Gui, Font, S8, Tahoma

    ; top left zone

    Gui, Add, Text, X4 Y4 Section, Buttons scheme: 
    Gui, Add, ListBox, XS vButtonsVal r7 w200 gGuiChanged AltSubmit, OK|OK / Cancel|Abort / Retry / Ignore|Yes / No / Cancel|Yes / No|Retry / Cancel|Cancel / Try Again / Continue [WinXP+]
    GuiControl, Choose, ButtonsVal, 1

    Gui, Add, Text, XS Section, Default Button: 
    Gui, Add, Radio, YS vDefaultButtonVal Checked gGuiChanged, &1
    Gui, Add, Radio, YS gGuiChanged, &2
    Gui, Add, Radio, YS gGuiChanged, &3
    
    Gui, Add, Text, XS W202 H2 Section 0x10

    Gui, Add, Checkbox, XS Section vHelpButton gGuiChanged, Add '&Help' button
    Gui, Font, s7, Tahoma
    Gui, Add, Text, XS+16 Disabled, Requires: Gui +OwnDialogs  and`nOnMessage(0x53, "WM_HELP")
    Gui, Font, S8, Tahoma

    Gui, Add, Text, XS W202 H2 Section 0x10

    Gui, Add, Text, XS Section, Text:
    Gui, Add, Checkbox, YS vRightJustify gGuiChanged, &Right-justify
    Gui, Add, Checkbox, YS vRightToLeft  gGuiChanged, Right-to-&left

    Gui, Add, Text, XS W202 H6 Section 0x10

    ; top right zone

    Gui, Add, Text, X208 Y4 Section, Icon and sound:
    Gui, Add, ListView, XS vIconVal r5 w98 h95 Count5 -hdr -Multi -WantF2 gGuiChanged AltSubmit +0x8, Type   ; 0x8 = LVS_SHOWSELALWAYS
    if (FileExist(A_WinDir . "\system32\user32.dll")) {
        hIL := IL_Create(5)
        IL_Add(hIL, "user32.dll", 5)
        IL_Add(hIL, "user32.dll", 2)
        IL_Add(hIL, "user32.dll", 3)
        IL_Add(hIL, "user32.dll", 4)
        LV_SetImageList(hIL)
        LV_Add("Icon99", "No icon")
        LV_Add("Icon1", "Info")
        LV_Add("Icon2", "Warning")
        LV_Add("Icon3", "Question")
        LV_Add("Icon4", "Error")
    } else {
        LV_Add("", "No icon")
        LV_Add("", "Info")
        LV_Add("", "Warning")
        LV_Add("", "Question")
        LV_Add("", "Error")
    }
    
    LV_ModifyCol(0, "AutoHdr")
    LV_Modify(1, "Select Focus")

    Gui, Add, Text, XS Section, Window options:
    Gui, Add, ListBox, XS vWindowVal r4 w98 gGuiChanged AltSubmit, Normal|Task modal|System modal|Always on top
    GuiControl, Choose, WindowVal, 1

    Gui, Add, Checkbox, XS Section vDefaultDesktop gGuiChanged, &Default desktop

    ; bottom zone

    Gui, Add, Text, X4 Section W30, Title: 
    Gui, Add, Edit, YS-4 w268 vTitle gGuiChanged -WantReturn, `%A_ScriptName`%

    Gui, Add, Edit, XS Section w302 vText r4 gGuiChanged +WantReturn, Text

    Gui, Add, Text, XS W303 H4 Section 0x10

    Gui, Add, Checkbox, XS Section vTimeoutOn gGuiChanged, Ti&meout:
    Gui, Add, Edit, w70 YS-3 Number Limit7 gGuiChanged vTimeout Disabled -WantReturn
    Gui, Add, UpDown, vTimeoutS Range1-60 gGuiChanged Disabled, 10
    Gui, Add, Text, YS, seconds   (Max 2147483 secs.)

    Gui, Add, Text, XS W303 H0 Section 0x10

    Gui, Add, Text, XS Section, Control-Shift-M inserts the code in the current application.
    Gui, Add, Button, XS w98 gCopyNum Section, Copy &option #
    Gui, Add, Button, YS w98 gCopyCode, Copy &code
    Gui, Add, Button, YS w98 gTest Default, &Test

    Gui, Add, StatusBar

    GoSub, GuiChanged

    SysGet, Mon, MonitorWorkArea
    posx := MonLeft + 20
    posy := MonTop + 20
    Gui, Show, X%posx% Y%posy%, AHK MsgBox generator v%VERSION%
}

GuiChanged:
    Gui, Submit, NoHide
    val := ButtonsVal -1
    if (val == 0) {
        GuiControl, Disable, &2
        GuiControl, Disable, &3
        DefaultButtonVal = 1
        GuiControl, , DefaultButtonVal, 1
    }
    else if (val == 1 || val == 4) {
        GuiControl, Enable, &2
        GuiControl, Disable, &3
        if (DefaultButtonVal > 2) {
            DefaultButtonVal = 2
            GuiControl, , &2, 1
        }
    } else {
        GuiControl, Enable, &2
        GuiControl, Enable, &3
    }
    val += (DefaultButtonVal - 1) * 256
    val += HelpButton ? 16384 : 0

    IconVal := LV_GetNext()
    if (IconVal != 1) {
        val += (6 - IconVal) * 16
        if (iconval == 2)
            iconnum = 5
        else if (iconval == 3)
            iconnum = 2
        else if (iconval == 4)
            iconnum = 3
        else
            iconnum = 4
    } else
        iconnum = 0


    if (WindowVal == 2)
        val += 8192
    else if (WindowVal == 3)
        val += 4096
    else if (WindowVal == 4)
        val += 262144

    if (DefaultDesktop)
        val += 131072

    if RightJustify
        val += 524288
    if RightToLeft
        val += 1048576

    ; convert options value to hex
    SetFormat, Integer, H
    val+=0
    SetFormat, Integer, D

    StringReplace, Title, Title, `, , ```, , 1
    StringReplace, Message, Text, `, , ```, , 1 
    StringReplace, Message, Message, `n, ``n, 1 
    st = MsgBox, %val%, %Title%, %Message%

    if (TimeoutOn && Timeout <= 0) {
        TimeoutOn = 0
        GuiControl, , TimeoutOn, 0
        GuiControl, , Timeout, 1
        soundbeep
    }
    if (TimeoutOn) {
        GuiControl, Enable, Timeout
        GuiControl, Enable, TimeoutS
        st = %st%, %Timeout%
    } else {
        Timeout = 0
        GuiControl, Disable, Timeout
        GuiControl, Disable, TimeoutS
    }

    if (iconnum && FileExist(A_WinDir . "\system32\user32.dll")) {
        SB_SetParts(22)
        SB_SetIcon("user32.dll", iconnum)
    } else {
        SB_SetParts(0)
    }
    SB_SetText(st, 2)
    if (ShowTrayTips) {
        GoSub, GenCode
        TrayTip, MsgBox code:, %code%, 10, 1
    }
return

CopyNum:
    GoSub, GuiChanged
    Clipboard = %val%
return

CopyCode:
    GoSub, GuiChanged
    GoSub, GenCode
    clipboard = %code%
return

GenCode:
    code = MsgBox, %val%, %Title%, %Message%
    if (IncludeBrackets)
        s = `n{`n`n}
    else
        s = `n
    if (TimeoutOn)
        code = %code%, %Timeout%
    if (ButtonsVal == 1 && TimeoutOn)
        code = %code%`nIfMsgBox, OK%s%
    if (ButtonsVal == 2)
        code = %code%`nIfMsgBox, OK%s%`nIfMsgBox, Cancel%s%
    if (ButtonsVal == 3)
        code = %code%`nIfMsgBox, Abort%s%`nIfMsgBox, Retry%s%`nIfMsgBox, Ignore%s%
    if (ButtonsVal == 4)
        code = %code%`nIfMsgBox, Yes%s%`nIfMsgBox, No%s%`nIfMsgBox, Cancel%s%
    if (ButtonsVal == 5)
        code = %code%`nIfMsgBox, Yes%s%`nIfMsgBox, No%s%
    if (ButtonsVal == 6)
        code = %code%`nIfMsgBox, Retry%s%`nIfMsgBox, Cancel%s%
    if (ButtonsVal == 7)
        code = %code%`nIfMsgBox, Cancel%s%`nIfMsgBox, Try Again%s%`nIfMsgBox, Continue%s%
    if (TimeoutOn)
        code = %code%`nIfMsgBox, Timeout%s%
    code = %code%`n
return

Test:
    GoSub, GuiChanged
    cmd = MsgBox, %val%, %Title%, %Message%
    if TimeoutOn
        cmd = %cmd%, %Timeout%
    FileDelete, %A_Temp%\MsgBoxTest.ahk
    if (HelpButton) {
        code = Gui, Show, Hide, %Title%`nGui, +OwnDialogs`nOnMessage(0x53, "WM_HELP")`n%cmd%`nexitapp`nWM_HELP()`n{`n MsgBox, 64, Help - %Title%, This is a dummy help message., 3`n}`n
        FileAppend, %code%`n, %A_Temp%\MsgBoxTest.ahk
        code =
    } else {
        FileAppend, %cmd%`n, %A_Temp%\MsgBoxTest.ahk
    }   
    Run, "%A_Temp%\MsgBoxTest.ahk", %A_Temp%, UseErrorLevel
return

GuiClose:
GuiEscape:
    GoSub, GuiChanged
    Gui, Hide
return

ToggleAlwaysOnTop:
    alwaysontop := ! alwaysontop
    Menu, OptionsMenu, ToggleCheck, Always on top
    if (alwaysontop)
        Gui, +AlwaysOnTop
    else
        Gui, -AlwaysOnTop
return

ToggleSuspend:
    Suspend, Toggle
    Menu, OptionsMenu, ToggleCheck, Enable Ctrl-Shift-M hotkey
return

ToggleTrayTips:
    ShowTrayTips := ! ShowTrayTips
    Menu, OptionsMenu, ToggleCheck, Show tray tips
    if (ShowTrayTips)
        GoSub, GuiChanged
    else
        TrayTip
return

ToggleBrackets:
    IncludeBrackets := ! IncludeBrackets
    Menu, OptionsMenu, ToggleCheck, Include brackets in code
    if (ShowTrayTips)
        GoSub, GuiChanged
return

^+m::
    GoSub, GuiChanged
    GoSub, GenCode
    SetKeyDelay, 0
    SendRaw, %code%
return

; **************** GUI 2: Input an option number **************************

InputNumber:
    GoSub, GuiChanged
    num := InputNumber(val)
return

InputNumber(defaultnum)
{
    Global InputNum

    Gui, 1:+Disabled
    Gui, 2:Default
    Gui, +Owner1

    Gui, -MinimizeBox -MaximizeBox

    Gui, Add, Text, Section, MsgBox Options value:

    Gui, Add, Edit, Limit7 Number vInputNum Section w110 -WantReturn, %defaultnum% 

    Gui, Add, Button, XS w50 g2GuiOK Section Default, &OK
    Gui, Add, Button, YS w50 g2GuiClose, &Cancel
    
    Gui, 1:+LastFound
    WinGetPos, posx, posy
    posx += 80
    posy += 50
    Gui, Show, X%posx% Y%posy%, Input#
}

2GuiOK:
    Gui, 2:Submit, NoHide
    Gui, 1:Default

    GuiControl, Enable, &2
    GuiControl, Enable, &3
    GuiControl, Choose, ButtonsVal, 3
    val := round((InputNum & 0x300) / 0x100) + 1
    GuiControl, , &%val%, 1

    val := (InputNum & 7) + 1
    if (val > 7) {
        val = 1
        soundbeep
    }
    GuiControl, Choose, ButtonsVal, %val%

    GuiControl, , HelpButton, % round((InputNum & 0x4000) / 0x4000)

    val := round((InputNum & 0x70) / 0x10)
    if (val == 0)
        LV_Modify(1, "Select Focus")
    else
        LV_Modify(6 - val, "Select Focus")

    val := InputNum & 0x63000
    if (val == 0)
        GuiControl, Choose, WindowVal, 1
    else if (val == 0x1000)
        GuiControl, Choose, WindowVal, 3
    else if (val == 0x2000)
        GuiControl, Choose, WindowVal, 2
    else if (val == 0x40000)
        GuiControl, Choose, WindowVal, 4
    else if (val == 0x20000)
        GuiControl, Choose, WindowVal, 5
    else {
        Soundbeep
        GuiControl, Choose, WindowVal, 1
    }

    GuiControl, , RightJustify, % round((InputNum & 0x80000) / 0x80000)
    GuiControl, , RightToLeft,  % round((InputNum & 0x100000) / 0x100000)

    GoSub, 2GuiClose
    GoSub, GuiChanged
return

2GuiEscape:
2GuiClose:
    Gui, 2:Destroy
    Gui, 1:-Disabled
    Gui, 1:Default
    Gui, 1:+LastFound
    WinActivate
return
[EDIT] Updated to v1.3 (See below for changelog)
r0lZ

Zod
  • Members
  • 214 posts
  • Last active: Jan 26 2012 08:02 PM
  • Joined: 06 Jul 2011
Good Job but this isnt the first one tho

there is one that comes with scite4Autohotkey However very nice job you duplicated the functionality quite well
Posted Image Stolen from SKAN

r0lZ
  • Members
  • 192 posts
  • Last active: Feb 11 2012 11:19 PM
  • Joined: 21 Apr 2007
Thanks for your comments. I didn't know scite4Autohotkey, and I've just downloaded the portable ZIP package. It seems somewhat outdated, and anyway, it's a complete package. My script is, well... just a simple script, easier to install and master for the casual user. And you can use it with any text editor.
r0lZ

Drugwash
  • Members
  • 1078 posts
  • Last active: May 24 2016 04:20 PM
  • Joined: 07 Sep 2008
If I may:
- BUG: default button count should reflect the number of buttons in the currently selected scheme; the screenshot shows a two-button scheme with default button #3 :shock:
- SUGGESTION: add a 'variables' field where user can declare variables that will then be used in message box' title and/or text; this way, the code can be copy/pasted as is to the destination script.
I.e.: the message box needs to display "Current drive is %cdrive%." If you type that text in the text field, obviously the test will display "Current drive is ." (unless the chosen variable clashes with an already defined variable from your script). This would allow for a better text alignment.
- SUGGESTION: When the user choses to add a Help button, provide a dummy WM_HELP function along with the other two required commands (OwnDialogs and OnMessage).

Otherwise, pretty handy script; saves a bit of work. ;)

(AHK 1.0.48.05 and Win98SE) forever | My scripts are here


r0lZ
  • Members
  • 192 posts
  • Last active: Feb 11 2012 11:19 PM
  • Joined: 21 Apr 2007

- BUG: default button count should reflect the number of buttons in the currently selected scheme; the screenshot shows a two-button scheme with default button #3 :shock:

I agree that it's not perfect, bit it's intentional. The generator takes the current buttons type into account to generate the option value. For example, in my screenshot, even if button 3 is selected, button 2 is assumed because there are only two buttons.
I agree that a more intuitive way would be to disable button 3 and force button 2. I will do it...

- SUGGESTION: add a 'variables' field where user can declare variables that will then be used in message box' title and/or text; this way, the code can be copy/pasted as is to the destination script.
I.e.: the message box needs to display "Current drive is %cdrive%." If you type that text in the text field, obviously the test will display "Current drive is ." (unless the chosen variable clashes with an already defined variable from your script). This would allow for a better text alignment.

In fact, the Test button creates another script in your %TEMP% folder (because the MsgBox option number cannot be an expression), and runs it. I want to leave that temp script as simple as possible. Adding variable fields is not a bad idea, but that will be difficult to handle. Anyway, the "Text" textbox holds an example message that you can change if you wish, but if you really want to include variables in the text, you have to do it in your code, and use litteral text in the generator. That's much more simple than having to define the variables and their contents each time you run the generator.

- SUGGESTION: When the user choses to add a Help button, provide a dummy WM_HELP function along with the other two required commands (OwnDialogs and OnMessage).

For the same reason than above, it is difficult to add a dummy help in the temp script (as that would require to create a dummy main GUI and the WM_HELP function). But I will check if it is sufficient to create an empty invisible GUI, and if it's the case, I will probably add that feature.

BTW, I had also another idea: There should be a way to enter an option value in the GUI to automatically change the GUI options. That way, it will be easy to check what options are in effect for an existing MsgBox instruction, and to duplicate it.
r0lZ

garry
  • Spam Officer
  • 3219 posts
  • Last active: Sep 20 2018 02:47 PM
  • Joined: 19 Apr 2005
very nice
use often also the script from Thalon (2006)
<!-- m -->http://de.autohotkey...m/topic534.html<!-- m -->

r0lZ
  • Members
  • 192 posts
  • Last active: Feb 11 2012 11:19 PM
  • Joined: 21 Apr 2007
Well, that script is very similar to mine (although I don't like the big window).

It has nice icons. I've just added the code to display the current icon in the status bar. :-)

It has also a "Default desktop" window option that I didn't know. What is the purpose of that option? Is it documented somewhere? Should I add it in my script?
r0lZ

garry
  • Spam Officer
  • 3219 posts
  • Last active: Sep 20 2018 02:47 PM
  • Joined: 19 Apr 2005
in help file msgbox found this ( I don't understand really ) :

Shows the MsgBox on default desktop
(Windows NT/2000/XP or later) decimal value=131072



r0lZ
  • Members
  • 192 posts
  • Last active: Feb 11 2012 11:19 PM
  • Joined: 21 Apr 2007
OK, I've updated the script to v1.1 in post #1. (The screenshot and output example have also been updated.)

Changelog:
; v1.1:
; - Bug: The default button numbers that cannot be selected due to the current
;   buttons type selection are now disabled.
; - Added the currently selected icon in the status bar.
; - Test button: When the "Help" button option is selected, clicking the Help
;   button of the messagebox displays now a dummy help message.
; - Added the possibility to input an options number to modify the GUI accordingly.
; - Added the Right-To-Left alignment option.
; - Added the "Default Desktop" (undocumented?) window type option.

Sorry Drugwash, still no support for variables. But I took your two other remarks into account.
r0lZ

r0lZ
  • Members
  • 192 posts
  • Last active: Feb 11 2012 11:19 PM
  • Joined: 21 Apr 2007

in help file msgbox found this ( I don't understand really ) :

Shows the MsgBox on default desktop
(Windows NT/2000/XP or later) decimal value=131072


I don't see that option in my AutoHotkey.chm file. (I have the doc released with AutoHotkey_L v1.1.02.01.)

Anyway, I've added the option in v1.1.
r0lZ

Drugwash
  • Members
  • 1078 posts
  • Last active: May 24 2016 04:20 PM
  • Joined: 07 Sep 2008
I've made some changes and fixed a few minor issues, but I now realized this script was meant for AHK_L, so my changes would not be compatible since they make use of DllCalls and I'm strictly using AHK Classic (or Basic or whatever they may call it).

I'm in doubt whether I should post the modified version, for non-AHK_L users or for whoever would want to make it cross-compatible. :roll: :?:

(AHK 1.0.48.05 and Win98SE) forever | My scripts are here


r0lZ
  • Members
  • 192 posts
  • Last active: Feb 11 2012 11:19 PM
  • Joined: 21 Apr 2007
My script should be compatible with AHK classic, as I think it doesn't use AHK_L specific features. But I may be wrong. I don't use AHK classic any more, since a long time.

Anyway, feel free to post your version. I'm curious to see your changes.
r0lZ

Drugwash
  • Members
  • 1078 posts
  • Last active: May 24 2016 04:20 PM
  • Joined: 07 Sep 2008
Indeed, your script is compatible with AHK Classic, that's why I haven't noticed the AHK_L comment until much later. However, the changes I've made are not AHK_L and/or x64 compatible, since I'm using DllCalls with Int/UInt data type, while AFAIK an UIntPtr is required in AHK_L.

I may try to mock up a workaround (usable in all other scripts, if it proves correct), but needs testing and I won't be able to do that.

Anyway, here it goes (screenshot taken on Win98SE, low icon quality):

Posted Image
; MsgBoxGenerator.ahk: AutoHotkey script by r0lZ, September 2011
; Generates the AutoHotkey code to build easily a nice-looking MsgBox.
; Developed vith AutoHotkey_L v1.1.02.01 Unicode X64 under Win7 X64 SP1
;
; Version history:
; v1.0:
; - First release
; v1.1:
; - Bug: The default button numbers that cannot be selected due to the current
;   buttons type selection are now disabled.
; - Added the currently selected icon in the status bar.
; - Test button: When the "Help" button option is selected, clicking the Help
;   button of the messagebox displays now a dummy help message.
; - Added the possibility to input an options number to modify the GUI accordingly.
; - Added the Right-To-Left alignment option.
; - Added the "Default Desktop" (undocumented?) window type option.

VERSION = 1.1 mod

#NoTrayIcon
#NoEnv
#SingleInstance force
#UseHook off
#Persistent
DetectHiddenWindows, on

SplitPath, A_ScriptName, , , , APPTITLE

IncludeBrackets = 1

Menu, OptionsMenu, Add, Always on top, ToggleAlwaysOnTop
Menu, OptionsMenu, add, Show tray tips, ToggleTrayTips
Menu, OptionsMenu, Add, Enable Ctrl-Shift-M hotkey, ToggleSuspend
Menu, OptionsMenu, Check, Enable Ctrl-Shift-M hotkey
Menu, OptionsMenu, Add, Include brackets in code, ToggleBrackets
Menu, OptionsMenu, Check, Include brackets in code

Menu, Tray, NoStandard
Menu, Tray, Add, Open GUI, OpenMainGui
Menu, Tray, Default, Open GUI
Menu, Tray, Add
Menu, Tray, Add, Options, :OptionsMenu
Menu, Tray, Add
Menu, Tray, Add, Exit, Exit
Menu, Tray, Click, 1
Menu, Tray, Tip, AHK MsgBox generator`nBy r0lZ`, September 2011

if (! A_IsCompiled) {
    tmp = %A_ScriptDir%\%APPTITLE%.ico
    if (FileExist(tmp))
        Menu, Tray, Icon, %tmp%
    else
        Menu, Tray, Icon
    tmp =
} else
    Menu, Tray, Icon

;########## added by Drugwash ##########
hIL := IL_Create(4, 1, 0)
Loop, 4
	{
	i := DllCall("LoadImage", "UInt", 0, "UInt", 32517-A_Index, "UInt", 1, "UInt", 16, "UInt", 16, "UInt", 0x8000, "UInt")
	DllCall("comctl32\ImageList_AddIcon", "UInt", hIL, "UInt", i)
	DllCall("DestroyIcon", "UInt", i)
	hI%A_Index% := DllCall("ImageList_GetIcon", "UInt", hIL, "Int", A_Index-1, "UInt", 1)	; ILD_TRANSPARENT
	}
;#################################

BuildMainGUI(hIL)

OnExit, CleanExit


Return


Exit:
    ExitApp
;    OnExit
CleanExit:
    FileDelete, %A_Temp%\MsgBoxTest.ahk
;########## added by Drugwash ##########
Loop, 4
	DllCall("DestroyIcon", "UInt", hI%A_Index%)
;#################################
    ExitApp
return


; **************************** Main GUI ***********************
OpenMainGui:
    Gui, Show
return

BuildMainGUI(hIL)
{
    Global VERSION
    Global ButtonsVal, DefaultButtonVal, HelpButton, IconVal, WindowVal, RightJustify, RightToLeft, Title, Text, TimeoutOn, Timeout, TimeoutE

    Menu, GuiMenu, add, Exit, Exit
    Menu, GuiMenu, Add, Options, :OptionsMenu
    Menu, GuiMenu, add, Input#, InputNumber
    Gui, Menu, GuiMenu

    Gui, Margin, 4, 4
    Gui, Font, S8


    Gui, Add, Text, X4 Y4 Section, Buttons:
    Gui, Add, ListBox, XS vButtonsVal r7 w200 gGuiChanged AltSubmit, OK|OK / Cancel|Abort / Retry / Ignore|Yes / No / Cancel|Yes / No|Retry / Cancel|Cancel / Try Again / Continue [WinXP+]
    GuiControl, Choose, ButtonsVal, 1

    Gui, Add, Text, XS Section, Default Button:
    Gui, Add, Radio, YS vDefaultButtonVal Checked gGuiChanged, &1
    Gui, Add, Radio, YS gGuiChanged, &2
    Gui, Add, Radio, YS gGuiChanged, &3
   
;    Gui, Add, Checkbox, XS Section vHelpButton gGuiChanged, Add '&Help' button`n[ Requires   Gui +OwnDialogs   and`nOnMessage(0x53, "WM_HELP") ]

    Gui, Add, Text, X208 Y4 Section, Icon:
;########## added by Drugwash ##########
Gui, Add, ListView, XS vIconVal h95 w83 Count5 -hdr -Multi -WantF2 gGuiChanged AltSubmit, Type
LV_ModifyCol(0, "AutoHdr")
LV_SetImageList(hIL)
LV_Add("Icon5", "No icon"), LV_Add("Icon1", "Info"), LV_Add("Icon2", "Warning"), LV_Add("Icon3", "Question"), LV_Add("Icon4", "Error")
LV_ModifyCol(0, "AutoHdr")
LV_Modify(1, "Select")
;############# replacing ##############
;    Gui, Add, ListBox, XS vIconVal r5 w83 gGuiChanged AltSubmit, No icon|Info|Warning|Question|Error
;    GuiControl, Choose, IconVal, 2
;#################################
   
    Gui, Add, Text, XS Section, Window:
    Gui, Add, ListBox, XS vWindowVal r5 w83 +0x8 gGuiChanged AltSubmit Section, Normal|Task modal|System modal|Always on top|Default desktop
    GuiControl, Choose, WindowVal, 1

;########## added by Drugwash ##########
    Gui, Add, Groupbox, x4 YS-6 W200 H75 0x8000 Section,
    Gui, Add, Checkbox, X10 YS+9 Section vHelpButton gGuiChanged, Add '&Help' button *
    Gui, Add, Checkbox, XS vRightJustify gGuiChanged, &Right-justify text [Win2000+]
    Gui, Add, Checkbox, XS vRightToLeft  gGuiChanged, &Right-To-Left reading
    Gui, Add, Checkbox, XS vTimeoutOn gGuiChanged, Ti&meout [max 60 seconds]:
    Gui, Add, Edit, X+1 W40 Yp-8 vTimeoutE Number gGuiChanged Disabled
    Gui, Add, UpDown, vTimeout Range1-60 gGuiChanged Disabled, 10
    Gui, Font, s7, Tahoma
    Gui, Add, Text, XS-6 Section Disabled, * Requires: Gui +OwnDialogs and OnMessage(0x53, "WM_HELP")
    Gui, Font
    Gui, Add, Text, XS H5 W288 Section 0x10,
;############# replacing ##############
/*
    Gui, Add, Text, X4 Section, Text alignment options:
    Gui, Add, Checkbox, YS vRightJustify gGuiChanged, &Right-justify
    Gui, Add, Checkbox, YS vRightToLeft  gGuiChanged, &Right-To-Left

    Gui, Add, Text, XS Section,
*/
;#################################

    Gui, Add, Text, XS Section W30, Title:
    Gui, Add, Edit, YS-4 w253 vTitle gGuiChanged -WantReturn, `%A_ScriptName`%

    Gui, Add, Edit, XS Section w287 vText r4 gGuiChanged +WantReturn, Text
;############# replacing ##############
/*
    Gui, Add, Checkbox, XS Section vTimeoutOn gGuiChanged, Ti&meout:
    Gui, Add, Edit, w50 YS-3 Number gGuiChanged
    Gui, Add, UpDown, vTimeout Range1-60 gGuiChanged, 10
    Gui, Add, Text, YS, seconds
*/
;#################################

    Gui, Add, Text, XS Section, Control-Shift-M inserts the code in the current application.
    Gui, Add, Button, XS w93 gCopyNum Section, Copy &option #
    Gui, Add, Button, YS w93 gCopyCode, Copy &code
    Gui, Add, Button, YS w93 gTest Default, &Test

    Gui, Add, StatusBar

    GoSub, GuiChanged

    SysGet, Mon, MonitorWorkArea
    posx := MonLeft + 20
    posy := MonTop + 20
    Gui, Show, X%posx% Y%posy%, AHK MsgBox generator v%VERSION%
}

GuiChanged:
    Gui, Submit, NoHide
    val := ButtonsVal -1
    if (val == 0) {
        GuiControl, Hide, &2
        GuiControl, Hide, &3
        DefaultButtonVal = 1
        GuiControl, , DefaultButtonVal, 1
    }
    else if (val == 1 || val == 4) {
        GuiControl, Show, &2
        GuiControl, Hide, &3
        if (DefaultButtonVal > 2) {
            DefaultButtonVal = 2
            GuiControl, , &2, 1
        }
    } else {
        GuiControl, Show, &2
        GuiControl, Show, &3
    }
    val += (DefaultButtonVal - 1) * 256
    val += HelpButton ? 16384 : 0
;########## added by Drugwash ##########
IconVal := LV_GetNext()
if iconnum := IconVal-1
	val += (6 - IconVal) * 16
;############# replacing ##############
/*
    if (IconVal != 1) {
	val += (6 - IconVal) * 16
        if (iconval == 2)
            iconnum = 5
        else if (iconval == 3)
            iconnum = 2
        else if (iconval == 4)
            iconnum = 3
        else
            iconnum = 4
    } else
        iconnum = 0
*/
;########## added by Drugwash ##########
Gui, +LastFound
if InStr(WindowVal, "1|")
	{
	if InStr(LastWindowVal, "1")
		{
		PostMessage, 0x185, 0, 0, ListBox2	; deselect item #1 if other items are selected
		StringTrimLeft, WindowVal, WindowVal, 2	; take item #1 off the selection list
		}
	else
		{
		PostMessage, 0x185, 0, -1, ListBox2	; deselect all items if item #1 was selected last
		WindowVal=
		}
	}
if !WindowVal
	{
	PostMessage, 0x185, 1, 0, ListBox2	; select item #1 if selection is empty
	WindowVal=1
	}
LastWindowVal := WindowVal
Loop, Parse, WindowVal, |
	val += 0x1000*(A_LoopField==3) + 0x2000*(A_LoopField==2) + 0x40000*(A_LoopField==4) + 0x20000*(A_LoopField==5)
;############# replacing ##############
/*
    if (WindowVal == 2)
        val += 8192
    else if (WindowVal == 3)
        val += 4096
    else if (WindowVal == 4)
        val += 262144
*/
;#################################
    if RightJustify
        val += 0x80000
    if RightToLeft
        val += 0x100000
SetFormat, Integer, H
val+=0
SetFormat, Integer, D
    StringReplace, Title, Title, `, , ```, , 1
    StringReplace, Message, Text, `, , ```, , 1
    StringReplace, Message, Message, `n, ``n, 1
    st = MsgBox, %val%, %Title%, %Message%
    if (TimeoutOn) {
;########## added by Drugwash ##########
GuiControl, Enable, TimeoutE
GuiControl, Enable, Timeout
;#################################
        if (Timeout > 0)
            st = %st%, %Timeout%
        else {
            TimeoutOn = 0
            GuiControl, , TimeoutOn, 0
            soundbeep
        }
    }
;########## added by Drugwash ##########
else {
GuiControl, Disable, TimeoutE
GuiControl, Disable, Timeout
}
    if iconnum {
        SB_SetParts(22)
	SendMessage, 0x40F, 0, hI%iconnum%, msctls_statusbar321	; SB_SETICON
;############# replacing ##############
;    if (iconnum && FileExist(A_WinDir . "\system32\user32.dll")) {
;        SB_SetParts(22)
;        SB_SetIcon("user32.dll", iconnum)
;#################################
   } else {
        SB_SetParts(0)
    }
    SB_SetText(st, 2)
    if (ShowTrayTips) {
        GoSub, GenCode
        TrayTip, MsgBox code:, %code%, 10, 1
    }
return

CopyNum:
    GoSub, GuiChanged
    Clipboard = %val%
return

CopyCode:
    GoSub, GuiChanged
    GoSub, GenCode
    clipboard = %code%
return

GenCode:
    code = MsgBox, %val%, %Title%, %Message%
    if (IncludeBrackets)
        s = `n{`n`n}
    else
        s = `n
    if (TimeoutOn)
        code = %code%, %Timeout%
    if (ButtonsVal == 1 && TimeoutOn)
        code = %code%`nIfMsgBox, OK%s%
    if (ButtonsVal == 2)
        code = %code%`nIfMsgBox, OK%s%`nIfMsgBox, Cancel%s%
    if (ButtonsVal == 3)
        code = %code%`nIfMsgBox, Abort%s%`nIfMsgBox, Retry%s%`nIfMsgBox, Ignore%s%
    if (ButtonsVal == 4)
        code = %code%`nIfMsgBox, Yes%s%`nIfMsgBox, No%s%`nIfMsgBox, Cancel%s%
    if (ButtonsVal == 5)
        code = %code%`nIfMsgBox, Yes%s%`nIfMsgBox, No%s%
    if (ButtonsVal == 6)
        code = %code%`nIfMsgBox, Retry%s%`nIfMsgBox, Cancel%s%
    if (ButtonsVal == 7)
        code = %code%`nIfMsgBox, Cancel%s%`nIfMsgBox, Try Again%s%`nIfMsgBox, Continue%s%
    if (TimeoutOn)
        code = %code%`nIfMsgBox, Timeout%s%
    code = %code%`n
return

Test:
    GoSub, GuiChanged
    cmd = MsgBox, %val%, %Title%, %Message%
    if TimeoutOn
        cmd = %cmd%, %Timeout%
    FileDelete, %A_Temp%\MsgBoxTest.ahk
    if (HelpButton) {
        code = Gui, Show, Hide, %Title%`nGui, +OwnDialogs`nOnMessage(0x53, "WM_HELP")`n%cmd%`nexitapp`nWM_HELP()`n{`n MsgBox, 64, Help - %Title%, This is a dummy help message., 3`n}`n
        FileAppend, %code%`n, %A_Temp%\MsgBoxTest.ahk
        code =
    } else {
        FileAppend, %cmd%`n, %A_Temp%\MsgBoxTest.ahk
    }   
    Run, "%A_Temp%\MsgBoxTest.ahk", %A_Temp%, UseErrorLevel
return

GuiClose:
GuiEscape:
    GoSub, GuiChanged
    Gui, Hide
return

ToggleAlwaysOnTop:
    alwaysontop := ! alwaysontop
    Menu, OptionsMenu, ToggleCheck, Always on top
    if (alwaysontop)
        Gui, +AlwaysOnTop
    else
        Gui, -AlwaysOnTop
return

ToggleSuspend:
    Suspend, Toggle
    Menu, OptionsMenu, ToggleCheck, Enable Ctrl-Shift-M hotkey
return

ToggleTrayTips:
    ShowTrayTips := ! ShowTrayTips
    Menu, OptionsMenu, ToggleCheck, Show tray tips
    if (ShowTrayTips)
        GoSub, GuiChanged
    else
        TrayTip
return

ToggleBrackets:
    IncludeBrackets := ! IncludeBrackets
    Menu, OptionsMenu, ToggleCheck, Include brackets in code
    if (ShowTrayTips)
        GoSub, GuiChanged
return

^+m::
    GoSub, GuiChanged
    GoSub, GenCode
    SetKeyDelay, 0
    SendRaw, %code%
return

; **************** GUI 2: Input an option number **************************

InputNumber:
    GoSub, GuiChanged
    num := InputNumber(val)
return

InputNumber(defaultnum)
{
    Global InputNum

    Gui, 1:+Disabled
    Gui, 2:Default
    Gui, +Owner1

    Gui, -MinimizeBox -MaximizeBox

    Gui, Add, Text, Section, MsgBox Options value:

    Gui, Add, Edit, Limit7 Number vInputNum Section w110 -WantReturn, %defaultnum%

    Gui, Add, Button, XS w50 g2GuiOK Section Default, &OK
    Gui, Add, Button, YS w50 g2GuiClose, &Cancel
   
    Gui, 1:+LastFound
    WinGetPos, posx, posy
    posx += 80
    posy += 50
    Gui, Show, X%posx% Y%posy%, Input#
}

2GuiOK:
    Gui, 2:Submit, NoHide
    Gui, 1:Default

    GuiControl, Enable, &2
    GuiControl, Enable, &3
    GuiControl, Choose, ButtonsVal, 3
    val := round((InputNum & 0x300) / 0x100) + 1
    GuiControl, , &%val%, 1

    val := (InputNum & 7) + 1
    if (val > 7) {
        val = 1
        soundbeep
    }
    GuiControl, Choose, ButtonsVal, %val%

    GuiControl, , HelpButton, % round((InputNum & 0x4000) / 0x4000)

    val := round((InputNum & 0x70) / 0x10)
    if (val == 0)
        GuiControl, Choose, IconVal, 1
    else
        GuiControl, Choose, IconVal, % 6 - val

    val := InputNum & 0x63000
    if (val == 0)
        GuiControl, Choose, WindowVal, 1
    else if (val == 0x1000)
        GuiControl, Choose, WindowVal, 3
    else if (val == 0x2000)
        GuiControl, Choose, WindowVal, 2
    else if (val == 0x40000)
        GuiControl, Choose, WindowVal, 4
    else if (val == 0x20000)
        GuiControl, Choose, WindowVal, 5
    else {
        Soundbeep
        GuiControl, Choose, WindowVal, 1
    }

    GuiControl, , RightJustify, % round((InputNum & 0x80000) / 0x80000)
    GuiControl, , RightToLeft,  % round((InputNum & 0x100000) / 0x100000)

    GoSub, 2GuiClose
    GoSub, GuiChanged
return

2GuiEscape:
2GuiClose:
    Gui, 2:Destroy
    Gui, 1:-Disabled
    Gui, 1:Default
return

(AHK 1.0.48.05 and Win98SE) forever | My scripts are here


r0lZ
  • Members
  • 192 posts
  • Last active: Feb 11 2012 11:19 PM
  • Joined: 21 Apr 2007
Nice job, but I don't understand why you use DllCall() and PostMessage, as there are standard AHK commands or functions to accomplish the same things.

I have adopted the idea to use a ListView instead of a ListBox for the Icon selector, as it is nice to see the icon in the selector. But I use IL_Add() instead of your DLLCalls. The icons added with IL_Add() are of much better visual quality. Similarly, I use LV_Modify() to change the selection in the listview. Any reason to prefer the DLLCalls and SendMessages?

There was a bug in my code that you have not fixed. The number of seconds for the timeout should not be limited to 60. (The real limit is 2147483 seconds.) But it was impossible to set a number of seconds > 60 in the GUI, due to the precedence of the spinboxes over the text field. Now, I use the text field to retrieve the value.

I've also fixed the Left-to-right typo. Thanks!

Also, I didn't know the trick of inserting a Text widget with the option 0x10 to create a separator line. Nice! I've used it in the new version. Thanks also for that trick!

I still prefer the way the GUI is organised in my version, as I want to have the options in the same order than the MsgBox command (options, then title, text and timeout). Your GUI looks nice, but IMO grouping all checkboxes in the same GroupBox (and especially the timeout option) is not a good idea.

(BTW, I hate the GroupBox widget, as it is almost impossible to compute its width and height if you have not forced a specific GUI font. This is a big limitation of the widget implementations in Autohotkey. I use also Tcl/Tk for some scripts, and the widgets are much much more easy to position, dynamically resize, etc... IMO, the entire AHK GUI stuff should be completely rewritten with the Pack and Grid Tcl/Tk options in mind. We can dream...)

I have also noticed a problem with the default buttons. Although the "Test" button has the Default option, when I press Enter, it is not activated. (Same thing for the OK button of the Input# GUI.) Is that "Default" option working for you? Is it an incompatibility with my Win7 X64? Or an Autohotkey bug?

Anyway, thanks for your script. I've stolen many of your ideas! ;-)

[EDIT] I forgot to say that IMO your smart multiselection in the Window box doesn't make sense. AFAIK, the options are mutually exclusive (except perhaps for the Default Desktop option, but I don't understand what that option is supposed to do).
r0lZ

r0lZ
  • Members
  • 192 posts
  • Last active: Feb 11 2012 11:19 PM
  • Joined: 21 Apr 2007
Version 1.2 available in the first post of this thread.

Changelog:
; v1.2:
; - Bug: Right-to-left was "Left-to-right"
; - Some modifications inspired by the mod version released by Drugwash:
; - Changed the Icon listbox to a listview to show the icons in the GUI.
; - The Timeout value combobox is now disabled when the Timeout checkbox is not ticked.
; - The option value is now in hexadecimal format.
; - GUI somewhat reorganised.

r0lZ