Jump to content

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

[Function] Custom Msgbox / Custom Buttons


  • Please log in to reply
20 replies to this topic
Icarus
  • Members
  • 851 posts
  • Last active: Jan 02 2012 11:17 AM
  • Joined: 24 Nov 2005
Hello everybody,

I am guessing there are some scripts like this lying around, but I needed my own dialog, with custom buttons.

So, a quick function for your enjoyment, to do this:
Posted Image

;-------------------------------------------------------------------------------
; Custom Msgbox
; Filename: cmsgbox.ahk
; Author  : Danny Ben Shitrit (aka Icarus)
;-------------------------------------------------------------------------------
; Copy this script or include it in your script (without the tester on top).
;
; Usage:
;   Answer := CMsgBox( title, text, buttons, icon="", owner=0 )
;   Where:
;     title   = The title of the message box.
;     text    = The text to display.
;     buttons = Pipe-separated list of buttons. Putting an asterisk in front of 
;               a button will make it the default.
;     icon    = If blank, we will use an info icon.
;               If a number, we will take this icon from Shell32.dll
;               If a letter ("I", "E" or "Q") we will use some predefined icons
;               from Shell32.dll (Info, Error or Question).
;     owner   = If 0, this will be a standalone dialog. If you want this dialog
;               to be owned by another GUI, place its number here.
;
;-------------------------------------------------------------------------------

; --- TESTER BEGIN - comment out the entire section when including -------------
#SingleInstance Force

; • Simple example
  Pressed := CMsgbox( "Hello World", "Are you sure you want to say hello to the world?`n`nWarning! This operation is irreversible.", "&Yes|*Not &Sure|&Not at All|&HELP!", "E" )
  Msgbox 32,,"%pressed%" was pressed

; • Custom icon
  Pressed := CMsgbox( "Where Is It?", "Do you want to find the holy grail?`n`n(Custom icons from Shell32.dll)", "*&Yes Please|&Not Today|Not &Ever", "23" )
  Msgbox 32,,"%pressed%" was pressed

; • Example for msgbox that is owned by our own GUI
  Gui Add, Text  ,w200, This is my GUI and I'll cry if I want to.
  Gui Add, Button,wp  , Cry
  Gui Show, x200 y200
  Pressed := CMsgbox( "Owned", "I am owned by GUI 1", "*&Ok|&Whatever","",1 )
  Msgbox 32,,"%pressed%" was pressed

  Return
; --- TESTER END ---------------------------------------------------------------



CMsgBox( title, text, buttons, icon="", owner=0 ) {
  Global _CMsg_Result
  
  GuiID := 9      ; If you change, also change the subroutines below
  
  StringSplit Button, buttons, |
  
  If( owner <> 0 ) {
    Gui %owner%:+Disabled
    Gui %GuiID%:+Owner%owner%
  }

  Gui %GuiID%:+Toolwindow +AlwaysOnTop
  
  MyIcon := ( icon = "I" ) or ( icon = "" ) ? 222 : icon = "Q" ? 24 : icon = "E" ? 110 : icon
  
  Gui %GuiID%:Add, Picture, Icon%MyIcon% , Shell32.dll
  Gui %GuiID%:Add, Text, x+12 yp w180 r8 section , %text%
  
  Loop %Button0% 
    Gui %GuiID%:Add, Button, % ( A_Index=1 ? "x+12 ys " : "xp y+3 " ) . ( InStr( Button%A_Index%, "*" ) ? "Default " : " " ) . "w100 gCMsgButton", % RegExReplace( Button%A_Index%, "\*" )

  Gui %GuiID%:Show,,%title%
  
  Loop 
    If( _CMsg_Result )
      Break

  If( owner <> 0 )
    Gui %owner%:-Disabled
    
  Gui %GuiID%:Destroy
  Result := _CMsg_Result
  _CMsg_Result := ""
  Return Result
}

9GuiEscape:
9GuiClose:
  _CMsg_Result := "Close"
Return

CMsgButton:
  StringReplace _CMsg_Result, A_GuiControl, &,, All
Return



jballi
  • Members
  • 1029 posts
  • Last active:
  • Joined: 01 Oct 2005
I like it. :) No need for it right now but I can see using this sometime in the future. Thanks for sharing.

And yes, you are at least the 4th person to write one of these. The others include Areilius, jonny, and Roland.

majkinetor
  • Moderators
  • 4512 posts
  • Last active: May 20 2019 07:41 AM
  • Joined: 24 May 2006
Cool, thx.
Posted Image

twhyman
  • Members
  • 348 posts
  • Last active: Sep 17 2014 01:55 AM
  • Joined: 07 Dec 2005
Thanks, this is great!

Deller
  • Members
  • 239 posts
  • Last active: Dec 08 2011 08:36 PM
  • Joined: 21 Nov 2007
This is great.

AmourSpirit
  • Members
  • 93 posts
  • Last active: Mar 01 2016 04:28 PM
  • Joined: 01 Dec 2010
I was inspired so I made [Function] Custom Input box / Password box

Deo
  • Members
  • 199 posts
  • Last active: Jan 31 2014 03:19 PM
  • Joined: 16 May 2010
try out this too )

weddingcoo
  • Members
  • 1 posts
  • Last active: Dec 13 2011 04:54 AM
  • Joined: 13 Dec 2011
Cool, thx. it is great!!

TedStriker
  • Members
  • 30 posts
  • Last active: Jul 31 2013 09:33 PM
  • Joined: 15 Jan 2008
Thanks @AmourSpirit for replying to a 3 years old thread. I'd have never found it. I love it.

Since I'm on dual screen I hate it when GUIs appear on the wrong screen. I've added some little code, so the GUI message box appears on the monitor with the mouse cursor.

I'm sure it can be done better, but it works for me. :)
WinGetPos wouldn't bring me results unless the GUI is shown and I wanted to avoid flickering. So I made this with transparency.


Replace

Gui,%GuiID%:Show,,%title%

with

DetectHiddenWindows,On
	Gui,%GuiID%:+LastFound
	hwnd:=WinExist()
	WinSet,Transparent,0,ahk_id %hwnd%
	Gui,%GuiID%:Show,,%title%
	WinGetPos,X,,,,ahk_id %hwnd%
	WinMove,ahk_id %hwnd%,,% DualDisplay(hwnd,x)
	WinSet,Transparent,OFF,ahk_id %hwnd%

and add following function in your script.

DualDisplay(hwnd,x){
	sysget, m1, monitor, 1
	sysget, m2, monitor, 2
	Coordmode, Mouse, Screen
	MouseGetPos, m_x
	m_l := (m_x > m1right) ? m1right : ((m_x > m2right) ? m2right : "")
	Return % (m_l = "") ? x : ((x < m_l) ? x + m_l : x)
}


iBob35555VR
  • Members
  • 89 posts
  • Last active: Dec 24 2012 03:32 PM
  • Joined: 02 Dec 2011
thats very useful (:
... All posts where I said the word "smart", maybe, I meant "small" as I had them confused. "Smart script","Smart change" etc are examples.

Icarus
  • Members
  • 851 posts
  • Last active: Jan 02 2012 11:17 AM
  • Joined: 24 Nov 2005
Updated screenshot.
Sector-Seven - Freeware tools built with AutoHotkey

ryanmm50
  • Members
  • 1 posts
  • Last active: Dec 20 2011 12:08 PM
  • Joined: 20 Dec 2011
Do I need to have a special plug-in to make the CMsgBox work? or QMsgBox? I'm trying to make message boxes that pop up. I just tried copying the code into Notepad++. This will be my first attempt at any kind of scripting besides my T-89 calculator in high school and a little VB at my job. Thank you.

TedStriker
  • Members
  • 30 posts
  • Last active: Jul 31 2013 09:33 PM
  • Joined: 15 Jan 2008
Hi Ryanmm50,

you'll need an installation of Autohotkey to make the script work.

This will probably help you: <!-- m -->http://www.autohotke...cs/Tutorial.htm<!-- m -->

Have fun! :)

Flowerpiggy
  • Members
  • 45 posts
  • Last active: Dec 20 2012 02:57 PM
  • Joined: 22 Apr 2011
Thank you for sharing. Icarus.

Joy2DWorld
  • Members
  • 562 posts
  • Last active: Jun 30 2014 07:48 PM
  • Joined: 04 Dec 2006
DualDisplay(hwnd,x){
	sysget, m1, monitor, 1
	sysget, m2, monitor, 2
	Coordmode, Mouse, Screen
	MouseGetPos, m_x
	m_l := (m_x > m1right) ? m1right : ((m_x > m2right) ? m2right : "")
	Return % (m_l = "") ? x : ((x < m_l) ? x + m_l : x)
}
[/quote]


hmm.. do you use hwnd in the function ?
Joyce Jamce