2013-04-08 209 views
1

我使用的是win32 API,這是我的代碼(我已經從這個網站獲得了一些幫助;))。用點擊改變字體的顏色

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, 
          WPARAM wParam, LPARAM lParam) 
    { 
    HDC hdc; 
    PAINTSTRUCT ps; 
    RECT  rect; 
    RECT  size; 
    int   width; 
    char *  widthHeight; 
    static char  Swidth[32]; 
    static char  Sheight[32]; 
    int   height; 
    char  sBottom[32]; 
    char  sTop[32]; 
    char  sLeft[32]; 
    char  sRight[32]; 
    char  SrightButtonClicked[32]; 
    static int rightButtonClicked = 0; 
    char  SleftButtonClicked[32]; 
    static int leftButtonClicked = 0; 
    static bool LeftButtonClicked = false; 
    static bool RightButtonClicked = false; 

    switch (message) 
     { 
      case WM_PAINT: 
       hdc = BeginPaint (hwnd, &ps); 

       if(LeftButtonClicked == true) 
       { 
        SetDCPenColor(hdc, RGB(0, 0, 255)); 
        LeftButtonClicked = false; 
       } 

       if(RightButtonClicked == true) 
       { 
        SetDCPenColor(hdc, RGB(255, 0, 0)); 
        RightButtonClicked = false; 
       } 

       GetClientRect (hwnd, &rect); 

       GetWindowRect(hwnd, &size); 

       //Variables used in outputting. 
       width = size.right - size.left; 
       itoa(width, Swidth, 10); 
       height = size.bottom - size.top; 
       itoa(height, Sheight, 10); 
       itoa(rect.bottom, sBottom, 10); 
       itoa(rect.top, sTop, 10); 
       itoa(rect.left, sLeft, 10); 
       itoa(rect.right, sRight, 10); 

       //Line 1, Width and height output 
       TextOut(hdc, 0, 0, "Here is my width: ", 18); 
       TextOut(hdc, 125, 0, Swidth, 5); 
       TextOut(hdc, 175, 0, "Here is my height: ", 18); 
       TextOut(hdc, 300, 0, Sheight, 4); 

       //Line 2, Output the bottom, right, left, and top of the window 
       TextOut(hdc, 0, 20, sBottom, strlen(sBottom)); 
       TextOut(hdc, 50, 20, sTop, strlen(sTop)); 
       TextOut(hdc, 100, 20, sRight, strlen(sRight)); 
       TextOut(hdc, 150, 20, sLeft, strlen(sLeft)); 

       //Outputs the number of times right click has been clicked 
       TextOut(hdc, 0, 40, "Right Button Clicked: ", 23); 
       itoa(rightButtonClicked, SrightButtonClicked, 10); 
       TextOut(hdc, 150, 40, SrightButtonClicked, strlen(SrightButtonClicked)); 

       //Output the number of times left click has been clicked 
       TextOut(hdc, 0, 60, "Left Button Clicked: ", 22); 
       itoa(leftButtonClicked, SleftButtonClicked, 10); 
       TextOut(hdc, 150, 60, SleftButtonClicked, strlen(SleftButtonClicked)); 
       EndPaint (hwnd, &ps); 
       return 0; 

      case WM_LBUTTONDOWN: 

       leftButtonClicked++; 
       LeftButtonClicked = true; 
       InvalidateRect(hwnd, 0, true); 
       return 0; 

      case WM_RBUTTONDOWN: 
       rightButtonClicked++; 
       RightButtonClicked = true; 

       //Uses char * widthHeight to concat all the resolution into one char to output in Messagebox 
       widthHeight = new char[strlen(Swidth) + strlen(Sheight) + 4]; 
       widthHeight = Swidth; 
       strcat(widthHeight, " x "); 
       strcat(widthHeight, Sheight); 

       //Has a message box pop up with resolution of window in Mouse Button OK format 
       MessageBox(NULL, widthHeight , "Right Mouse Button Pressed!", MB_OK); 
       InvalidateRect(hwnd, 0, true); 
       return 0; 

      case WM_DESTROY: 
       PostQuitMessage(0); 
       return 0; 

     } 

    return DefWindowProc (hwnd, message, wParam, lParam); 
    } 

現在我想讓它做點改變文字的顏色,當我點擊一個顏色的時候..說紅色?當我點擊右鍵時,它會轉到藍色。我該如何去做這件事?我搜索了很多,一切似乎都非常複雜。

+1

請閱讀[MSDN文章(http://msdn.microsoft.com/en-us/library/windows/desktop/dd145133(V = vs.85).aspx)爲TextOut,它顯示了它使用什麼顏色來呈現文本。鏈接到改變這些顏色的winapi函數在文章的底部。 Petzold的Programming Windows也是這種基本信息的極好來源。 – 2013-04-08 18:13:12

+0

謝謝=)。這工作。我沒有意識到這很簡單! – 2013-04-08 18:18:06

回答

0

試試這個(後您調用BeginPaint()調用將工作)開始輸出前:

// Set the Pen to Blue 
SetDCPenColor(hdc, RGB(0,0,255)); 

做你是想只設置在你的鼠標下的處理程序,您在您的油漆檢查的值處理程序。如果未設置[RGB(0,0,0)],則使用黑色,並且如果設置爲使用藍色[RGB(0,0,255)]。

編輯:也不要使用靜態hdc。每個人都想這樣做,但它不起作用。 HDC實際上由操作系統維護,因此C++將無法跟蹤它。最終這是一個保證失敗。在使用之前,總是得到一個新的hdc(或者任何名爲DC的東西)。

編輯2:試試這個......

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, 
         WPARAM wParam, LPARAM lParam) 
{ 
HDC hdc; 
PAINTSTRUCT ps; 
RECT  rect; 
RECT  size; 
int   width; 
char *  widthHeight; 
static char  Swidth[32]; 
static char  Sheight[32]; 
int   height; 
char  sBottom[32]; 
char  sTop[32]; 
char  sLeft[32]; 
char  sRight[32]; 
char  SrightButtonClicked[32]; 
static int rightButtonClicked = 0; 
char  SleftButtonClicked[32]; 
static int leftButtonClicked = 0; 
static bool LeftButtonClicked = false; 
static bool RightButtonClicked = false; 
static COLORREF pencolor = RGB(0,0,0); // Added by KRowe 

switch (message) 
    { 
     case WM_PAINT: 
      hdc = BeginPaint (hwnd, &ps); 

      SetDCPenColor(hdc, pencolor); // Added by KRowe 

      GetClientRect (hwnd, &rect); 
      GetWindowRect(hwnd, &size); 

      //Variables used in outputting. 
      width = size.right - size.left; 
      itoa(width, Swidth, 10); 
      height = size.bottom - size.top; 
      itoa(height, Sheight, 10); 
      itoa(rect.bottom, sBottom, 10); 
      itoa(rect.top, sTop, 10); 
      itoa(rect.left, sLeft, 10); 
      itoa(rect.right, sRight, 10); 

      //Line 1, Width and height output 
      TextOut(hdc, 0, 0, "Here is my width: ", 18); 
      TextOut(hdc, 125, 0, Swidth, 5); 
      TextOut(hdc, 175, 0, "Here is my height: ", 18); 
      TextOut(hdc, 300, 0, Sheight, 4); 

      //Line 2, Output the bottom, right, left, and top of the window 
      TextOut(hdc, 0, 20, sBottom, strlen(sBottom)); 
      TextOut(hdc, 50, 20, sTop, strlen(sTop)); 
      TextOut(hdc, 100, 20, sRight, strlen(sRight)); 
      TextOut(hdc, 150, 20, sLeft, strlen(sLeft)); 

      //Outputs the number of times right click has been clicked 
      TextOut(hdc, 0, 40, "Right Button Clicked: ", 23); 
      itoa(rightButtonClicked, SrightButtonClicked, 10); 
      TextOut(hdc, 150, 40, SrightButtonClicked, strlen(SrightButtonClicked)); 

      //Output the number of times left click has been clicked 
      TextOut(hdc, 0, 60, "Left Button Clicked: ", 22); 
      itoa(leftButtonClicked, SleftButtonClicked, 10); 
      TextOut(hdc, 150, 60, SleftButtonClicked, strlen(SleftButtonClicked)); 
      EndPaint (hwnd, &ps); 
      return 0; 

     case WM_LBUTTONDOWN: 

      leftButtonClicked++; 
      LeftButtonClicked = true; 
      pencolor = RGB(255,0,0); // Added by KRowe 
      InvalidateRect(hwnd, 0, true); 
      return 0; 

     case WM_RBUTTONDOWN: 
      rightButtonClicked++; 
      RightButtonClicked = true; 
      pencolor = RGB(0, 255,0); // Added by KRowe 
      widthHeight = new char[strlen(Swidth) + strlen(Sheight) + 4]; 
      widthHeight = Swidth; 
      strcat(widthHeight, " x "); 
      strcat(widthHeight, Sheight); 

      //Has a message box pop up with resolution of window in Mouse Button OK format 
      MessageBox(NULL, widthHeight , "Right Mouse Button Pressed!", MB_OK); 
      InvalidateRect(hwnd, 0, true); 
      return 0; 

     case WM_DESTROY: 
      PostQuitMessage(0); 
      return 0; 

    } 

return DefWindowProc (hwnd, message, wParam, lParam); 
} 
+0

沒有運氣。我必須讓我的hdc變量爲靜態,以使其不會中斷,但它仍然不會將文本變成藍色。 – 2013-04-08 18:02:08

+0

糾正我,如果我錯了,但我認爲hdc只能通過設置它等於BeginPaint和開始和結束油漆僅用於WM_PAINT ..所以我怎麼會得到一個新的hdc在我的case語句?在那裏做一個BeginPaint和EndPaint? – 2013-04-08 18:10:37

+0

我編輯我以前的帖子,包括我的新代碼,不工作.. :( – 2013-04-08 18:16:29