Download code

Jump to: navigation, search

Back to Dynamic_tray_icon_(Windows)

Download for Windows: single file, zip

Download for UNIX: single file, zip, tar.gz, tar.bz2

dynamic_tray_icon.c

  1 /* The authors of this work have released all rights to it and placed it
  2 in the public domain under the Creative Commons CC0 1.0 waiver
  3 (http://creativecommons.org/publicdomain/zero/1.0/).
  4 
  5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  6 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  7 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  8 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  9 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 10 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 11 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 12 
 13 Retrieved from: http://en.literateprograms.org/Dynamic_tray_icon_(Windows)?oldid=4224
 14 */
 15 
 16 
 17 /*
 18 **
 19 ** Dynamic tray icon example
 20 **
 21 ** Link with: shell32.lib
 22 */
 23 
 24 #include <windows.h>
 25 #include <shellapi.h>
 26 #include <stdio.h>
 27 
 28 //------------------------------------------------------------------------------------------------------------------
 29 // replaceicon() - draws stuff on top of a template icon. Destroys the input icon and returns a new one
 30 //
 31 // Inputs
 32 // 						hIcon  : Handle to a icon
 33 // 						value  : 0 - 100
 34 //						clear  : 1 = clear icon first, 0 = don't clear icon first
 35 // returns
 36 // 						Failure: 0
 37 //						Success: Handle to a icon
 38 //------------------------------------------------------------------------------------------------------------------
 39 
 40 HICON replaceicon(HICON hIcon, int value, BOOL clear)
 41 {
 42   ICONINFO  uInfo;
 43   INT       lRes;
 44   HDC       hMemDc;
 45   HGDIOBJ   hOldBMP;
 46   HICON     hNewIcon;
 47   RECT      rectDraw;
 48   BITMAP    uBitmap;
 49   LOGFONT   font;
 50   HBRUSH    hBrush1;
 51   HBRUSH    hBrush2;
 52 
 53   lRes = GetIconInfo(hIcon, &uInfo);
 54   if(!lRes)
 55     return 0;
 56 
 57   hMemDc = CreateCompatibleDC(NULL);
 58   if(!hMemDc)
 59     return 0;
 60 
 61   if(!GetObject(uInfo.hbmColor , sizeof(BITMAP), &uBitmap))
 62     return 0;
 63 
 64   rectDraw.left  =                   0 ; rectDraw.top                       = 0 ;
 65   rectDraw.right = uBitmap.bmWidth - 1 ; rectDraw.bottom = uBitmap.bmHeight - 1 ;
 66 
 67   hOldBMP = SelectObject(hMemDc, uInfo.hbmColor);
 68 
 69   // ---
 70 
 71   RECT		bar;
 72 
 73   bar.left      = rectDraw.left;
 74   bar.right     = rectDraw.right;
 75   bar.top       = rectDraw.bottom  - (value * ((rectDraw.bottom - rectDraw.top)) / 100);
 76   bar.bottom    = rectDraw.bottom;
 77 
 78   if(clear)
 79     FillRect(hMemDc,&rectDraw,(HBRUSH) (hBrush1=CreateSolidBrush(RGB( 64,  0, 64 ))));
 80 
 81   FillRect(hMemDc,&bar,     (HBRUSH) (hBrush2=CreateSolidBrush(RGB( 220,32,255 ))));
 82 
 83   if(hBrush1)
 84     DeleteObject(hBrush1);
 85   if(hBrush2)
 86     DeleteObject(hBrush2);
 87 
 88   // ---
 89 
 90   char string[8];
 91 
 92   font.lfHeight             = uBitmap.bmHeight;
 93   font.lfWidth              = uBitmap.bmWidth/4;
 94   font.lfEscapement         = 0;
 95   font.lfOrientation        = 0;
 96   font.lfWeight             = 0; //FW_BOLD;
 97   font.lfItalic             = 0;
 98   font.lfUnderline          = 0;
 99   font.lfStrikeOut          = 0;
100   font.lfCharSet            = ANSI_CHARSET;
101   font.lfOutPrecision       = OUT_DEFAULT_PRECIS;
102   font.lfClipPrecision      = CLIP_DEFAULT_PRECIS;
103   font.lfQuality            = ANTIALIASED_QUALITY; //DEFAULT_QUALITY;
104   font.lfPitchAndFamily     = DEFAULT_PITCH;
105   strcpy(font.lfFaceName,   "Tahoma");
106 
107        SelectObject(hMemDc, CreateFontIndirect(&font));
108           SetBkMode(hMemDc, TRANSPARENT);
109             SetROP2(hMemDc, R2_COPYPEN);
110   SetStretchBltMode(hMemDc, COLORONCOLOR);
111        SetTextColor(hMemDc, RGB(235,235,255));
112             sprintf(string, "%d",value);
113 
114   lRes = DrawText(hMemDc, string, strlen(string), &rectDraw, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
115 
116   // ---
117 
118   if(!lRes)
119     return 0;
120 
121   if(NULL || HGDI_ERROR == SelectObject(hMemDc, hOldBMP))
122     return 0;
123 
124   hNewIcon = CreateIconIndirect(&uInfo);
125 
126   if(!hNewIcon)
127     return 0;
128 
129   DestroyIcon(hIcon);
130   hIcon = hNewIcon;
131 
132   if(hMemDc)
133    DeleteDC(hMemDc);
134 
135   if(uInfo.hbmMask)
136     DeleteObject(uInfo.hbmMask);
137 
138   if(uInfo.hbmColor)
139     DeleteObject(uInfo.hbmColor);
140 
141   return hIcon;
142 }
143 
144 //------------------------------------------------------------------------------------------------------------------
145 // Add, modify or removes the tray icon. A value of 0 means remove
146 //------------------------------------------------------------------------------------------------------------------
147 
148 HICON settrayicon(HICON hIcon,int value)
149 {
150   NOTIFYICONDATA   icon;
151   static BOOL      firstrun=1;
152 
153   ZeroMemory(&icon,sizeof(NOTIFYICONDATA));
154   icon.cbSize               = sizeof(NOTIFYICONDATA);
155   icon.uID                  = 0;
156   icon.hWnd                 = GetDesktopWindow();
157   icon.uFlags               = NIF_ICON | NIF_TIP | NIF_MESSAGE;
158   icon.hIcon                = hIcon;
159   sprintf(icon.szTip,"Value = %d",value);
160 
161   icon.hIcon = replaceicon(icon.hIcon, value, 1); // Modify the icon
162 
163   if(!icon.hIcon)
164     printf("***ERROR: replaceicon()\n"), exit(0);
165 
166   Shell_NotifyIcon(value==0?NIM_DELETE:firstrun?NIM_ADD,firstrun=0:NIM_MODIFY,&icon);
167   Sleep(0);
168 
169   return icon.hIcon;
170 }
171 
172 //------------------------------------------------------------------------------------------------------------------
173 // Entry point...
174 //------------------------------------------------------------------------------------------------------------------
175 
176 int main(void)
177 {
178   HICON		icon;
179 
180   icon = LoadImage(0,"c:\\some_icon.ico",IMAGE_ICON,32,32,LR_LOADFROMFILE);
181 
182   for(int x=0; x<100; x+=3)
183   {
184     icon = settrayicon(icon,x); // add/update the icon
185     sleep(200);                 // Let us se what happens
186   }
187 
188   settrayicon(icon,0); // remove the icon
189   return 0;
190 }
191 
192 
193 


hijacker
hijacker
hijacker
hijacker

build.log

1 /tmp/litprog372940/dynamic_tray_icon.c:24:21: fatal error: windows.h: No such file or directory
2 compilation terminated.