c++ - C++ 中的开关只能有一个默认值吗?

标签 c++ winapi switch-statement

<分区>

我正在从“编程 2D 游戏”学习用 C++ 编写游戏代码,我有一个基本的 WinMain 程序,如下所示,但是书中说要使用两个默认标签。但是我的 IDE (VS2013) 告诉我默认标签已经出现在开关中。

是否有解决此问题的方法,还是我在转换中犯了错误?我已经与这本书进行了交叉引用,但是我找不到这方面的任何信息。

#define WIN32_LEAN_AND_MEAN
#include <windows.h> 
// Function prototypes
int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int);
bool CreateMainWindow(HINSTANCE, int);
LRESULT WINAPI WinProc(HWND, UINT, WPARAM, LPARAM);
// global variable
HINSTANCE hinst;
HDC hdc;                // Handle to device context
TCHAR ch = ' ';         // Character entered
RECT rect;              // Rectangle structure
PAINTSTRUCT ps;         // Used in WM_PAINT
// Constants 
const char CLASS_NAME[] = "Keyboard";
const char APP_TITLE[] = "Character Input";
const int WINDOW_WIDTH = 400;  
const int WINDOW_HEIGHT = 400;
//==================================
// Starting point for the windows application
// Parameters are
// hInstance. Handle to the current instance of the application 
// hPrevInstance. Always NULL, obsolete parameter
// lpCmdLine. Pointer to null-terminated string of command arguements 
// nCmdShow. Specifies how the window is to be shown 
//=================================

int WINAPI WinMain(HINSTANCE    hInstance,
    HINSTANCE   hPrevInstance,
    LPSTR       lpCmdLine,
    int         nCmdShow )

{
    MSG msg;
    // Create thw window 
    if (!CreateMainWindow(hInstance, nCmdShow))
        return false;
    // Main message loop 
    int done = 0;
    while (!done)
    {
        // PeekMessage is a non blocking message for Windows messages 
        if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
        {
            // Look for quit message
            if (msg.message == WM_QUIT)
                done = 1;
            // Decode and pass messages on to WinProc
            TranslateMessage(&msg);
            DispatchMessage(&msg);    

        }

    }
    return msg.wParam;

}

//==============
// Window event callback function 
// ===============================
LRESULT WINAPI WinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
        {
    switch (msg)
    {
    case WM_DESTROY:
            //Tell windows to kill this program 
            PostQuitMessage(0);
            return 0;
    case WM_CHAR:       // A character was entered by the keyboard
        switch (wParam);    // The character is in wParam
        {
        case 0x08:  //Backspace
        case 0x09:  // Tab
        case 0x0A:  // Linefeed
        case 0x0D:  // Carriage return
        case 0x1B:  // Escape
            MessageBeep((UINT) -1);         // Beep but do not display 
            return 0;
        default:            // Displayable character
            ch = (TCHAR) wParam;        // Get the character 
            InvalidateRect(hWnd, NULL, TRUE);   // Force WM_PAINT 
            return 0;
        }
        case WM_PAINT:                  // The window needs to be redrawn 
            hdc = BeginPaint(hWnd, &ps); // Get handle to device context 
            GetClientRect(hWnd, &rect);     // Get the window rectangle
            // Display the character 
            TextOut(hdc, rect.right / 2, rect.bottom / 2, &ch, 1);
            EndPaint(hWnd, &ps);
            return 0;
        default:
            return DefWindowProc( hWnd, msg, wParam, lParam );
        }


}
//===========================================
// Create the window
// Returns: False on error
//===========================================
bool CreateMainWindow(HINSTANCE hInstance, int nCmdShow)
{
    WNDCLASSEX wcx;
    HWND hwnd;

    // Fill in the window class structure with parameters 
    // That describe the main window 
    wcx.cbSize = sizeof(wcx);               // Size of the structure 
    wcx.style = CS_HREDRAW | CS_VREDRAW;    // Redraw if the size changes 
    wcx.lpfnWndProc = WinProc;              // Points to windows procedure 
    wcx.cbClsExtra = 0;                     // No extra class memory 
    wcx.cbWndExtra = 0;                     // No extra window memory
    wcx.hInstance = hInstance;
    wcx.hIcon = NULL;
    wcx.hCursor = LoadCursor(NULL, IDC_ARROW);      // Predifined arrow
    // Background brush
    wcx.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
    wcx.lpszMenuName = NULL;    // Name of menu resource 
    wcx.lpszClassName = CLASS_NAME; // Name of window class
    wcx.hIconSm = NULL;

    // Register the window class
    // RegisterClassEx return 0 on error
    if (RegisterClassEx(&wcx) == 0) // if error 
        return false;
    // Create Window
    hwnd = CreateWindow(
        CLASS_NAME,                     // Name of window class
        APP_TITLE,                      // Title bar text
        WS_OVERLAPPEDWINDOW,            // Window style
        CW_USEDEFAULT,                  // Default horizontal postion of window
        CW_USEDEFAULT,                  // Default vertical postion of window
        WINDOW_WIDTH,                   // Width of window 
        WINDOW_HEIGHT,                  // Height of window 
        (HWND) NULL,                    // No parent window 
        (HMENU) NULL,                   // No menu
        hInstance,                      // Handle to application window
        (LPVOID) NULL);                 // No window parameters
    // If there was an error the window
    if (!hwnd)
        return false;
    // Show the window
    ShowWindow(hwnd, nCmdShow);
    // Send a WM_PAINT message to the window procedure 
    UpdateWindow(hwnd);
    return true;
}

最佳答案

每个 switch 语句只能有一个 default:。但是您可以将一个 switch 语句放在另一个 switch 语句中,然后每个语句都可以有一个 default: case。

switch (var1)
{
    case 1:
    break;
    case 2:
    {
        switch (var2)
        {
            case 1:
            break;
            case 3:
            break;
            default: // var1 is 2, and var2 is not 1 or 3
            break;
        }
    }
    break;
    default: // var1 is not 1 or 2
    break;
}

你的代码几乎是嵌套的 switch 语句那样,但是你犯了一个错误:

switch (wParam);    // The character is in wParam

不应有分号 ;switch 应用于紧跟在 ) 字符之后的一个语句,您几乎总是希望该语句是一个“复合语句”,即被 {} 包围的 block 。相反,此 switch 适用于无操作语句 ;

关于c++ - C++ 中的开关只能有一个默认值吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25572142/

相关文章:

java - 使用 CORBA 进行 Java/C++ 互操作的最佳 ORB 是什么?

c++ - 用于将 token 的整数映射到字符串的宏

c++ - 引用 GetCapture() 函数捕获鼠标意味着什么?

c++ - 如何在选项卡控件页面内显示对话框?

c# - 在 C# 8 中 System.Type 上的开关表达式

c - 修复 C switch 语句函数溢出?

PHP:带有字符串的 switch-case 语句效率低吗?

c++ - 在异步调用中将 shared_ptr 作为参数传递

c++ - 如何在 Qt 5 中将 New-Signal-Slot 语法声明为函数的参数

c# - 欺骗应用程序关于鼠标位置的信息