c++ - 单击更改字体颜色

标签 c++ api winapi

我正在使用 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);
     } 

现在我想让它做一些事情,比如当我左键单击一种颜色时改变文本颜色。说红色?当我右键单击它时,它会变成蓝色。我该怎么做呢?我用谷歌搜索了很多,一切看起来都很复杂。

最佳答案

在开始输出之前尝试此操作(在您的 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);
 } 

关于c++ - 单击更改字体颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15885702/

相关文章:

c++ - 使用 C++ Builder 10 西雅图时出现很多歧义错误

c++ - STL vector 元素去除效率

javascript - Angular 6 - 通过 API (OpenLayers) 使用用户位置

javascript - 谷歌浏览器也有一个控制台对象。它的等效 API 页面在哪里?

c# - Pinvoke 返回 int, API

c++ - UXTheme:绘制无边框的组合框雪佛龙

c++ - 什么会阻止 GetPixel 工作?

c++ - 如何区分控制台程序是在 Powershell 中打开还是在 Windows 终端中打开?

c++ - 使用 BitBlt 获取另一个窗口的单个像素

c++ - STL中的优先队列