YellowLab Posted October 29, 2008 Posted October 29, 2008 (edited) I wanted a custom message box that would be centered on the application window when clicked rather than centered on the screen. I found this MsgBox by photonbuddy ( http://www.autoitscript.com/forum/index.php?showtopic=43832 ) and modified it. I thought I would share:expandcollapse popup;=========================================================================================== ;_CenteredMsg($title,$text,$button,$hWin,[$msgwidth,[$msgheight,[$buttonwidth,[$ntimeout]]]]) ; Where: ; $title = The title of the message box ; $text = The text to be displayed. Long lines will wrap. The text will also ; be centered. ; $button = The text for the buttons. Seperate buttons with the ; pipe (|) character. To set a button as the default button, ; place the ampersand (&) character before the word. If you ; put more than 1 ampersand character, the function will ; fail and return -1, plus set @error to 1 ; $hWin = The handle of the window the message box is to be centered in. ; $msgwidth = Width of the displayed window. This value will be automatically ; increased if the resulting window will not be wide enough to ; accommodate the buttons requested. Default is 300 ; $msgheight = Height of the displayed window. This is not adjusted. If the ; text is too big, it will not display it all. Default is 80 ; $buttonwidth= The width of the button in pixels. Default is 80 ; ; Success: Returns the button pressed, starting at 1, counting from the LEFT. ; Returns 0 is the Message Box was closed via a "CloseGUI" event, i.e. click ; the close box or press Escape. ; Failure: Returns -1 if Message Box was not created due to more than 1 default button ; being set or other GUI error. Error is set to 1. ;=========================================================================================== Func _CenteredMsg($title, $text, $button, $hWin, $msgwidth=300,$msgheight=80,$buttonwidth=80,$ntimeout=-1) Local $nOldOpt = Opt('Guioneventmode',0) Local $buttonarray=StringSplit($button,"|") Local $msgButton[$buttonarray[0]+1] Local $buttoncount,$defbutton=0, $retvalue, $buttonwork, $buttonxpos, $mmsg Local $arTemp=WinGetPos($hWin) If StringInStr($button,"&",0,2)<>0 Then;if two buttons are showing focus $nOldOpt = Opt('Guioneventmode',$nOldOpt) SetError(1) Return -1 EndIf If ($buttonwidth+8)*$buttonarray[0]+8>$msgwidth Then;if button width exceeds window size, resize window $msgwidth=($buttonwidth+8)*$buttonarray[0]+8 EndIf Local $msggui = GUICreate($title, $msgwidth, $msgheight, ($arTemp[2]-$msgwidth)/2+$arTemp[0]-3,($arTemp[3]-$msgheight)/2+$arTemp[1]-16, _ BitOr(0x80880000,0x00C00000)); $WS_POPUPWINDOW & $WS_CAPTION If $msggui=0 Then $nOldOpt = Opt('Guioneventmode',$nOldOpt) SetError(1) Return -1 EndIf Local $hDummy=GUICtrlCreateLabel($text, 8, 8, $msgwidth-16, $msgheight-40) $buttonxpos=(($msgwidth/$buttonarray[0])-($buttonwidth))/2 For $buttoncount=0 To $buttonarray[0]-1 $buttonwork=$buttonarray[$buttoncount+1] If StringLeft($buttonwork,1)="&" Then $defbutton=0x0001;$BS_DEFPUSHBUTTON $buttonarray[$buttoncount+1]=StringTrimLeft($buttonwork,1) EndIf $msgbutton[$buttoncount] = GUICtrlCreateButton($buttonarray[$buttoncount+1], $buttonxpos+($buttoncount*$buttonwidth)+($buttoncount*$buttonxpos*2), _ $msgheight-32, $buttonwidth, 24,$defbutton) $defbutton=0 Next GUISetState() Dim $timeout If $ntimeout <> -1 Then $timeout = TimerInit() While 1 $mmsg = GUIGetMsg() If $mmsg > 2 Then $retvalue = $mmsg-$hDummy;ctrl number of button minus the ctrl number of the label will result in button 1 = 1, button 2 = 2 Else Select Case $mmsg = -3;$GUI_EVENT_CLOSE = -3 GUIDelete($msggui) $nOldOpt = Opt('Guioneventmode',$nOldOpt) Return 0 Case Else If TimerDiff($timeout)/1000 >= $ntimeout AND $ntimeout <> -1 Then GUIDelete($msggui) $nOldOpt = Opt('Guioneventmode',$nOldOpt) Return 0 EndIf EndSelect EndIf If $retvalue <> '' Then ExitLoop WEnd $nOldOpt = Opt('Guioneventmode',$nOldOpt) GUIDelete($msggui) Return $retvalue EndFunc;==> _My_DisplayMsg()Here is a sample script using the function:$hGUI=GUICreate("My Test",300,600,200,200) $hButton=GUICtrlCreateButton("Test",10,10,50,25) $arTemp=WinGetPos($hGUI) GUISetState(@SW_SHOW, $hGUI) While 1 $nMsg = GUIGetMsg() Switch $nMsg Case -3;$GUI_EVENT_CLOSE = -3 Exit Case $hButton $nValue=_CenteredMsg("My Test Message","This is Just a Test","&1|2|3|4|5",$hGUI,400,100,50) ConsoleWrite($nValue&@CRLF) EndSwitch WEndI hope someone finds this useful.Bob Edited October 29, 2008 by YellowLab You can't see a rainbow without first experiencing the rain.
TehWhale Posted October 29, 2008 Posted October 29, 2008 Nice! It should also have a flag so that it will set the parent of the GUI to the HWND.
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