首页 > 解决方案 > 在 C++ 中使用变量将动态文本添加到 WM_CREATE

问题描述

一直在研究这个问题,现在我终于解决了一个问题。我正在使用 VS 2019。

我正在创建的应用程序从 4 个源获取动态输入。花了一段时间,但我能够将动态文本添加到 WM_PAINT 执行以下操作:

case WM_PAINT:
{    
  PAINTSTRUCT ps;
  HDC hdc;
  
  TCHAR myMessage[10000];
  _tcscpy_s(myMessage, CA2T(st1.c_str()));
  hdc = BeginPaint(hwnd, &ps);

  TextOut(hdc, 25, 20, myMessage, _tcslen(myMessage));

  EndPaint(hwnd, &ps);
  break;
}

即使我可以添加动态文本,我也无法添加多行并且文本总是以白色背景显示。最后放弃了,转而尝试别的东西。

我使用 WM_CREATE 和 WM_CTLCOLORSTATIC 进行了以下工作,但我无法弄清楚如何使每一行的文本成为动态的(从变量中读取)。我在程序顶部定义了 IDC_USER_LABEL。

case WM_CREATE:
{                                   // Create text and exit button
  // The following will be simplified using tests and loops once I get variables to work 
  int textHStart  = 20; int textLineSpace = 16;
  int textVStart1 = 20; int textVStart2 = 60; int textVStart3 = 100;  int textVStart4 = 120; 
  string string1 = "This is the first line";
  string string2 = "This is the second line";
  string string3 = "The third line which is  followed by...";
  string string4 = "this line and is the end of the text written to the screen xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.";  // 59 chr
  int textLength1 = string1.length() * 10;
  int textLength2 = string2.length() * 10;
  int textLength3 = string3.length() * 10;
  int textLength4 = string4.length() * 10;

  HINSTANCE hIns = ((LPCREATESTRUCT)lParam)->hInstance;
  DWORD dwStyle = WS_CHILD | WS_VISIBLE;
  hCtl = CreateWindowEx(NULL, TEXT("Static"), TEXT("This is the first line"),                   dwStyle, textHStart, textVStart1, textLength1, textLineSpace, hwnd, (HMENU)IDC_USER_LABEL, hIns, NULL);
  hCtl = CreateWindowEx(NULL, TEXT("Static"), TEXT("This is the second line"),                  dwStyle, textHStart, textVStart2, textLength2, textLineSpace, hwnd, (HMENU)IDC_USER_LABEL, hIns, NULL);
  hCtl = CreateWindowEx(NULL, TEXT("Static"), TEXT("The third line which is  followed by..."),  dwStyle, textHStart, textVStart3, textLength3, textLineSpace, hwnd, (HMENU)IDC_USER_LABEL, hIns, NULL);
  hCtl = CreateWindowEx(NULL, TEXT("Static"), TEXT("this line and is the end of the text written to the screen xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"), 
                                                                                                dwStyle, textHStart, textVStart4, textLength4, textLineSpace, hwnd, (HMENU)IDC_USER_LABEL, hIns, NULL);
  // Create the exit button
  HWND hButton = CreateWindow(_T("button"), _T("Exit"), dwStyle, 385, 250, 60, 25, hwnd, (HMENU)IDC_BUTTON, hIns, 0);
  return 0;
}

case WM_CTLCOLORSTATIC: 
{
  HDC hEdit = (HDC)wParam;

  SetTextColor(hEdit, RGB(0, 0, 0));
  SetBkColor(hEdit, RGB(154, 228, 243));

  return (INT_PTR)GetStockObject(HOLLOW_BRUSH);
}

我想更改 CreateWindowEx 中的 TEXT 以使用变量而不是硬编码字符串。有可能还是我需要从头开始?

标签: c++winapi

解决方案


以下是我最终得到的结果。像我想要的那样工作,有很多优化和增强的可能性。

case WM_CREATE: {                                   // Create text and exit button
  int textHStart  = 20; int textLineSpace = 16;
  vector < int > textVStart = {20,60,100,120,160 };
  vector < string > newStrings;
  newStrings = getDynamicData();                    // Get the strings for output

  HINSTANCE hIns = ((LPCREATESTRUCT)lParam)->hInstance;
  DWORD dwStyle = WS_CHILD | WS_VISIBLE;

  for(int i = 0; i < newStrings.size(); i++) {
    hCtl = CreateWindowEx(NULL, TEXT("Static"), newStrings[i].c_str(), dwStyle, textHStart, textVStart[i], newStrings[i].length() * 10, textLineSpace, hwnd, (HMENU)IDC_USER_LABEL, hIns, NULL);
  }

  // Create the exit button
  HWND hButton = CreateWindow(_T("button"), _T("Exit"), dwStyle, 385, 250, 60, 25, hwnd, (HMENU)IDC_BUTTON, hIns, 0);
  return 0;
}

推荐阅读