Console Program to Tray??????



  • Import library shell32.lib



  • Richtig.... was meinst de was in dem vom mir angehengten Link steht 😃



  • Great, Is it possibale that You can upload somewhere You sln?? - poject form vc++??

    I can check what is wrong...

    darezik@gmail.com - e mail

    Thasnk mr Devil for all really thanks



  • Yep it is works 😃

    but, when i close CMD window, TEST progra is shoutdown...-no minimaising to tray closing.. when i try to press on icon tray it no maximalize... nth do...



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


Anmelden zum Antworten