#include #systray.asm -- shows how an app can be placed and used from the system tray #Basically we fill a NOTIFYICONDATA structure and call Shell_NotifyIcon #to set up or modify or delete the tray icon. A user defined message WM_ICONNOTIFY #(could be named anything, of course) is defined in the WM_USER range , arbitrarily #chose 1135 here. When the user clicks, dblclicks etc on the systray icon #Windows sends this message to our window's window proc. When we receive this #message we just respond to it appropriately -- show a menu,exit, show the window #etc. That's it ! Just a few lines of code and we can use the cool system tray. #Yes EP IS easy to use and one can be very productive using it #Please drop me an email for any clarifications,suggestions,comments begin imports #many frequently used functions are here [Kernel32.dll] GetLocalTime GetLastError WaitForSingleObject CreateEventA GetModuleHandleA GetCommandLineA LoadLibraryA ExitProcess GlobalAlloc GlobalSize GlobalFree GlobalReAlloc Sleep HeapCreate HeapAlloc HeapFree CreateFileA ReadFile WriteFile CloseHandle GetFileSize CreateThread CreateProcessA SuspendThread ResumeThread ExitThread TerminateThread [user32.dll] RegisterClassExA CreateWindowExA ShowWindow UpdateWindow DefWindowProcA GetMessageA TranslateMessage DispatchMessageA SendMessageA LoadCursorA LoadIconA LoadMenuA BeginPaint EndPaint DrawTextA GetClientRect PostQuitMessage MessageBoxA SetForegroundWindow SetActiveWindow BringWindowToTop FindWindowA DestroyWindow PostMessageA CreatePopupMenu AppendMenuA SetMenu CreateMenu GetCursorPos TrackPopupMenu SetFocus [gdi32.dll] CreateFontIndirectA Rectangle CreateSolidBrush SelectObject SetBkMode ExtFloodFill CreatePen SetTextColor [comctl32.dll] InitCommonControls [shell32.dll] Shell_NotifyIcon [comdlg32.dll] GetOpenFileNameA GetSaveFileNameA [crtdll.dll] fopen fclose fgets fputs fgetc fputc feof malloc realloc free fread fwrite fprintf fscanf _itoa memcpy memcmp memmove memset sprintf atoi strcpy strstr strncat end imports #-----------------Constants---------------------------------------------- begin const #many commonly used constants are here SW_HIDE 0 SW_SHOW 5 WS_CHILD 1073741824 WS_VISIBLE 268435456 WS_BORDER 8388608 WS_HSCROLL 1048576 WS_VSCROLL 2097152 WS_CAPTION 12582912 WS_CHILDWINDOW 1073741824 WS_OVERLAPPEDWINDOW 13565952 WS_POPUPWINDOW 2156396544 WS_SYSMENU 524288 WS_CLIENTEDGE 512 WS_MINIMIZEBOX 131072 WS_MAXIMIZEBOX 65536 WS_SIZEBOX 262144 WM_CREATE 1 WM_COPY 769 WM_CUT 768 WM_PASTE 770 WM_CLOSE 16 WM_COMMAND 273 WM_DESTROY 2 WM_ENABLE 10 WM_GETTEXT 13 WM_GETTEXTLENGTH 14 WM_INITDIALOG 272 WM_INITMENU 278 WM_KEYDOWN 256 WM_KEYUP 257 WM_KILLFOCUS 8 WM_LBUTTONDBLCLK 515 WM_LBUTTONDOWN 513 WM_LBUTTONUP 514 WM_MOVE 3 WM_NOTIFY 78 WM_PAINT 15 WM_ACTIVATE 6 WM_QUIT 18 WM_RBUTTONDBLCLK 518 WM_RBUTTONDOWN 516 WM_RBUTTONUP 517 WM_SETFOCUS 7 WM_SETFONT 48 WM_SETTEXT 12 WM_SIZE 5 WM_TIMER 275 WM_UNDO 772 BS_AUTOCHECKBOX 3 BS_AUTORADIOBUTTON 9 BS_GROUPBOX 7 LB_ADDSTRING 384 LB_RESETCONTENT 388 LB_SELECTSTRING 396 LB_SETTOPINDEX 407 LBS_HASSTRINGS 64 DT_SINGLELINE 32 DT_CENTER 1 DT_VCENTER 4 DT_BOTTOM 8 IDI_APPLICATION 32512 IDC_CROSS 32515 ES_AUTOVSCROLL 64 ES_AUTOHSCROLL 128 ES_MULTILINE 4 ES_WANTRETURN 4096 ES_LEFT 0 ES_NOHIDESEL 256 EN_MSGFILTER 1792 EN_CHANGE 768 ENM_MOUSEEVENTS 131072 ENM_SELCHANGE 524288 ENM_PROTECTED 2097152 ENM_KEYEVENTS 65536 ENM_UPDATE 2 ENM_SCROLL 4 ENM_CHANGE 1 EM_STREAMIN 1097 EM_STREAMOUT 1098 EM_GETFIRSTVISIBLELINE 206 EM_FINDTEXTEX 1103 EM_LINEFROMCHAR 201 EM_LINESCROLL 182 EM_SETLIMITTEXT 197 EM_SETEVENTMASK 1093 MF_ENABLED 0 MF_BYPOSITION 1024 MF_SEPARATOR 2048 MF_STRING 0 MF_POPUP 16 MF_DISABLED 2 MF_BYCOMMAND 0 GENERIC_READ 2147483648 GENERIC_WRITE 1073741824 OPEN_EXISTING 3 FILE_ATTRIBUTE_NORMAL 128 GMEM_ZEROINIT 64 SFF_SELECTION 32768 SFF_PLAINRTF 16384 SF_TEXT 1 SF_RTF 2 CB_ADDSTRING 323 CB_DELETESTRING 324 CB_GETCOUNT 326 CB_GETLBTEXT 328 CB_GETLBTEXTLEN 329 CB_INSERTSTRING 330 CB_RESETCONTENT 331 CB_GETCURSEL 327 CB_SETCURSEL 334 CB_FINDSTRINGEXACT 344 CBS_DROPDOWN 2 CBS_DROPDOWNLIST 3 CBS_HASSTRINGS 512 CBS_SORT 256 CBS_SIMPLE 1 CBN_DROPDOWN 7 CBN_SELENDOK 9 CBN_SELCHANGE 1 CBN_CLOSEUP 8 OFN_EXPLORER 524288 OFN_HIDEREADONLY 4 OFN_LONGNAMES 2097152 OFN_PATHMUSTEXIST 2048 HEAP_ZERO_MEMORY 8 MB_ICONASTERISK 64 WM_ICONNOTIFY 1135 NIF_ICON 2 NIM_ADD 0 NIM_DELETE 2 SIZE_MINIMIZED 1 NIF_MESSAGE 1 NIF_TIP 4 WM_LBUTTONDBLCLK 515 WS_POPUP 2147483648 end const #-----------------Data Section------------------------------------------- #some useful predefined variables are here begin data dword hwndMain 0 string Class Window dword hInstance 0 string ClassName WinClass string AppName Windows Template ! string MenuName Menu string btn button string text Edit string combo combobox string list listbox string label static string colon : dword quit 0 string font1 ms sans serif string font2 Courier New sequence fon1 60 sequence fon2 60 dword hfont1 0 dword hfont2 0 string systray Using the system tray string ofnfilter All Files string read r string write w string readb rb string writeb wb string WindowCaption Using the system tray ! string ab About ... string ab1 MyApp Version 1.0 string ab2 by .... string ab3 ---------------------------------------------------- string spc string spc6 dword hmenu 0 dword hfilemenu 0 dword heditmenu 0 dword hviewmenu 0 dword hhelpmenu 0 string fmenu &File string new &New string open &Open string show &Show Window string hyphen - string exit E&xit string emenu &Edit string undo &Undo string cut &Cut string copy C&opy string paste &Paste string find &Find... string replace &Replace... string selall &Select All string pmenu &Help string about &About ... sequence buffer 256 sequence buf 48 sequence listbuf 48 sequence icobuf 24 sequence pt 8 sequence ofnfname 256 sequence blank 1 dword CommandLine 0 dword hmem 0 sequence wndclass 16 sequence msg 28 sequence ibuf 32 dword mem 0 dword hicon 0 sequence nidata 88 dword dataend 4294967295 end data #------------------------------------------------------------------------- begin code #the real code begin here ! proc WndProc dword hwnd dword msg dword wparam dword lparam dword fn 0 dword hdc 4 sequence ps 64 sequence rect 16 dword ordata 0 dword n 0 dword m 0 dword x 0 dword y 0 sequence pt 8 push ebp mov ebp esp add esp -180 cmp msg {WM_CREATE} je wmcreate cmp msg {WM_PAINT} je wmpaint cmp msg {WM_COMMAND} je wmcommand cmp msg {WM_SIZE} je wmsize cmp msg {WM_ICONNOTIFY} je wmiconnotify cmp msg {WM_DESTROY} jne default invoke Shell_NotifyIcon {NIM_DELETE} nidata #delete systray icon on exit push 0 call PostQuitMessage jmp exit wmcreate: #setting up the systray icon on start; basic window starts invisible [nidata]=88 #filling the NOTIFYICONDATA structure eax=hwnd [nidata+4]=eax [nidata+8]=1 eax={NIF_ICON} or eax {NIF_TIP} #there is a tool top or eax {NIF_MESSAGE} #there is a user defined message [nidata+12]=eax [nidata+16]={WM_ICONNOTIFY} #user defined message eax=hicon #handle of icon loaded fron resource in winmain [nidata+20]=eax eax=nidata add eax 24 invoke strcpy eax systray #tool tip string invoke Shell_NotifyIcon {NIM_ADD} nidata jmp exit wmpaint: lea eax ps invoke BeginPaint hwnd eax mov hdc eax lea eax ps invoke EndPaint hwnd eax jmp exit wmcommand: #menu items if lparam = 0 if wparam = 101 jmp exit endif if wparam = 102 invoke ShowWindow hwnd 1 jmp exit endif if wparam = 104 invoke SendMessageA hwnd {WM_DESTROY} 0 0 jmp exit endif if wparam = 202 jmp exit endif if wparam = 203 jmp exit endif if wparam = 204 endif if wparam = 806 endif jmp default endif #end of menu items jmp exit wmsize: eax=wparam if eax={SIZE_MINIMIZED} invoke ShowWindow hwnd 0 #make window invisible on minimize so that it doesn't appear on the task bar endif jmp exit wmiconnotify: #events have happened to our tray icon eax=lparam if eax={WM_LBUTTONDBLCLK} invoke ShowWindow hwnd 1 #make window visible jmp exit endif if eax={WM_RBUTTONUP} #right mouse --show menu invoke GetCursorPos pt eax=pt ebx=[eax] x=ebx add eax 4 ebx=[eax] y=ebx invoke TrackPopupMenu hfilemenu 0 x y 0 hwnd 0 jmp exit endif default: invoke DefWindowProcA hwnd msg wparam lparam leave ret 16 exit: xor eax eax leave ret 16 endp #------------------------------------------------------------------------ proc WinMain dword hinst dword previnst dword comline dword cmdshow sequence wc 48 dword ordata 0 dword len 0 dword topmenu 0 dword dropmenu 0 dword sepmenu 0 push ebp mov ebp esp add esp -80 call CreateMenu hfilemenu=eax call CreateMenu heditmenu=eax call CreateMenu hviewmenu=eax call CreateMenu hhelpmenu=eax call CreateMenu hmenu=eax mov eax {MF_STRING} or eax {MF_POPUP} or eax {MF_BYPOSITION} or eax {MF_ENABLED} topmenu=eax mov eax {MF_STRING} or eax {MF_ENABLED} dropmenu=eax mov eax {MF_STRING} or eax {MF_SEPARATOR} sepmenu=eax invoke AppendMenuA hmenu topmenu hfilemenu fmenu #invoke AppendMenuA hfilemenu dropmenu 100 new #invoke AppendMenuA hfilemenu dropmenu 101 open invoke AppendMenuA hfilemenu dropmenu 102 show invoke AppendMenuA hfilemenu sepmenu 103 hyphen invoke AppendMenuA hfilemenu dropmenu 104 exit invoke AppendMenuA hmenu topmenu heditmenu emenu invoke AppendMenuA heditmenu dropmenu 200 undo invoke AppendMenuA heditmenu sepmenu 201 hyphen invoke AppendMenuA heditmenu dropmenu 202 cut invoke AppendMenuA heditmenu dropmenu 203 copy invoke AppendMenuA heditmenu dropmenu 204 paste invoke AppendMenuA heditmenu sepmenu 205 hyphen invoke AppendMenuA heditmenu dropmenu 206 find invoke AppendMenuA heditmenu dropmenu 207 replace invoke AppendMenuA heditmenu sepmenu 208 hyphen invoke AppendMenuA heditmenu dropmenu 209 selall invoke AppendMenuA hmenu topmenu hhelpmenu pmenu invoke AppendMenuA hhelpmenu dropmenu 806 about mov eax fon2 mov [eax] 16 add eax 16 mov [eax] 400 add eax 12 invoke strcpy eax font2 hfont2 = CreateFontIndirectA fon2 mov eax fon1 mov [eax] 10 add eax 16 mov [eax] 400 add eax 12 invoke strcpy eax font1 hfont1 = CreateFontIndirectA fon1 [wc]= 48 [wc+4] =3 [wc+8]= WndProc [wc+12]= 0 [wc+16] =0 eax =hInstance [wc+20]= eax push 1 push hInstance call LoadIconA hicon=eax [wc+24]= eax [wc+44]= eax push {IDC_CROSS} push 0 call LoadCursorA [wc+28] =eax [wc+32]= 16 [wc+36]= 0 [wc+40]= ClassName lea eax wc invoke RegisterClassExA eax eax=0 or eax {WS_SYSMENU} or eax {WS_MINIMIZEBOX} or eax {WS_MAXIMIZEBOX} or eax {WS_SIZEBOX} ordata=eax invoke CreateWindowExA {WS_CLIENTEDGE} ClassName WindowCaption ordata 50 50 550 400 0 hmenu hInstance 0 hwndMain= eax #this is old style before the advent of invoke push 0 #window starts invisible push hwndMain call ShowWindow push hwndMain call UpdateWindow MsgLoop: invoke GetMessageA msg 0 0 0 cmp eax 0 je exitprocess invoke TranslateMessage msg invoke DispatchMessageA msg jmp MsgLoop exitprocess: mov eax [msg+8] leave ret 16 endp #------------------------------------------------------------------------ EntryPoint #this is old style before the advent of invoke push 0 call GetModuleHandleA mov hInstance eax call GetCommandLineA mov CommandLine eax invoke WinMain hInstance 0 CommandLine 10 push eax call ExitProcess end code #-----------------Resources--------------------------------------- begin resources icon win32app.ico end resources