• Welcome to Valhalla Legends Archive.
 

Console Application -> Minimize to System Tray?

Started by Sargera, July 21, 2004, 02:49 PM

Previous topic - Next topic

Spht

Quote from: Sargera on July 21, 2004, 02:49 PM
Is this possible?

Yes.  Very similar to how you'd do it for a non-console window.



LordVader

#4
Quote from: Sargera on July 21, 2004, 04:59 PM
Do you have a link?

I use this class for systray stuff:
http://www.codeproject.com/shell/ss_trayicon.asp

To use it in a non MFC project do:

//This is a global Variable:
SS_TrayIcon* m_pTrayIcon;
LRESULT CALLBACK OnMouseRightClickTI(WPARAM wParam, LPARAM lParam)
{
   m_pTrayIcon->ShowMenuAtMouse(IDR_TRAYMENU, hMainWnd);// --- hMainWnd is the dialog/window pointer, and IDR_TRAYMENU is a menu resource to show.
   return 0;
}

void AddTaskbarIcons()
{
   m_pTrayIcon = new SS_TrayIcon( hWndInst, 1, 1 );
   m_pTrayIcon->LoadIcon( 0, 0, IDI_MAIN_ICON ); // ---- point to a icon resource
   m_pTrayIcon->ShowIcon( 0 );  
   m_pTrayIcon->ToolTip( 0, _T("Some Text") );   
   m_pTrayIcon->MapMessageToFunction(WM_RBUTTONDOWN, ::OnMouseRightClickTI); // ---- Display a menu on right click jumps to the functions above.
}


Then in your callback for the main window or dialog being created it needs to be fired up:

LRESULT CALLBACK WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{

   static UINT s_uTaskbarRestart; // ----  declare a new msg see below
   switch(uMsg)
   {
      case WM_CREATE:
         hMainWnd = hwnd;
         // ---- Init msg to windows so we can replace systray icon if explorer crashes
         s_uTaskbarRestart = RegisterWindowMessage(TEXT("TaskbarCreated"));

         // ---- Add the systray icons using a offloaded function
         AddTaskbarIcons();

         // ---- Add a Status area to the main window
         //hStatusWnd = CreateWindowEx(0, STATUSCLASSNAME, NULL, WS_CHILD | WS_VISIBLE | SBARS_SIZEGRIP, 0, 0, 0, 0,hwnd, (HMENU)IDC_MAIN_STATUS, GetModuleHandle(NULL), NULL);
         //SendMessage(hStatusWnd, SB_SETTEXT, 0, (LPARAM)" Status - Disconnected");
         break;
                          /**rest of your cases, then give a default**/
       default:
         if(uMsg == s_uTaskbarRestart){
            // ---- This will fire "IF" explorer crashes, as noted from Skywings reference in the documents.
            AddTaskbarIcons();
         }
   }
   return DefWindowProc(hwnd, uMsg, wParam, lParam);
}


*Note this example is a window NOT a dialog or console slight adjustments needed dialog doesn't "Create" it uses WM_INIT etc. figure it out for a console should be easy  ;D.

Edit: Also need to add the SS_Trayicon.cpp/.h and SS_Wnd.cpp/h files to your project workspace.

Alot of ppl need to read that or figure it out from Skywings reference on how to replace icons when explorer crashes *Pokes DM and half the other bot developer's out there* who's icons vanish if explorer goes poof.
I'm just picky tho heh.

Come to think of it Stealthbot is the only Bot I've used that wont loose it's icon in systray (if present) so hey..