开关/外壳内的开关/外壳

时间:2011-09-22 23:35:02

标签: c++ switch-statement

以下内容出现在我的WinProc中:

if(message == WM_CREATE)
{
//Do WM_CREATE stuff
}

else
{
    switch(message)
    {
        case WM_KEYDOWN:
        {
            switch(wParam)
            {
                case VK_LEFT:
                {
                //declare new variable here
                D2D1_RECT_F bounds;
                HRESULT hr = pDemoApp->mpGeometryGroup->GetBounds(pDemoApp->mTransform, &bounds);
                }
             }
         }
    }
}

以这种方式声明和使用变量有什么问题吗?

我声明并使用了边界(仍在范围内)之后设置了一个断点,但我似乎无法在调试器的“Locals”窗口中找到它。有什么问题?

我不想用一堆不相关的代码垃圾邮件,但这里是完整的WinProc。

LRESULT CALLBACK DemoApp::WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
LRESULT result = 0;

if (message == WM_CREATE)
{
    LPCREATESTRUCT pcs = (LPCREATESTRUCT)lParam;
    DemoApp *pDemoApp = (DemoApp *)pcs->lpCreateParams;

    ::SetWindowLongPtrW(
        hwnd,
        GWLP_USERDATA,
        PtrToUlong(pDemoApp)
        );

    result = 1;
}
else
{
    DemoApp *pDemoApp = reinterpret_cast<DemoApp *>(static_cast<LONG_PTR>(
        ::GetWindowLongPtrW(
        hwnd,
        GWLP_USERDATA
        )));

    bool wasHandled = false;

    if (pDemoApp)
    {
        switch (message)
        {
        case WM_SIZE:
            {
                UINT width = LOWORD(lParam);
                UINT height = HIWORD(lParam);
                pDemoApp->OnResize(width, height);
            }
            result = 0;
            wasHandled = true;
            break;

        case WM_DISPLAYCHANGE:
            {
                InvalidateRect(hwnd, NULL, FALSE);
            }
            result = 0;
            wasHandled = true;
            break;

        case WM_PAINT:
            {
                pDemoApp->OnRender();
                ValidateRect(hwnd, NULL);
            }
            result = 0;
            wasHandled = true;
            break;

        case WM_KEYDOWN:
            {
                D2D1_SIZE_F rtSize = pDemoApp->mpRenderTarget->GetSize();
                static float angle = 0.0f;

                switch(wParam)
                {
                case VK_LEFT:
                    {

                        angle -= 90;

                        if(angle < -360)
                            angle = 0;

                        D2D1_RECT_F bounds;
                        HRESULT hr = pDemoApp->mpGeometryGroup->GetBounds(pDemoApp->mTransform, &bounds);

                            pDemoApp->mTransform = D2D1::Matrix3x2F::Rotation(
                            angle,
                            D2D1::Point2F((bounds.right + bounds.left)/2, (bounds.bottom + bounds.top)/2)
                            );

                            hr = hr;


                        InvalidateRect(hwnd, NULL, FALSE);
                        break;
                        }

                case VK_RIGHT:
                    {
                        angle += 90;

                        if(angle > 360)
                            angle = 0;

                        D2D1_RECT_F bounds;
                        pDemoApp->mpGeometryGroup->GetBounds(pDemoApp->mTransform, &bounds);

                            pDemoApp->mTransform = D2D1::Matrix3x2F::Rotation(
                            angle,
                            D2D1::Point2F((bounds.right + bounds.left)/2, (bounds.bottom + bounds.top)/2)
                            );


                        InvalidateRect(hwnd, NULL, FALSE);
                        break;
                    }

                case VK_DOWN:
                    {
                        pDemoApp->mTransform = pDemoApp->mTransform * D2D1::Matrix3x2F::Translation(
                            0.0f,
                            5.0f);

                        InvalidateRect(hwnd, NULL, FALSE);
                        break;
                    }
                }
            }
            result = 0;
            wasHandled = true;
            break;

        case WM_LBUTTONDOWN:
            {
                FLOAT xPos, yPos;

                xPos = LOWORD(lParam);
                yPos = HIWORD(lParam);

                BOOL contains = false;

                pDemoApp->mpGeometryGroup->FillContainsPoint(
                    D2D1::Point2F(xPos, yPos),
                    pDemoApp->mTransform,
                    &contains);

                if(contains)
                    MessageBoxA(hwnd, "Hooray!", NULL, NULL);

                D2D1_GEOMETRY_RELATION relation;

                pDemoApp->mpGeometryGroup->CompareWithGeometry(
                    pDemoApp->mpSecondGeometryGroup,
                    pDemoApp->mTransform,
                    0.001f,
                    &relation);

                if(relation == D2D1_GEOMETRY_RELATION_CONTAINS ||
                    relation == D2D1_GEOMETRY_RELATION_IS_CONTAINED ||
                    relation == D2D1_GEOMETRY_RELATION_OVERLAP)
                {
                    MessageBoxA(hwnd, "overlap or contains.", 0, 0);
                }


            }
            result = 0;
            wasHandled = true;
            break;

        case WM_DESTROY:
            {
                PostQuitMessage(0);
            }
            result = 1;
            wasHandled = true;
            break;
        }
    }

    if (!wasHandled)
    {
        result = DefWindowProc(hwnd, message, wParam, lParam);
    }
}

return result;

}

1 个答案:

答案 0 :(得分:1)

以这种方式声明变量没有问题,因为您已为VK_LEFT情况指定了新范围。如果您没有声明一个单独的范围,那么变量将是可见的,但可能是未初始化的,这将是一个问题。请注意,顺便说一下,你错过了一些breaks