开关/外壳内的开关/外壳
我的 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);
}
}
}
}
}
以这种方式声明和使用变量是否存在任何问题?
我在声明和使用边界后设置了一个断点(仍在范围内),但我似乎无法在调试器的“本地”窗口中找到它。怎么了?
我不想在帖子中添加一堆不相关的代码,但这里是完整的 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;
}
The following appears in my 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);
}
}
}
}
}
Is there any problem with declaring and using variables this way?
I set up a breakpoint after I declare and use bounds (still within the scope) but I can't seem to find it in the 'Locals' window in the debugger. What is wrong?
I didn't want to spam the post with a bunch of unrelated code, but here is the full 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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
以这种方式声明变量没有问题,因为您已经为
VK_LEFT
情况指定了新范围。如果您没有声明单独的作用域,那么变量将是可见的,但可能未初始化,这将是一个问题。请注意,顺便说一句,您错过了一些中断
。There is no problem in declaring variables that way, since you have specified a new scope for the
VK_LEFT
case. If you weren't declaring a separated scope, then variables would be visible yet possibly non-initialized which would be a problem. Note that you missed a fewbreaks
by the way.