Win32 DrawText颜色和显示

Win32 DrawText Colour and Display

本文关键字:显示 颜色 DrawText Win32      更新时间:2023-10-16

我想在我的窗口上显示一些文本。我正在使用Win32/OpenGL与c++。

我发现了这个问题,这是我试图实现的方法,不幸的是,我做错了,因为它不工作。

这是我的CALLBACK函数:
LRESULT CALLBACK WinProc(HWND hWnd,UINT uMsg, WPARAM wParam, LPARAM lParam){
    LONG    lRet = 0; 
    PAINTSTRUCT    ps;
    switch (uMsg)
    { 
    case WM_SIZE:
        if(!g_bFullScreen)
        {
            SizeOpenGLScreen(LOWORD(lParam),HIWORD(lParam));
            GetClientRect(hWnd, &g_rRect);
        }
        break; 
    case WM_PAINT:
        //BeginPaint(hWnd, &ps);
        //adding code from SO question here
         HDC hdc = BeginPaint(hWnd, &ps);  //line 403
            RECT rec;
            //       SetRect(rect, x ,y ,width, height)
            SetTextColor(hdc, RGB(255,255,255))
            SetRect(&rec,10,10,100,100);
            //       DrawText(HDC, text, text length, drawing area, parameters "DT_XXX")
            DrawText(hdc, TEXT("Text Out String"),strlen("Text Out String"), &rec, DT_TOP|DT_LEFT);
            EndPaint(hWnd, &ps);
            ReleaseDC(hWnd, hdc);
        //EndPaint(hWnd, &ps);
        break;
       case WM_KEYDOWN: //line 418
                    //some key presses
   case WM_CLOSE:
        PostQuitMessage(0);
        break; 
    default://line 510
        lRet = DefWindowProc (hWnd, uMsg, wParam, lParam); 
        break; 
    } 
    return lRet;
}

我似乎在执行一些错误的东西或忽视一些东西,因为我只是看不到它。

错误如下:main.cpp(403) : see declaration of 'hdc'

如果有人可以建议编辑或帮助我在哪里我错了,那将是伟大的。提前谢谢。

有一些错误(在上面的代码中添加了几行):

main.cpp(418): error C2360: initialization of 'hdc' is skipped by 'case' label
main.cpp(506): error C2360: initialization of 'hdc' is skipped by 'case' label
main.cpp(510): error C2361: initialization of 'hdc' is skipped by 'default' label

不能在switch语句中间声明变量。它必须在块中,或者在switch开始之前声明。

只需将代码放入括号{}中的case中,错误就会消失。