2
\$\begingroup\$

Please help make this code more cleaner. It's a part of window procedure that notifies my renderer before client area of window resized.

I need to check two combinations of bit blags: some of them must be set, and some must be not. How to do it without bunch of temp variables? (and probably calculate only 2 possible states and only once, not every call)

Thanks!

LRESULT CALLBACK MSWindow::Impl::WndProc( HWND hWnd, uint msg, WPARAM wParam, LPARAM lParam )
{
switch( msg )
{

// ...

// WM_WINDOWPOSCHANGING is sent just about window position, size or order changed.
case WM_WINDOWPOSCHANGING:
    {
        const WINDOWPOS* wp = ((WINDOWPOS*)lParam);
        const auto& flags = wp->flags;

        const bool NOCOPYBITS       = (flags & SWP_NOCOPYBITS)      > 0;
        const bool NOSIZE           = (flags & SWP_NOSIZE)          > 0;
        const bool FRAMECHANGED     = (flags & SWP_FRAMECHANGED)    > 0;
        const bool NOOWNERZORDER    = (flags & SWP_NOOWNERZORDER)   > 0; 
        const bool NOZORDER         = (flags & SWP_NOZORDER)        > 0;
        const bool NOACTIVATE       = (flags & SWP_NOACTIVATE)      > 0;
        const bool SHOWWINDOW       = (flags & SWP_SHOWWINDOW)      > 0;

        const bool IsResizingAboutToEnd  = !(NOSIZE) && NOOWNERZORDER && NOZORDER && (!NOACTIVATE);
        const bool IsGoingToMaximizeOrRestore = FRAMECHANGED && (!NOACTIVATE) && (!NOCOPYBITS) && (!SHOWWINDOW);

        if ( IsGoingToMaximizeOrRestore || IsResizingAboutToEnd )
        {
            Resize(Point(wp->cx, wp->cy));
        }

        break;
    }
// .....
}
\$\endgroup\$
5
\$\begingroup\$

You first need to select the bits in the flag you want to test by ANDing them with a mask containing all the bits.

This will zero all the bits that aren't being tested.

You then just need to check the value of the bits you want so the masks and values are:

const UINT EndMask = NOSIZE | NOOWNERZORDER | NOZORDER | NOACTIVATE;
const UINT MaxRestoreMask = FRAMECHANGED | NOACTIVATE | NOCOPYBITS | SHOWWINDOW;
const UINT EndValue = NOOWNERZORDER | NOZORDER;
const UINT MaxRestoreValue = FRAMECHANGED;

The test then becomes:

if ((flags & EndMask) == EndValue || (flags & MaxRestoreMask) == MaxRestoreValue)
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.