2014-03-19 65 views
1

我試圖在我的窗口上顯示一些文本。我使用的是Win32/OpenGL和C++。Win32 DrawText顏色和顯示

我發現this question這是我試圖實施的方法,不幸的是,我做錯了,因爲它不工作。

這是我的回調函數:

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 
+1

您能否給我們提供完整的錯誤信息(這看起來只有它的一半),並標記哪一行是403? –

+0

我已更新我的問題。感謝發佈。 – Reanimation

+0

@Reanimation把'case WM_PAINT:'之後的所有內容都放到'{}}中,直到'case WM_KEYDOWN:'之前。 – user2802841

回答

4

switch聲明中你不能聲明一個變量。它必須在塊內,或者在switch開始之前聲明。

只要將case中的代碼放在括號內{}中,錯誤就會消失。

+0

哦,你好!多麼愚蠢的錯誤。錯誤已經消失,程序會建立。不幸的是,這段文字在圖形渲染完成之前似乎只是暫時顯示,我猜想現在OpenGL有一個問題。感謝發佈。 – Reanimation