【问题标题】:Win32 DrawText Colour and DisplayWin32 DrawText 颜色和显示
【发布时间】:2014-03-20 03:07:45
【问题描述】:

我正在尝试在我的窗口上显示一些文本。我正在使用带有 c++ 的 Win32/OpenGL。

我找到了this question,这是我正在尝试实施的方法,不幸的是,我做错了,因为它不起作用。

这是我的 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

【问题讨论】:

    标签: c++ winapi drawtext


    【解决方案1】:

    您不能在switch 语句的中间声明变量。它必须要么在块内,要么在switch 开头之前声明。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-26
      • 2010-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-14
      • 2013-06-12
      • 1970-01-01
      相关资源
      最近更新 更多