win32 WM_SETTEXT not working

win32 WM_SETTEXT not working

本文关键字:not working SETTEXT WM win32      更新时间:2023-10-16

我做了一个像这样的小文本框

EBX =   CreateWindow(TEXT("EDIT"),  TEXT(""),  WS_CHILD | WS_VISIBLE | WS_TABSTOP | ES_NUMBER | WS_BORDER, 
            client.right - offset[1] - 200, client.top + offset[2] - 27, 
            45, 25, hwnd, (HMENU)ID_EDIT_SPEED, NULL, NULL);

一切正常但是当我尝试像这样改变里面的文本时,我遇到了一些问题

SendMessage(EBX, WM_SETTEXT, 0, (LPARAM)"12"); // working
int a = 40;
SendMessage(EBX, WM_SETTEXT, 0, (LPARAM)a); // not working

你知道怎么回事吗?

40不是字符串,"40"是。如果要将数字转换为字符串,必须使用sprintf等函数。

int a = 40;
char str[20];
StringCchPrintf(str, _countof(str), "%ld", a);
SendMessage(EBX, WM_SETTEXT, 0, (LPARAM)str);

不能盲目地将int类型转换为char*,使用sprintf、stringstream或std::to_string创建包含int值的文字表示形式的字符串。
或者,如果你想输出值为40的字符,你需要传递指针到null终止字符数组。像

char str[2];
str[0]=40;
str[1]=0;

将40转换为c-string并在sendmessage函数中使用

char buffer [33];
int i =40;
itoa (i,buffer,10);
SendMessage(EBX, WM_SETTEXT, 0, (LPARAM)buffer);