Console Program to Tray??????



  • 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.



  • Darez
    No... VC++ is a standard conform C++ Compiler! One of the best...
    I do not know why you use a integer whether you want to read out a char... i guess that is the Problem... and your style isn't very well:

    #include <iostream>
    #include <conio.h>
    
    typedef struct Config__
    {
    	unsigned int nUsername;
    	unsigned int nPassword;
    	unsigned int nPort;
    	unsigned int nConfigKey;
    }CONFIG, *LPCONFIG;
    
    void ReadConfig(LPCONFIG /*lpConfig*/);
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	CONFIG cfg;
    	ReadConfig(&cfg);
    
    	std::cout << cfg.nUsername << std::endl;
    
    	getch();
    
    	return 0;
    }
    
    void ReadConfig(LPCONFIG lpConfig)
    { 
    	FILE* pFile = fopen("twister.cfg", "r"); 
    
    	if (!pFile) 
    	{
    		(*lpConfig).nConfigKey	= 0;
    		(*lpConfig).nPassword	= 0;
    		(*lpConfig).nUsername	= 0;
    		(*lpConfig).nPort		= 0;
    
    		return;
    	}
    
    	char cBuffer[100]; 
    
    	while (fgets(cBuffer, 100, pFile)) 
    	{ 
    		sscanf(cBuffer, "USER= %2d ", &(*lpConfig).nUsername); 
    		sscanf(cBuffer, "PASSWORD= %2d ", &(*lpConfig).nPassword); 
    		sscanf(cBuffer, "PORT= %2d ", &(*lpConfig).nPort); 
    		sscanf(cBuffer, "CKEY= %2d ", &(*lpConfig).nConfigKey); 
    	} 
    
    	fclose(pFile); 
    }
    

    But why you do not simply find the solution of your primary project before you do something else?


Anmelden zum Antworten