使用变量将动态文本添加到 C++ 中的 WM_CREATE
我已经研究了很长一段时间,现在我终于解决了一个问题.我使用的是 VS 2019.
Been hacking away on this for quite awhile and now I'm finally down to a single issue. I'm using VS 2019.
我正在创建的应用程序从 4 个源获取动态输入.花了一些时间,但我能够通过执行以下操作将动态文本添加到 WM_PAINT:
The application I'm creating gets dynamic input from 4 souces. It took awhile, but I was able to add dynamic text to WM_PAINT doing the following:
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;
}
即使我可以添加动态文本,我也无法添加多行并且文本总是以白色背景显示.最终放弃并继续尝试其他东西.
Even though I could add dynamic text, I could not add multiple lines and the text always showed with a white background. Finally gave up and moved on to trying something else.
我使用 WM_CREATE 和 WM_CTLCOLORSTATIC 使以下内容工作,但我不知道如何使每一行的文本动态化(从变量中读取).我已经在程序顶部定义了 IDC_USER_LABEL.
I got the following to work using WM_CREATE and WM_CTLCOLORSTATIC, but I cannot figure out how to make the text for each line dynamic (read from a variable). I've defined IDC_USER_LABEL at the top of the program.
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 以使用变量而不是硬编码字符串.有可能还是需要从头开始?
I want to change TEXT in CreateWindowEx to use a variable instead of a hard coded string. Is it possible or do I need to start over from scratch?
以下是我最终得到的.像我想要的那样工作,有很多优化和增强的可能性.
The following is what I finally ended up with. Works like I wanted with plenty of possibilities for optimizing and enhancing.
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;
}