DiscordMessenger / dm

Discord Messenger is a free Discord-compatible messaging client targeting both new and old Windows.
MIT License
862 stars 16 forks source link

Add minimize to tray support. #113

Closed KnockKnockP closed 2 months ago

iProgramMC commented 3 months ago

Thanks! I'll take a look.

On Wed, Jun 26, 2024, 22:26 KnockKnockP @.***> wrote:


You can view, comment on, or merge this pull request online at:

https://github.com/DiscordMessenger/dm/pull/113 Commit Summary

File Changes

(10 files https://github.com/DiscordMessenger/dm/pull/113/files)

Patch Links:

— Reply to this email directly, view it on GitHub https://github.com/DiscordMessenger/dm/pull/113, or unsubscribe https://github.com/notifications/unsubscribe-auth/AEHFR5RXBOXX6RLDEHBKB63ZJMIXBAVCNFSM6AAAAABJ6QMC4GVHI2DSMVQWIX3LMV43ASLTON2WKOZSGM3TMMJQHA3TANY . You are receiving this because you are subscribed to this thread.Message ID: @.***>

KnockKnockP commented 3 months ago

Thinking about it, I could probably just use mutex to force single instance. I don't know why I overcomplicated it.

iProgramMC commented 3 months ago

Yeah, you can do that. Here's an example:


// I use a class here to perform cleanup after winmain
class InstanceMutex
{
    HANDLE m_handle = NULL;

public:
    HRESULT Init()
    {
#ifdef _DEBUG
        return 0;
#else
        SetLastError(NO_ERROR);

        m_handle = CreateMutex(NULL, TRUE, WINDOW_CLASS);

        if (GetLastError() == ERROR_ALREADY_EXISTS)
        {
            if (m_handle)
            {
                CloseHandle(m_handle);
                m_handle = NULL;
            }

            return ERROR_ALREADY_EXISTS;
        }

        return GetLastError();
#endif
    }

    ~InstanceMutex()
    {
#ifdef _DEBUG
        if (m_handle)
            CloseHandle(m_handle);
#endif
    }
};

InstanceMutex g_instanceMutex;

bool DetectDuplicateInstances()
{
    HRESULT hres = g_instanceMutex.Init();
    if (hres != ERROR_ALREADY_EXISTS) return false;

    HWND hWnd = FindWindow(WINDOW_CLASS_NAME, NULL);
    if (hWnd) {
        SendMessage (hWnd, WM_REQUESTACTIVATE, 0, 0);
        return true;
    }

    return false;
}