Notification popup window in the style of the Windows 7 system tray balloon

Post your working scripts, libraries and tools for AHK v1.1 and older
fpl9000
Posts: 10
Joined: 30 Aug 2016, 11:42

Notification popup window in the style of the Windows 7 system tray balloon

30 Aug 2016, 12:27

Since Windows 10 has replaced the classic system tray balloon (as displayed by TrayTip) with the uglier and less functional slide-in alerts that don't behave at all like the classic system tray balloons, I wrote this function that pops up a small notification window in the lower right corner of the screen that disappears after 5 seconds (but that's configurable). I tried to make it look similar to the classic system tray balloon. To use this, you'll have to provide your own icon file referenced by the Gui Add Picture ... command in my code. Configurable options (see function parameter OPTIONS) include the ability to specify the X and Y offset of the notification window, the color of the text, the delay (in milliseconds) before the window is closed, the ability to disable word-wrap for the text, and the ability to hide the title. The window closes if you click on the text or title. You can use a hotkey to close it like this:

Code: Select all

CapsLock::
PopupClose()
;; Delete the timer set by Popup() so it doesn't close the next popup to appear.
SetTimer PopupClose, Delete
Return
I hope someone finds this useful.

Image

Code: Select all

;; Pop up a notification window in the lower right corner of the screen showing TEXT having TITLE.
;; TEXT is word-wrapped to fit in the fixed-width notification window unless option "nowrap" is
;; given (see below).  The window's vertical position is adjusted upwards so the amount of TEXT
;; doesn't force it to extend off the bottom of the screen.  OPTIONS is a comma-separated (with no
;; space around the commas) string of zero or more of the following:
;;
;;   delay=N     => Close the popup window in N milliseconds.  If N is 0, the window is shown
;;                  indefinitely, until PopupClose() or another call to Popup() replaces it.
;;
;;   xoffset=N   => Position the popup window N pixels from the left edge of the screen. N cannot
;;                  be negative.
;;
;;   yoffset=N   => Position the popup window N pixels from the top edge of the screen.  N cannot
;;                  be negative.  This option disables auto-adjustment of the popup position to
;;                  prevent the window from extending off the bottom of the screen, so the caller
;;                  is responsible to avoid that.
;;
;;   color=COLOR => Set the text color to COLOR, which must be a hex color value (e.g., 0xffeecc)
;;                  or a color name known to AutoHotkey.
;;
;;   nowrap      => Don't word-wrap long lines of TEXT.  This dynamically sizes the window's width
;;                  to fit TEXT and TITLE.  If TEXT or TITLE is too long, the window can extend
;;                  past the right edge of the screen.  Option "xoffset" can help avoid that.
;;
;;   notitle     => Do not show the title and icon.
;;
;; Unrecognized options are silently ignored.  Clicking on the TITLE or TEXT closes the popup.
;; Examples:
;;
;;   Popup("Good morning!", "Greeting", "delay=10000,color=blue,yoffset=100")
;;   Popup("Time to leave!", "Alert", "delay=0")

Popup(TEXT, TITLE := "AutoHotkey Info", OPTIONS := "")
{
        ;; Parse OPTIONS.
        OPTIONS := "," . OPTIONS . ","
        NOTITLE := InStr(OPTIONS, ",notitle,")
        NOWRAP := InStr(OPTIONS, ",nowrap,")

        If (RegExMatch(OPTIONS, ",delay=([0-9]+),", DELAY))
                DELAY := DELAY1
        Else
                DELAY := 5000

        If (RegExMatch(OPTIONS, ",color=([^,]+),", COLOR))
                COLOR := COLOR1
        Else
                COLOR := "0x000000"

        If (RegExMatch(OPTIONS, ",xoffset=([0-9]+),", XOFFSET))
                XOFFSET := XOFFSET1
        Else
                XOFFSET := A_SCREENWIDTH - 310

        If (RegExMatch(OPTIONS, ",yoffset=([0-9]+),", YOFFSET))
                YOFFSET := YOFFSET1

        If (TITLE == "")
                TITLE := "AutoHotkey Info"

        If (!NOWRAP)
                WIDTH := "W250"

        ;; Count the number of lines in TEXT.  This doesn't modify TEXT.
        LINES := StrSplit(TEXT, "`n", "`n")

        If (!YOFFSET)
        {
                ;; The caller did not specify a Y offset, so we compute one.
                YOFFSETBASE := 105      

                If (NOWRAP)
                {
                        ;; No word-wrapping is happening.  The window is as wide as the text
                        ;; requires.  Move the window up by 15 pixels for each line of text.
                        YOFFSET := A_SCREENHEIGHT - YOFFSETBASE - 15 * LINES.Length()
                }
                Else
                {
                        ;; We have word-wrapped text in a fixed width window that is WIDTH pixels
                        ;; wide.  Set YOFFSET based on how many wrapped lines will appear.
                        YOFFSET := A_SCREENHEIGHT - YOFFSETBASE

                        For INDEX, VALUE in LINES
                        {
                                ;; Move the window 15 pixels up for each line of wrapped text.
                                YOFFSET := YOFFSET - 15 * (StrLen(VALUE) // 46 + 1)
                        }
                }
        }

        ;; Create the base window.
        Gui MyPopup: New
        Gui MyPopup: +AlwaysOnTop +Owner -Resize -Caption +Border
        Gui MyPopup: Color, 0xF9F9FB  ;; Background color that matches system tray balloon.

        TEXTPOSITION := ""

        If (!NOTITLE)
        {
                ;; When TITLE is shown, position the text below the icon and TITLE.
                TEXTPOSITION := "X12"

                ;; Show TITLE to the right of a 16x16 icon.
                Gui MyPopup: Add, Picture, W16 H-1 GPopupClose, %ICONSDIR%\yellow-button.ico
                Gui MyPopup: Font, s11 c0x000088, Verdana
                Gui MyPopup: Add, Text, R1 XP+23 YP-2 GPopupClose, %TITLE%
        }

        ;; Create the text control showing TEXT.
        Gui MyPopup: Font, s9 c%COLOR%, Segoe UI
        Gui MyPopup: Add, Text, %TEXTPOSITION% %WIDTH% GPopupClose, %TEXT%
        Gui MyPopup: Show, AutoSize NoActivate X%XOFFSET% Y%YOFFSET%

        ;; If DELAY is 0, display the popup window indefinitely.  Call PopupClose() to close it.
        If (DELAY == 0)
                Return

        ;; Close the popup window DELAY milliseconds from now.
        SetTimer PopupClose, -%DELAY%
}

PopupClose()
{
        Gui MyPopup: Destroy
}

PopupError(TEXT)
{
        Popup(TEXT, "AutoHotkey Error")
}
fpl9000
Posts: 10
Joined: 30 Aug 2016, 11:42

Re: Notification popup window in the style of the Windows 7 system tray balloon

31 Aug 2016, 07:19

Here's a small improvement:

Code: Select all

PopupClose()
{
        Gui MyPopup: Destroy
        SetTimer PopupClose, Delete
}
By cancelling the close timer in PopupClose, the caller doesn't have to do it.

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: Bing [Bot] and 121 guests