Amby131
Amby131

Reputation: 81

Questions regarding GetWindowPlacement return data

I'm a bit unsure of the meaning of some of the return values from a call to the GetWindowPlacement() function, so I'd like your help, please.

I'll be calling this to obtain the normal dimensions of a hidden window.

First, where do the values of the showCmd field come from? In the Microsoft documentation of the return structure (WINDOWPLACEMENT structure, all the descriptions of the possible values use verbs/action words; e.g., "SW_MAXIMIZE: Maximizes the specified window", or "SW_SHOWNOACTIVATE: Displays a window in its most recent size and position."

I want to obtain the dimensions of the hidden window without unhiding/restoring it first, so with the verbs it seems that I would have to call SetWindowPlacement() with showCmd set to SW_SHOWNOACTIVATE before calling GetWindowPlacement. Is that correct?

So do I understand correctly that the primary (and perhaps only) way that field gets its various values is by an explicit call to SetWindowPlacement() somewhere?

My second question relates to the rcNormalPosition return values. Do those data include the window decorations, or are they client values?

Thank you for your time!

Upvotes: 2

Views: 791

Answers (2)

Michael Haephrati
Michael Haephrati

Reputation: 4225

I found these 2 functions to work for me.

void MyDialog::LoadDialogPlacement()
{
    static WINDOWPLACEMENT last_wp = {};
    // Load last stored DB version
    WINDOWPLACEMENT *wp = new WINDOWPLACEMENT;
    GetStoredWindowPlacement(&wp);
    if (memcmp((void *)&last_wp, (const void *)wp, sizeof(WINDOWPLACEMENT)) == 0) return;
    memcpy((void *)&last_wp, (const void *)wp, sizeof(WINDOWPLACEMENT));
    SetWindowPlacement(wp);
    delete[] wp;

}
void MyDialog::SaveDialogPlacement()
{
    static WINDOWPLACEMENT last_wp = {};

    if (IsWindowVisible())
    {
        WINDOWPLACEMENT wp = {};
        wp.length = sizeof(WINDOWPLACEMENT);
        GetWindowPlacement(&wp);
        if (memcmp((void *)&last_wp, (const void *)&wp, wp.length) == 0) return;
        memcpy((void *)&last_wp, (const void *)&wp, wp.length);
        StoreWindowPlacement(&wp);
    }

}

Upvotes: 0

Sean Cline
Sean Cline

Reputation: 7199

The meaning of the showCmd member of the WINDOWPLACEMENT struct is a bit confusing because Win32 is reusing the SW_* commands used by ShowWindow().

Luckily, the meaning is documented on the GetWindowPlacement() function.

If the window identified by the hWnd parameter is maximized, the showCmd member is SW_SHOWMAXIMIZED. If the window is minimized, showCmd is SW_SHOWMINIMIZED. Otherwise, it is SW_SHOWNORMAL.

So, based on which of those 3 values is returned, you can tell whether the window is currently maximized, minimized or, normal (restored). And if you'd like to know what the normal placement is, you can just use the rcNormalPosition member. You do not need to call SetWindowPlacement() at all.

However, heed the warning that GetWindowPlacement() returns workspace coordinates rather than screen coordinates, which differ based on taskbar position and size. This is not a problem if you are only using the coordinates returned by GetWindowPlacement() to call SetWindowPlacement(). Otherwise, you might have to find a way to convert from workspace to screen coordinates.

Upvotes: 5

Related Questions