Mehrfaches starten verhindern -> TrayIcon1->Restore()



  • Hi,

    ich habe im projekt quellcodes folgendes stehen:

    WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
    {
            Application->Title = "";
            HWND hPrevApp = ::FindWindow(NULL, "Mein Title");
    
            if(hPrevApp)
            {
               PostMessage(hPrevApp, WM_SYSCOMMAND, SC_RESTORE, 0);
               return 0;
            }
            // else: other instance not found, restore the title and continue
            else
               Application->Title = "Mein Title";
    
            try
            {
                     Application->Initialize();
                     Application->Title = "Mein Title";
                     Application->CreateForm(__classid(TForm1), &Form1);
                     Application->Run();
            }
            catch (Exception &exception)
            {
                     Application->ShowException(&exception);
            }
            return 0;
    }
    

    Meine Anwendung nutzt das Tray Icon und nun möchte gerne, sofern die Anwendung schon geöffnet ist, einfach TrayIcon1->Restore() aufrufen, da mein jetziger code dies nicht macht.

    Oder hat jemand ne andere Idee?

    Kevin



  • Hi,
    möglich wäre das z.B. in dem du eine benutzerdefinierte WindowsMessage sendest und abfängst. Wenn die Message ankommt dann einfach TrayIcon1->Restore(); aufrufen....

    MfG

    Alexander Sulfrian

    PS: Zu Messages findet sich auch in den FAQ was...



  • Hm, also bei mir funktioniert das, unter Verwendung von TApplicationEvents, auch ohne benutzerdefinierte Nachricht...

    //aus der Projekt.cpp
    WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
    {
    	HANDLE hInstanceMutex = ::CreateMutex(NULL, TRUE, "TESTTI.MUTEX");
    	if (GetLastError() == ERROR_ALREADY_EXISTS)
    	{
    		if (hInstanceMutex)
    			CloseHandle(hInstanceMutex);
    		Application->Title = "";
    		HWND hPrevApp = ::FindWindow(NULL, "TESTTI");
    		if (hPrevApp)
    		{
    			PostMessage(hPrevApp, WM_SYSCOMMAND, SC_RESTORE, 0); // Falls minimiert
    			SetForegroundWindow(hPrevApp); // kein schöner Stil, aber bringt das Fenster in den Vordergrund...
    		}
    		return 0;
    	}
    	try
    	{
    		Application->Initialize();
    		Application->Title = "TESTTI";
    		Application->CreateForm(__classid(TForm1), &Form1);
    		Application->Run();
    	}
    	catch (Exception &exception)
    	{
    		Application->ShowException(&exception);
    	}
    	catch (...)
    	{
    		try
    		{
    			throw Exception("");
    		}
    		catch (Exception &exception)
    		{
    			Application->ShowException(&exception);
    		}
    	}
    	ReleaseMutex(hInstanceMutex);
    	CloseHandle(hInstanceMutex);
    	return 0;
    }
    //---------------------------------------------------------------------------
    
    // die beiden ApplicationEvents:
    void __fastcall TForm1::ApplicationEvents1Minimize(TObject *Sender)
    {
    	ShowWindow(Application->Handle, SW_HIDE);
    	TrayIcon1->Visible = true;
    }
    //---------------------------------------------------------------------------
    
    void __fastcall TForm1::ApplicationEvents1Restore(TObject *Sender)
    {
    	TrayIcon1->Visible = false;
    	ShowWindow(Application->Handle, SW_SHOW);
    }  
    //---------------------------------------------------------------------------
    

Anmelden zum Antworten