Enhance your gmail notifier

Share your advanced PureBasic knowledge/code with the community.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Enhance your gmail notifier

Post by netmaestro »

[edit] Please check a few posts down for a much better version.

I wanted to write a replacement for the gmail notifier but I didn't have any luck. So I realized you can check the status of the notifier gmail provides and enhance it to be more visible. This isn't perfect by any means, and what I originally wanted to do was get the hDC of the notification icon and do some GetPixel_() on it, but I didn't have any luck with that either, so I'm reduced to counting blue pixels in the system tray. Pathetic, I know, but I'm hoping someone will suggest something better:

Code: Select all

;===============================================
; Program:     Gmail Notifier Enhancer
; Date:        July 18, 2006
; Author:      netmaestro
; TargetOS:    Windows 2000/XP
; Compiler:    PureBasic 4.0
;===============================================
;

CreateImage(0,640,480,#PB_Image_DisplayFormat)
StartDrawing(ImageOutput(0))
  b = RGB(0,0,160)
  Box(0,0,640,400,b)
  Box(30,30,580,340,#White)
  Box(200,185,240,30,b)
  LineXY(0,15, 200, 200, b)
  LineXY(15,0, 215, 185, b) 
  FillArea(32,32,b,b)
  LineXY(0,384,200,200,b)
  LineXY(15,399,230,200,b)
  FillArea(32,368,b,b)
  LineXY(624,0,425,186,b)
  LineXY(639,15,440,200,b)
  FillArea(442,197,b,b)
  LineXY(440,200,639,385,b)
  LineXY(424,214,610,385,b)
  FillArea(442,220,b,b)
StopDrawing()

Repeat
  Delay(60000) ; Check every one minute
  hTaskBar.l = FindWindow_("Shell_TrayWnd", #Null) 
  hSystemTray.l = FindWindowEx_(hTaskBar, 0, "TrayNotifyWnd", 0) 
  GetWindowRect_(hSystemTray, Tray.RECT) 
  
  dc=CreateDC_("DISPLAY",0,0,0) 
  yes = 0
  no  = 0
  For i = Tray\left To Tray\right
    For j = Tray\top To Tray\bottom
      p=GetPixel_(dc,i,j)
      If p = RGB(155,153,238) ; count the light blue px in systray
        no+1
      ElseIf p = RGB(0,0,205) ; count the dark blue px in systray
        yes+1
      EndIf
    Next
  Next
  DeleteDC_(dc) 

  If yes > 55 And no < 55 ; new mail: dark px > 55, light px usually 0 (here)
    win = OpenWindow(0,0,0,640,400,"",#PB_Window_BorderLess|#PB_Window_ScreenCentered)
    SetWindowLong_(win, #GWL_EXSTYLE, GetWindowLong_(win, #GWL_EXSTYLE) | #WS_EX_LAYERED)
    SetLayeredWindowAttributes_(win, 0, 128, 2)
    StickyWindow(0,1)
    CreateGadgetList(WindowID(0))
    ImageGadget(0,0,0,640,400,ImageID(0))
    cc=0
    Repeat
      EventID = WaitWindowEvent()
      If eventid=#WM_MOUSEMOVE
        cc+1
      EndIf
    Until cc > 10
    CloseWindow(0)
  EndIf
ForEver
Last edited by netmaestro on Tue Jul 25, 2006 1:56 am, edited 3 times in total.
BERESHEIT
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

Excellent :)
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

Thanks, I'm finding it very useful.

I knew there had to be a better way than counting blue pixels! This approach listens for the little gmail notification window sliding up and when it finds it, displays the graphic. So the big envelope will come out once each time a new message is announced by gmail, regardless of the color of the systray icon. It stays up until you touch it with the mouse. It seems perfect to me. The listening loop happens once every one second, and uses less than one ms to do the test. No cpu hit at all.

Code: Select all

;=============================================== 
; Program:     Google Notifier Enhancer 
; Date:        July 18, 2006 
; Author:      netmaestro 
; TargetOS:    Windows 2000/XP 
; Compiler:    PureBasic 4.0 
;=============================================== 
; 

*MyMutex = CreateMutex_(#Null, 1, "EnhancedGMailNotifier") 
If *MyMutex <> 0 And GetLastError_() = #ERROR_ALREADY_EXISTS 
  CloseHandle_(*MyMutex) 
  MessageRequester("Notice", "The Enhanced Gmail Notifier is already running!", #MB_ICONINFORMATION) 
  End 
EndIf 

Global gMessages = 0 
Global win 

Declare Envelope(void)
Declare StartWithWindows(state.b)

StartWithWindows(1)
  
CreateImage(0,640,480,#PB_Image_DisplayFormat) 
StartDrawing(ImageOutput(0)) 
  b = RGB(0,0,160) 
  Box(0,0,640,400,b) 
  Box(30,30,580,340,#White) 
  Box(200,185,240,30,b) 
  LineXY(0,15, 200, 200, b) 
  LineXY(15,0, 215, 185, b) 
  FillArea(32,32,b,b) 
  LineXY(0,384,200,200,b) 
  LineXY(15,399,230,200,b) 
  FillArea(32,368,b,b) 
  LineXY(624,0,425,186,b) 
  LineXY(639,15,440,200,b) 
  FillArea(442,197,b,b) 
  LineXY(440,200,639,385,b) 
  LineXY(424,214,610,385,b) 
  FillArea(442,220,b,b) 
StopDrawing() 

Global  hBrush = CreatePatternBrush_(ImageID(0)) 

CreateImage(1,200,80,#PB_Image_DisplayFormat) 
StartDrawing(ImageOutput(1)) 
  Box(0,0,200,80,#White) 
StopDrawing() 

CreateImage(2, 360,65,#PB_Image_DisplayFormat) 

LoadFont(0, "Arial", 60, #PB_Font_Bold|#PB_Font_HighQuality) 

Repeat 
  hTaskBar.l = FindWindow_("Shell_TrayWnd", #Null) 
  hSystemTray.l = FindWindowEx_(hTaskBar, 0, "TrayNotifyWnd", 0) 
  GetWindowRect_(hSystemTray, Tray.RECT) 
  pt.POINT 
  pt\x = Tray\right - 50 
  pt\y = Tray\top - 20 
  wc$ = Space(255) 
  win = WindowFromPoint_(pt\x,pt\y) 
  GetClassName_(win, @wc$, 254) 
  If wc$="RichEdit20W" 
    win2=GetParent_(win) 
    win3=GetParent_(win2) 
    GetClassName_(win3,@wc$,254) 
    If wc$ = "GoogleGmailNotifySysTray" 
      gMessages + 1 
      Delay(1500)
      
      ;*****************************************************
      GetWindowRect_(win2, @wr.RECT)
      srcDC = GetWindowDC_(win2) 
      trgDC = StartDrawing(ImageOutput(2)) 
        Box(0,0,360,65,RGB(227,226,229)) 
        BitBlt_( trgDC, 0, 0, wr\right-wr\left, wr\bottom-wr\top, srcDC, 0,0, #SRCCOPY) 
      StopDrawing()  
      ReleaseDC_(win2, srcDC)            
      ;*****************************************************
      
      If Not FindWindow_(0, "EnhancedgmailNotifier") 
        CreateThread(@Envelope(), win) 
      EndIf 
      Repeat 
        Delay(10) 
      Until IsWindow_(win) = #False 
    EndIf 
  EndIf 
  Delay(100) 
ForEver 

DeleteObject_(hBrush) 
End

Procedure Envelope(void)  
  cc = 0 : oldgM = 0 : alphas = 20 
  OpenWindow(0,0,0,640,400,"EnhancedgmailNotifier",#PB_Window_BorderLess|#PB_Window_ScreenCentered|#PB_Window_Invisible) 
  SetWindowLong_(WindowID(0), #GWL_EXSTYLE, GetWindowLong_(WindowID(0), #GWL_EXSTYLE) | #WS_EX_LAYERED | #WS_EX_TOOLWINDOW) 
  SetLayeredWindowAttributes_(WindowID(0), 0, alphas, #LWA_ALPHA) 
  CreateGadgetList(WindowID(0)) 
  ContainerGadget(0,0,0,640,400) 
  SetClassLong_(GadgetID(0), #GCL_HBRBACKGROUND, hBrush) 
  ImageGadget(1,225,90,200,80,ImageID(1)) 
  ImageGadget(2,144,304,360,65,ImageID(2)) 
  ShowWindow_(WindowID(0), #SW_SHOW) 
  StartTime = ElapsedMilliseconds() 
  While WindowEvent():Wend 
  SetForegroundWindow_(WindowID(0))
  StickyWindow(0,1) 
  Repeat 
    EventID = WaitWindowEvent(10) 
    If gMessages <> oldgM 
      oldgM = gMessages 
      StartDrawing(ImageOutput(1)) 
        Box(0,0,200,80,#White) 
        DrawingFont(FontID(0)) 
        DrawText(98 - TextWidth(Str(gMessages)) / 2, 0, Str(gMessages), RGB(0,0,160), #White) 
      StopDrawing() 
      SetGadgetState(1, ImageID(1)) 
      SetGadgetState(2, ImageID(2)) 
    EndIf 
    If ElapsedMilliseconds()-StartTime > 350 
      StartTime = ElapsedMilliseconds() 
      If alphas < 180 
        alphas + 1 
        SetLayeredWindowAttributes_(WindowID(0), 0, alphas, #LWA_ALPHA) 
      EndIf 
    EndIf 
    If EventID = #WM_MOUSEMOVE 
      If cc=0 
        GetCursorPos_(@cp1.POINT) 
      Else 
        GetCursorPos_(@cp2.POINT) 
      EndIf 
      cc+1 
      If cc > 5 
        If Abs(cp2\x - cp1\x) > 10 Or Abs(cp2\y - cp1\y) > 10 
          Break 
        EndIf 
      EndIf 
    EndIf 
  ForEver 
  gMessages = 0 
  CloseWindow(0) 
EndProcedure 

Procedure StartWithWindows(State.b) 
  Protected Key.l = #HKEY_CURRENT_USER ;or #HKEY_LOCAL_MACHINE for every user on the machine 
  Protected Path.s = "Software\Microsoft\Windows\CurrentVersion\Run" ;or RunOnce if you just want to run it once 
  Protected Value.s = "GmailEnhancer" ;Change into the name of your program 
  Protected String.s = Chr(34)+ProgramFilename()+Chr(34) ;Path of your program 
  Protected CurKey.l 
  If State 
    RegCreateKey_(Key,@Path,@CurKey) 
    RegSetValueEx_(CurKey,@Value,0,#REG_SZ,@String,Len(String)) 
  Else 
    RegOpenKey_(Key,@Path,@CurKey) 
    RegDeleteValue_(CurKey,@Value) 
  EndIf 
  RegCloseKey_(CurKey) 
EndProcedure
Last edited by netmaestro on Mon Feb 12, 2007 10:58 pm, edited 2 times in total.
BERESHEIT
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

Download exe compiled with icon: http://www.networkmaestro.com/GMailEnhancer.exe
Last edited by netmaestro on Mon Feb 12, 2007 10:59 pm, edited 2 times in total.
Tomred
New User
New User
Posts: 1
Joined: Thu Oct 19, 2006 12:52 am
Location: Vancouver, Canada

Post by Tomred »

Works great, thanks for sharing.
Tom

A complex system that does not work is invariably found to have evolved from a simpler system that worked just fine.
Post Reply