Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 5th, 2012  |  syntax: Autohotkey  |  size: 8.30 KB  |  hits: 26  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include <CGUI>
  2.  
  3. Notify(Title, Text, Timeout = "", Icon = "", Action = "", Progress = "", Style = "")
  4. {
  5.         return new CNotificationWindow(Title, Text, Icon, Timeout, Action, Progress, Style)
  6. }
  7. Class CNotification
  8. {
  9.         static Windows := Array()
  10.         static pToken :=  Gdip_Startup() ; Start gdi+
  11.         ;Default style used for a notification window. An instance can be supplied to the constructor to use a specific style
  12.         Class Style
  13.         {
  14.                 static BackgroundColor := "333333"
  15.  
  16.                 static Radius := 9
  17.                 static Transparency := "220"
  18.                 Class Title
  19.                 {
  20.                         static FontSize := 8
  21.                         static FontWeight := 625
  22.                         static FontColor := "DDDDDD"
  23.                         static Font := "Arial"
  24.                 }
  25.                 Class Text
  26.                 {
  27.                         static FontSize := 8
  28.                         static FontColor := "DDDDDD"
  29.                         static FontWeight := 550
  30.                         static Font := "Arial"
  31.                 }
  32.  
  33.                 Class Border
  34.                 {
  35.                         static Color := 0xAA000000
  36.                         static Width := 2
  37.                         static Radius := 13
  38.                         static Transpacency := 105
  39.                 }
  40.  
  41.                 static ImageWidth := 32
  42.                 static ImageHeight := 32
  43.  
  44.                 Class Progress
  45.                 {
  46.                         ;Width := 350
  47.                         static Color := "Default"
  48.                         static BackgroundColor := "Default"
  49.                 }
  50.                 __new()
  51.                 {
  52.                         this.Title := new this.Title()
  53.                         this.Text := new this.Text()
  54.                         this.Border := new this.Border()
  55.                         this.Progress := new this.Progress()
  56.                 }
  57.         }
  58.  
  59.         ;Class used to describe a progress bar in a notification window. Can be passed to the constructor to show a progress bar
  60.         Class CProgress
  61.         {
  62.                 Min := 0
  63.                 Max := 100
  64.                 Value := 0
  65.                 __new(Min, Max, Value)
  66.                 {
  67.                         this.Min := Min
  68.                         this.Max := Max
  69.                         this.Value := Value
  70.                 }
  71.         }
  72.         RegisterNotificationWindow(NotificationWindow)
  73.         {
  74.                 ;msgbox % this.Windows.MaxIndex() ": " this.Windows[this.Windows.MaxIndex()].Y
  75.                 if(this.Windows.MaxIndex())
  76.                         Y := this.Windows[this.Windows.MaxIndex()].Y - NotificationWindow.WindowHeight
  77.                 else
  78.                 {
  79.                         SysGet, Mon, MonitorWorkArea, %mon%
  80.                         this.WorkspaceArea := {Left: MonLeft, Top : MonTop, Right : MonRight, Bottom : MonBottom}
  81.                         Y := this.WorkspaceArea.Bottom - NotificationWindow.WindowHeight
  82.                 }
  83.                 this.Windows.Insert(NotificationWindow)
  84.                 ;msgbox % objmaxindex(this.Windows)
  85.                 NotificationWindow.OnClose.Handler := new Delegate(this, "OnClose")
  86.                 if(NotificationWindow.Timeout)
  87.                         SetTimer, CNotification_CloseTimer, -10
  88.                 return {X : this.WorkspaceArea.Right - NotificationWindow.WindowWidth, Y : Y}
  89.         }
  90.         OnClose(Sender)
  91.         {
  92.                 for index, Window in this.Windows
  93.                 {
  94.                         if(Window = Sender)
  95.                         {
  96.                                 this.Windows.Remove(Index)
  97.                                 this.CalculateTargetPositions()
  98.                                 SetTimer, CNotification_MoveWindows, -10
  99.                                 return
  100.                         }
  101.                 }
  102.         }
  103.         CalculateTargetPositions()
  104.         {
  105.                 Target := this.WorkspaceArea.Bottom
  106.                 Loop % this.Windows.MaxIndex()
  107.                 {
  108.                         ;msgbox % "Window " A_Index ": " Target - this.Windows[A_Index].WindowHeight
  109.                         Target := this.Windows[A_Index].Target := Target - this.Windows[A_Index].WindowHeight
  110.                 }
  111.         }
  112.         MoveWindows()
  113.         {
  114.                 Moved := false
  115.                 for index, Window in this.Windows
  116.                 {
  117.                         hwnd := Window.hwnd
  118.                         WinGetPos, , Y, , , ahk_id %hwnd%
  119.                         ;Y := Window.Y
  120.                         if((Distance := Window.Target - Y) > 0)
  121.                         {
  122.                                 Moved := true
  123.                                 Delta := (Distance > 50 ? 5 : Round(5 - 5/Distance))
  124.                                 Delta := Delta > Distance ? Distance : Delta
  125.                                 WinMove, ahk_id %hwnd%, , , % Y + Delta
  126.                                 ;Window.Y := Y + (Distance > 50 ? 5 : Round(5 - 5/Distance))
  127.                         }
  128.                 }
  129.                 if(Moved)
  130.                         SetTimer, CNotification_MoveWindows, -10
  131.         }
  132.         CloseTimer()
  133.         {
  134.                 StillWaiting := false
  135.                 for index, Window in this.Windows
  136.                 {
  137.                         if(Window.Timeout && A_TickCount > Window.CloseTime)
  138.                         {
  139.                                 Window.Remove("Timeout")
  140.                                 Window.Close()
  141.                                 continue
  142.                         }
  143.                         if(Window.Timeout)
  144.                                 StillWaiting := true
  145.                 }
  146.                 if(StillWaiting)
  147.                         SetTimer, CNotification_CloseTimer, -10
  148.         }
  149. }
  150. CNotification_MoveWindows:
  151. CNotification.MoveWindows()
  152. return
  153. CNotification_CloseTimer:
  154. CNotification.CloseTimer()
  155. return
  156.  
  157. Class CNotificationWindow Extends CGUI
  158. {
  159.         OnClick := new EventHandler()
  160.  
  161.         /*Creates a new notification. Parameters:
  162.         Title: Title
  163.         Text: Text. Supports links in markup language, see link control
  164.         Icon: Path or icon handle
  165.         Timeout: Empty for no timeout, otherwise timeout in ms.
  166.         OnClick: function or delegate to handle clicks on the notification. It can optionally accept two arguments that indicate the clicked link, URLorID and Index.
  167.         Progress: See CNotification.CProgress
  168.         Style: See CNotification.CStyle
  169.         */
  170.  
  171.         __new(Title, Text, Icon = "", Timeout = "", OnClick = "", Progress = "", Style = "")
  172.         {
  173.                 this.Timeout := Timeout * 1000
  174.                 this.CloseTime := A_TickCount + Timeout * 1000
  175.                 this.OnClick.Handler := OnClick
  176.                 this.AlwaysOnTop := true
  177.                 this.Border := false
  178.                 this.Caption := false
  179.                 this.ToolWindow := true
  180.                 ;this.SysMenu := false
  181.                 if(!Style)
  182.                         Style := CNotification.Style
  183.  
  184.                 this.WindowColor := Style.BackgroundColor
  185.                 this.Transparent := Style.Transparency
  186.  
  187.                 ;Background picture control to detect clicks on the GUI and render the border
  188.                 this.icoBackground := this.AddControl("Picture", "icoBackground", "x0 y0 w0 h0 0xE")
  189.                
  190.                 ;Offset from border
  191.                 Offset := Style.Border.Width > Style.Radius ? Style.Border.Width : Style.Radius
  192.                 if(Icon)
  193.                         this.icoIcon := this.AddControl("Picture", "icoIcon", "x" Offset " y" Offset " w" Style.ImageWidth " h" Style.ImageHeight " 0xE 0x40", Icon)
  194.                 ;Revert to basic functions here so the font can be set before the control is added
  195.                 Gui, % this.GUINum ":Font", % "s" Style.Title.FontSize " w" Style.Title.FontWeight " c" Style.Title.FontColor, % Style.Title.Font
  196.                 this.txtTitle := this.AddControl("Text", "txtTitle", Icon ? "x+5" : "", Title)
  197.                 this.txtTitle.AutoSize()
  198.                 ;Progress control
  199.                 if(Progress)
  200.                         this.prgProgress := this.AddControl("Progress", "prgProgress", "y+5 Range" Progress.Min "-" Progress.Max, Progress.Value)
  201.  
  202.                 ;Notification text is a link control
  203.                 Gui, % this.GUINum ":Font", % "s" Style.Text.FontSize " w" Style.Text.FontWeight " c" Style.Text.FontColor, % Style.Text.Font
  204.                 if(Text)
  205.                         this.lnkText := this.AddControl("Link", "lnkText", "y+5", Text)
  206.  
  207.                 ;Register and show window
  208.                 this.Position := CNotification.RegisterNotificationWindow(this)
  209.                 this.Show("NA")
  210.  
  211.                 ;Calculate width of progress bar
  212.                 w1 := this.HasKey("lnkText") ? this.lnkText.Width : 0
  213.                 w2 := this.txtTitle.Width
  214.                 width := w1 > w2 ? w1 : w2
  215.                 width := width > this.prgProgress.Width ? width : this.prgProgress.Width
  216.                 this.prgProgress.Width := width
  217.  
  218.                 ;Resize background picture control to fit whole window
  219.                 this.icoBackground.Size := {Width : this.Width, Height : this.Height}
  220.                
  221.  
  222.                 ;Draw border:
  223.                
  224.                 ;Create background brush and pen for border
  225.                 pBrushBack := Gdip_BrushCreateSolid(0x00000000)
  226.                 pPen := Gdip_CreatePen(Style.Border.Color, Style.Border.Width)
  227.                
  228.                 ;Create bitmap and graphics
  229.                 pBitmap := Gdip_CreateBitmap(this.icoBackground.Width, this.icoBackground.Height)
  230.                 G := Gdip_GraphicsFromImage(pBitmap)
  231.                
  232.                 ;Draw background and border, taking into account the pen width
  233.                 Gdip_FillRectangle(G, pBrushBack, 0, 0, this.icoBackground.Width, this.icoBackground.Height)
  234.                 Gdip_DrawRoundedRectangle(G, pPen, Style.Border.Width/2, Style.Border.Width/2, this.icoBackground.Width - Style.Border.Width * 2, this.icoBackground.Height - Style.Border.Width * 2, Style.Radius)
  235.                
  236.                 ;Create HBITMAP and set it to the picture control
  237.                 hBitmap := Gdip_CreateHBITMAPFromBitmap(pBitmap)
  238.                 SetImage(this.icoBackground.hwnd, hBitmap)
  239.                
  240.                 ;Cleanup
  241.                 Gdip_DeletePen(pPen)
  242.                 Gdip_DeleteBrush(pBrushBack)
  243.                 Gdip_DeleteGraphics(G)
  244.                 Gdip_DisposeImage(pBitmap)
  245.                 DeleteObject(hBitmap)
  246.  
  247.  
  248.                 ;Set region for rounded windows
  249.                 if(Style.Radius)
  250.                         this.Region := "0-0 w" this.WindowWidth " h" this.WindowHeight " R" Style.Radius * 2 "-" Style.Radius * 2
  251.                
  252.                 ;Register handlers for all controls
  253.                 for index, control in this.Controls
  254.                         if(control.Type != "Progress" && control.Type != "Link")
  255.                                 control.Click.Handler := new Delegate(this, "Click")
  256.         }
  257.         Click()
  258.         {
  259.                 this.Close()
  260.                 this.OnClick.()
  261.         }
  262.         lnkText_Click(URLOrID, Index)
  263.         {
  264.                 this.Close()
  265.                 this.OnClick.(URLOrID, Index)
  266.         }
  267.         __Set(Key, Value)
  268.         {
  269.                 if(Key = "Progress" && this.HasKey("prgProgress"))
  270.                         this.prgProgress.Value := Value
  271.                 else if(Key = "Text")
  272.                 {
  273.                         this.lnkText.Text := Value
  274.                         this.lnkText.AutoSize()
  275.                 }
  276.                 else if(Key = "Title")
  277.                 {
  278.                         this.txtTitle.Text := Value
  279.                         this.txtTitle.AutoSize()
  280.                 }
  281.                 else
  282.                         Ignore := true
  283.                 if(!Ignore)
  284.                         return Value
  285.         }
  286. }