Another Custom MsgBox for AHK_L v1

Post your working scripts, libraries and tools for AHK v1.1 and older
DRocks
Posts: 565
Joined: 08 May 2018, 10:20

Another Custom MsgBox for AHK_L v1

18 Feb 2019, 00:29

Hi,

So many people have contributed to helping me here with Autohotkey.
You all are amazing when you share code, reply to questions, post tutorials. This is great.

My way of saying thank you for now is to in turn share this function that can be handy when designing Custom Guis all scripted in Autohotkey.

This function will generate a MsgBox and provide basic fonctionnality to do an action with the user selection.
Please let me know what you think and if you know how to do a genuine FrameShadow without affecting other windows of the same class, please post :)

Version Changes:
2019-02-19 v0.2 - Basic support for bigger titlebar size and text size. Uses Ternary checks and picture stretching (pics stretch not ideal)

Code: Select all

;--------------------------------------------------------------------------------
; D Rocks Custom MsgBox v0.2 - 2019-02-19
;
;
; Inspired and motivated by: Hellbent aka CivReborn
; - https://www.autohotkey.com/boards/viewtopic.php?f=76&t=62081
;
; Model by: TheDewd and kczx3 - 2016 post
; -  https://www.autohotkey.com/boards/viewtopic.php?t=18271
; 
; 
; KNOWN ISSUE: 
; - Using FrameShadow affects all windows of same class which is most of the times unwanted
; 	> Disabled until I find how to solve this
;	> If you know how please post here
;
; - Pictures must scale for bigger font sizes
; 	> Basic modification implemented to stretch pictures, not ideal. It is better to modify source image size non-destructively
;--------------------------------------------------------------------------------

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
#SingleInstance, Force ; Prevent double execution of this script

;--------------------------------------------------------------------------------
; ExampleA including 2 buttons: YES and NO
CustomMsgBox_Func(350, 150, "Another Custom MsgBox will pop-up`nif you choose YES.`n`nDo you want to continue ?", "CONFIRMATION", "gCustomMsgBox", "YES", "NO")
WinWaitClose, % "ahk_id " hMsgBox ; Waits until you select something and by selecting something the window should close
;--------------------------------------------------------------------------------
; ExampleB including 1 button: YEP
If (MsgBoxChoice = "YES") {
	CustomMsgBox_Func(250, 100, "You have pressed the YES button.", "SUCCESS", "gCustomMsgBox", "YEP !")
	WinWaitClose, % "ahk_id " hMsgBox ; Waits until you select something and by selecting something the window should close
} ;--------------------------------------------------------------------------------

;--------------------------------------------------------------------------------
; ExampleC including 2 buttons: HmK too bad
If (MsgBoxChoice = "YEP !") {
	CustomMsgBox_Func(500, 150, "You just experienced amazing stuff.", "THANK YOU", "gCustomMsgBox", "Hmm K", "Too bad")
	WinWaitClose, % "ahk_id " hMsgBox ; Waits until you select something and by selecting something the window should close
} ;--------------------------------------------------------------------------------

;--------------------------------------------------------------------------------
; ExampleD including 2 buttons: BIG BROTHER
If (MsgBoxChoice = "Hmm K") {
	CustomMsgBox_Func(800, 270, "This example uses monster font size and image scaling.`n`nIs it looking good on 4k display?", "BIGGER EVERYTHING", "gCustomMsgBox", "BIG...", "GUY", , 26, 18)
} ;--------------------------------------------------------------------------------
return


;-----------------------------------------------------------------------------------------------------------------------------------------
CustomMsgBox_Func(GuiW, GuiH, DisplayText, GuiWinTitle, gLabel, Button1text, Button2text:="", Owner:="", TitleSize:="", TextSize:="") {
;-----------------------------------------------------------------------------------------------------------------------------------------
	Global hMsgBox, hButton1text, hButton2text, HasOwner
	
;=== Gui pop-up options
	Gui, CustomMsgBox: New, +hWndhMsgBox -Caption -SysMenu -MinimizeBox +AlwaysOnTop
	Gui, Color, White
	
	
;=== Gui Variables	
	TitlebarColor:= "000" ; black
	,TitlebarHeight:= (TitleSize>18? (TitleSize>24? 60:45):30)
	,ButtonW:=(TextSize>12? 250:140)
	,BorderThickness:= 4
	,InnerMargin:= 15, Margin:=  InnerMargin + BorderThickness
	,Padding:= 12
	,CenterHorizontal:= "Center", CenterVertical:= 0x200
	,Height:= GuiH+30 + BorderThickness + (Padding-4)
	,TextPos:= "x" Margin " y+"((GuiH)//7) " w" (GuiW - (Margin * 2))
	
	
;=== Use a Progress Meter to fake a title bar.
	Gui, Font, % (TitleSize>11? "s" . TitleSize : "s11") " bold cWhite"
	Gui, Add, Progress	, % "x0 y0 w" GuiW " h" TitlebarHeight " Background" TitlebarColor
	Gui, Add, Text		, % "x0 y0 w" GuiW " h" TitlebarHeight " BackgroundTrans " CenterHorizontal " " CenterVertical,% GuiWinTitle
	
	
;=== Place the MsgBox controls
	Gui, Font
	Gui, Font, % (TextSize>10? "s"TextSize:"s10") " cBlack"
	
	; text displayed in center ----------------------------------------------------------------------
	Gui, Add, Text, % TextPos " center", % DisplayText
	;--------------------------------------------------------------------------------
	
	;== CUSTOM BUTTONS ==============================================================================
	if (Button2text) {
	;-- Button1------------------------------------------------------------------------------
		Gui, Add, Picture, % "x"Margin+2 " y"GuiH-(Margin) " SECTION" , % ImageFolder "ConfirmerMsgBoxIcon.png"									; Check icon
		Gui, Add, Picture, % "xp yp w" ButtonW " +BackgroundTrans", % ImageFolder "CustomMsgBoxButtonShadow.png"											; Shadow button
		Gui, Add, Text, % "g" gLabel " hWndhButton1text xp yp-2 w" ButtonW-11 " h45 +BackgroundTrans center "	 	CenterVertical, % "     " Button1text	; Texte sur bouton
	;--------------------------------------------------------------------------------
	;-- Button2--------------------------------------------------------------------------------
		Gui, Add, Picture, % "x" GuiW-(30+(Margin+Padding)) " ys+3",% ImageFolder "AnnulerMsgBoxIcon.png"						; X icon
		Gui, Add, Picture, % "xp-"(ButtonW-50) " yp-3 w"ButtonW " +BackgroundTrans", % ImageFolder "CustomMsgBoxButtonShadow.png"								; Shadow button
		Gui, Add, Text, % "g" gLabel " hWndhButton2text xp yp-2 w"ButtonW-11 " hp +BackgroundTrans center " 		CenterVertical, % Button2text "     "	; Texte sur bouton
	} ;--------------------------------------------------------------------------------
	else {
		;-- Button1------------------------------------------------------------------------------
		Gui, Add, Picture, % "x"(GuiW/2)-(140/2) " y"GuiH-(Margin), % ImageFolder "ConfirmerMsgBoxIcon.png"								; Check icon
		Gui, Add, Picture, % "xp yp +BackgroundTrans", % ImageFolder "CustomMsgBoxButtonShadow.png"									; Shadow button
		Gui, Add, Text, % "g" gLabel " hWndhButton1text xp yp w" ButtonW " h45 +BackgroundTrans center "	 	CenterVertical, % "     " Button1text	; Texte sur bouton
		;--------------------------------------------------------------------------------
	}
	;==============================================================================================
	
	
;=== Add the border.
	Gui, Add, Progress, % "x" 0 						" y" (Height - BorderThickness) " w" GuiW 				" h" BorderThickness 	" Background" TitlebarColor	; Border Bottom
	Gui, Add, Progress, % "x" 0 						" y" TitlebarHeight			  " w" BorderThickness		" h" Height-34			" Background" TitlebarColor	; Border Left
	Gui, Add, Progress, % "x" (GuiW - BorderThickness) 	" y" TitlebarHeight			  " w" BorderThickness		" h" Height-34 		" Background" TitlebarColor	; Border Right
	
	
;=== Shadow - Added by kczx3
	;FrameShadow(hMsgBox)
	;DllCall("SetClassLong","UInt",hMsgBox,"Int",-26,"Int",DllCall("GetClassLong","UInt",hMsgBox,"Int",-26)|0x20000)
; Disabled until I find how to not affect other window shadows of the same class
	
	
;=== Show MsgBox and disable owner Gui
	Gui, %hMsgBox%:Show, % "w" GuiW " h" Height, % GuiWinTitle	; At last show the Custom MsgBox
	
	if (Owner) {
		HasOwner:=Owner
		Gui, %Owner%:+Disabled 							; Owner(hWnd) will be disabled
	}
}
/*
;--------------------------------------------------------------------------------
	FrameShadow(HGui) {
;--------------------------------------------------------------------------------
	; https://www.autohotkey.com/boards/viewtopic.php?f=6&t=29117&p=264035#p264035
		
		DllCall("dwmapi\DwmIsCompositionEnabled","IntP",_ISENABLED) ; Get if DWM Manager is Enabled
		if _ISENABLED ; if DWM is not enabled, Make Basic Shadow
			DllCall("SetClassLong","UInt",HGui,"Int",-26,"Int",DllCall("GetClassLong","UInt",HGui,"Int",-26)|0x20000)
		else {
			VarSetCapacity(_MARGINS,16)
			NumPut(1,&_MARGINS,0,"UInt")
			NumPut(1,&_MARGINS,4,"UInt")
			NumPut(1,&_MARGINS,8,"UInt")
			NumPut(1,&_MARGINS,12,"UInt")
			DllCall("dwmapi\DwmSetWindowAttribute", "Ptr", HGui, "UInt", 2, "Int*", 2, "UInt", 4)
			DllCall("dwmapi\DwmExtendFrameIntoClientArea", "Ptr", HGui, "Ptr", &_MARGINS)
		}
	}
*/

;-- On MsgBox Exit ------------------------------------------------------------------------------
gCustomMsgBox:
CustomMsgBoxGuiEscape:
CustomMsgBoxGuiClose:
if (A_GuiControl) ; vrai quand on clic sur un bouton 
	MsgBoxChoice:= Trim(A_GuiControl)

if (HasOwner)
	Gui, %hGuiMain%:-Disabled 	; hGuiMain owner will be re-enabled
HasOwner:=""

Gui, %hMsgBox%:Destroy		; msgbox pop-up will be destroyed
return
;--------------------------------------------------------------------------------
You will also need the 3 .png images included in the .zip file here to show correctly
https://1drv.ms/u/s!AnxboyZ6ZdpRg_0Y43MhN54UpvK2hw

Image

Thank you for this great community
Alex
Last edited by DRocks on 19 Feb 2019, 08:52, edited 1 time in total.
burque505
Posts: 1732
Joined: 22 Jan 2017, 19:37

Re: Another Custom MsgBox for AHK_L v1

18 Feb 2019, 16:30

@DRocks, it works great and looks really good on Win7 64-bit, AH_L 1.1.30.01 64-bit.
Thank you!
Regards,
burque505
DRocks
Posts: 565
Joined: 08 May 2018, 10:20

Re: Another Custom MsgBox for AHK_L v1

18 Feb 2019, 17:33

Thanks burque, I really appreciate your feedback :)
I can't test on Win7 so this info is precious
User avatar
Frosti
Posts: 426
Joined: 27 Oct 2017, 14:30
Contact:

Re: Another Custom MsgBox for AHK_L v1

19 Feb 2019, 07:32

A very nice MsgBox, but what happens if I want to use a larger font? I have to scale the images?
DRocks
Posts: 565
Joined: 08 May 2018, 10:20

Re: Another Custom MsgBox for AHK_L v1

19 Feb 2019, 07:41

Good point Frosti, thanks for pointing.
I have tried font s12 yesterday and it was already too big for longer texts.

Yes my immediate thought would be to open the button shadow picture which is in fact the only picture to give the impression of a button. And I would simply stretch it the desired size. In my case I use Affinity photo which is not destructive even with .png so I guess you could modify those image too. But I can add more pic sizes when I get time.

The other step will be to tell what size the new bigger buttons will be so that the text will be as wide and tall as the button full size. (This text is responsible for the gLabel events)

For now the buttons are fixed at w140 but it would be easy to recalculate with bigger pics

As for the font itself it is a matter of changing the only font command to affect all text bellow it. Try s24 and a lets say 900w 300h msgbox window size. All will scale already except pictures
DRocks
Posts: 565
Joined: 08 May 2018, 10:20

Re: Another Custom MsgBox for AHK_L v1

19 Feb 2019, 08:53

Code is updated to v0.2 in original post for basic implementation of auto stretching with big font sizes - @Frosti
DRocks
Posts: 565
Joined: 08 May 2018, 10:20

Re: Another Custom MsgBox for AHK_L v1

20 Feb 2019, 08:01

@moderators, for some reason I cant post my v0.3 code update CloudFlare blocking suspicious content?
There is nothing else but code and comments, no sexual content, no discriminatory content, nothing... why blocked?
redgum
Posts: 6
Joined: 30 Oct 2014, 20:05

Re: Another Custom MsgBox for AHK_L v1

19 Apr 2020, 02:40

What happened to this? Is v0.3 available? Or v1.0? :-)
DRocks
Posts: 565
Joined: 08 May 2018, 10:20

Re: Another Custom MsgBox for AHK_L v1

19 Apr 2020, 15:29

@redgum Thanks for asking, no I have not reworked on this since :D

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: crossgamma and 158 guests