Console Program to Tray??????



  • That function is to add icon to tray... it isn't to minimalise console program to Tray...:(



  • Hide the window with ShowWindow.



  • Maybe you should use my code 😉 You can start the application not in tray, when you start the app with start parameters... but when you start without startparams, the App will stay in tray until the sleep function has finished!

    I haven't add the message procedure because i thought that there is something which you can do on your own! That is not so hard...



  • I know... but i ve got 2 paramtrs..

    -b - work inbackground....
    -c - work in console....

    That is strange when i run -b - it is gong like -c...that is my problem...



  • [offtopic]Can you not speak german?[/offtopic]

    Hmm please show us your pieve of code...



  • static int usage (char* basename)
    {
    	printf(" \n     Server v0.2  \n \npress argument to:\n \n");
        printf("Work in console:      %s -c\n", basename);
    	printf("Work in background:   %s -b\n", basename);
    
    	return -1;
    }
    
    bool AddTrayIcon(UINT, ::HICON, LPTSTR); 
    int main(int argc, _TCHAR* argv[])
    {
    
        if(argc<2)
    	{
    		printf(" \n   \n \n");
    
    		return usage(argv[0]);
    
    	}
    
    	else if(lstrcmp(argv[1], _T("-b"))) 
    	{  
    	if (!AddTrayIcon(11, ::LoadIcon(0, MAKEINTRESOURCE(IDI_ICON1)), _T("TwisterServer 0.1")))
               return -1; 
    	::ShowWindow(::GetConsoleWindow(), SW_HIDE);
    
    	printf(" \n Start Working in background   \n \n");
    
    	start_read();
    	return 0;
    	}
    
    	else if(!strcmp(argv[1], "-c"))
    	{ 
    	printf(" \n Start Working in console   \n \n");
    	start_read();
    	return 0;
    	}
    }
    
    bool AddTrayIcon(UINT nID, ::HICON hIcon, LPTSTR lpszTip)
    {
        if (!hIcon || !lpszTip)
            return false;
    
        ::NOTIFYICONDATA tnid;
        tnid.cbSize    = sizeof(NOTIFYICONDATA);
        tnid.hWnd    = ::GetConsoleWindow();
        tnid.uID    = nID;
        tnid.uFlags    = NIF_MESSAGE | NIF_ICON | NIF_TIP;
        tnid.uCallbackMessage    = 0;
        tnid.hIcon    = hIcon;
        lstrcpy(tnid.szTip, lpszTip);
    
        return (::Shell_NotifyIcon(NIM_ADD, &tnid) ? true : false);
    }
    

    i ve got 2 parametrs... -c - console open, work in console

    -b -background - icon tray - window minimalised into tray.

    Danke!! 😃



  • I'm not sure whether a console application can have a tray icon. Because a console application has no windows message loop, because it has no window. And you need a message loop for the tray icon to send it's messages to.



  • Luckie schrieb:

    I'm not sure whether a console application can have a tray icon. Because a console application has no windows message loop, because it has no window. And you need a message loop for the tray icon to send it's messages to.

    But nobody stops you to create a window in a console application.



  • I HATE THIS FORUM-SOFTWARE! Always there are bugs -.-

    back to topic:
    There is a bug in your code.... you check only whether there exist a difference between the start params and -b ... but you do not reagist upon it..

    I have tested the whole code:

    #include <iostream>
    #include <tchar.h>
    #define _WIN32_WINNT 0x0501 
    #include <Windows.h>
    #include <shellapi.h>
    #include <conio.h>
    #include "resource.h"
    
    void PrintUsage(LPCTSTR);
    bool AddTrayIcon(UINT, ::HICON, LPCTSTR);
    bool DeleteTrayIcon(UINT);
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	//PlayGame();
    	if (argc < 2) 
        { 
    		PrintUsage(argv[0]); 
    		getch();
    
            return 0; 
        } 
        else if (lstrcmp(argv[1], _T("-b")) == 0) 
        {   
    		if (!AddTrayIcon(11, ::LoadIcon(0, MAKEINTRESOURCE(IDI_ICON1)), _T("TwisterServer 0.1"))) 
    		{
    			std::cout << "Das Programm konnte nicht im Hintergrund gestartet werden!" << std::endl;
    			getch();
    
    			return 0; 
    		}
    
    		::ShowWindow(::GetConsoleWindow(), SW_HIDE); 
    
    		std::cout << "Start working in background" << std::endl; 
    
    		::Sleep(10000); 
    
    		::MessageBox(GetConsoleWindow(), _T("Test"), _T("Caption"), MB_OK | MB_ICONINFORMATION);
    
    		if (!DeleteTrayIcon(11))
    			return -1;
        } 
        else if (lstrcmp(argv[1], _T("-c")) == 0) 
        { 
    		std::cout << "Start working in console" << std::endl; 
    
    		// PlayGame(); 
        } 
    
    	return 0;
    }
    
    void PrintUsage(LPCTSTR lpszBasename) 
    { 
    	std::cout << "Server v0.2\n\nUse one of this parameters:" << std::endl; 
    	std::cout << "test.exe [-c] [-b]" << std::endl;
    	std::cout << "-c\t: Work in console" << std::endl;
    	std::cout << "-b\t: Work in tray" << std::endl;
    } 
    
    bool AddTrayIcon(UINT nID, ::HICON hIcon, LPCTSTR lpszTip) 
    { 
        if (!hIcon || !lpszTip) 
            return false; 
    
        ::NOTIFYICONDATA tnid; 
        tnid.cbSize				= sizeof(NOTIFYICONDATA); 
        tnid.hWnd				= ::GetConsoleWindow(); 
        tnid.uID				= nID; 
        tnid.uFlags				= NIF_MESSAGE | NIF_ICON | NIF_TIP; 
        tnid.uCallbackMessage	= 0; 
        tnid.hIcon				= hIcon; 
        lstrcpy(tnid.szTip, lpszTip); 
    
        return (::Shell_NotifyIcon(NIM_ADD, &tnid) ? true : false); 
    } 
    
    bool DeleteTrayIcon(UINT nID)
    {
    	::NOTIFYICONDATA tnid; 
        tnid.cbSize				= sizeof(NOTIFYICONDATA); 
        tnid.hWnd				= ::GetConsoleWindow(); 
        tnid.uID				= nID; 
    
    	return (::Shell_NotifyIcon(NIM_DELETE, &tnid) ? true : false); 
    }
    

    But maybe nexttime you can use your own brain?



  • yes... i am using it all the time 😃

    but look in your code... when you push Tray Icon (wich it is create when you start with -b) it is no maximalize.... you must turn it off via ctrl+alt+del



  • Sry... i can not understand you...

    Maybe you can explain your problem otherwise ... (When you thing that the Trayicon still exist, even when the app is closed... then you are wrong... my DeleteTrayIcon function clean everything up 😉



  • I commpile your src now, so

    when do you run it with -b parametr (tray)

    and want to stop - it is working all the time, how do you stop it ?? Manually? 😉



  • You are not able to stop it... only if you show the console again or you implement a tray icon context menu...



  • Yes You have right, so can u gave me some tips? What do looking for??



  • look at the msdn 😉
    CreateMenu(...) and so.
    MfG schirrmie



  • I guess that Darez will have big problems with the MessageProc... but this time he should use his brain und the MSDN and than he will find the solution 😉



  • Ohh shti 😃 VC++ is so hard...

    Linux, GCC, PowerPC and enigma or VDR is the best 😃 I think, i never do that, thanks for tips, help for all. I need to learn much or some tips need...



  • You need to learn WinApi. why do you use console? All you can use in Console you can use in WinApi and much more!

    MfG schirrmie



  • anyway i see that is another language vc++ then c++

    for example...

    unsigned int username, 
       password, 
       port, 
       configKey;
    
    void read_cfg()
    {
    FILE *fn = fopen("twister.cfg", "r");
     if(fn)
     {
      char buffer[100];
    
      while( fgets(buffer, 100, fn) != NULL )
      {
       sscanf(buffer, "USER= %2d ", &username);
       sscanf(buffer, "PASSWORD= %2d ", &password);
       sscanf(buffer, "PORT= %2d ", &port);
       sscanf(buffer, "CKEY= %2d ", &configKey);
    
      }
      fclose(fn);
     else
         {
      user=0;
      password=0;
      port =0;
      ckey= 0;
         }
    
    }
    int _tmain(int argc, _TCHAR* argv[])
    {
     read_cfg()
     printf(username);
     return 0;
    }
    

    This is working perfectly on linux... here on vc++ it is not commpile....



  • @(D)Evil:
    Sorry, dieser Threat war ne Weile in C++ drinnen, und dort gibt's die Möglichkeit, einen minimierenden Effekt über die Simulation von betriebssystem-spezifischen Tastaturkürzeln mit ISO-C++ zu erzeugen. Da es sich bei der Frage aber um WinAPI handelt, ist das natürlich Schwachsinn. Mir auch klar, dass das natürlich nicht die beste Lösung gewesen wäre, quick and dirty, aber funktionierend. Mein Fehler...

    @Darez:
    Sorry, this threat was located at our C++ category. Under C++ it's possible to minimize the console window with OS-depending shortcuts (application simulates keystrokes). This trick is just a kind of stupid while using WinAPI. I didn't know that you want a WinAPI-solution as I did my post, so excuse me.


Anmelden zum Antworten