如何将文本追加到文本框
How to append text to a TextBox?
我认为下面的代码应该是不言自明的。
#include <Windows.h>
static HWND textBoxInput;
static HWND button;
static HWND textBoxOutput;
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
int CALLBACK WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR cmdLine,int nCmdShow)
{
HWND hMainWindow;
WNDCLASS wc = {};
wc.lpfnWndProc = WindowProc;
wc.lpszClassName = "Main's window class";
wc.hInstance = hInstance;
RegisterClass(&wc);
hMainWindow = CreateWindow(wc.lpszClassName,"Append text main window",WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,500,400,NULL,NULL,hInstance,NULL);
error=GetLastError();
if(hMainWindow == NULL) return 1;
textBoxInput = CreateWindowEx(WS_EX_CLIENTEDGE, "Edit", NULL,WS_CHILD | WS_VISIBLE | ES_AUTOHSCROLL, 10, 10, 300, 21, hMainWindow, NULL, NULL, NULL);
button = CreateWindowEx(WS_EX_CLIENTEDGE,"Button","Append",WS_CHILD | WS_VISIBLE | ES_CENTER, 10, 41,75,30,hMainWindow,NULL,NULL,NULL);
textBoxOutput = CreateWindowEx(WS_EX_CLIENTEDGE,"Edit",TEXT("->This content is untouchable and unreadable!<-"),WS_CHILD | WS_VISIBLE | WS_VSCROLL | ES_AUTOVSCROLL | ES_MULTILINE | ES_READONLY ,10,81,500,90,hMainWindow,NULL,NULL,NULL);
ShowWindow(hMainWindow,SW_SHOW);
MSG msg = { };
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch(uMsg)
{
case WM_COMMAND:
if((HWND)lParam == button)
{
TCHAR* buffer = new TCHAR[150];
GetWindowText(textBoxInput,buffer,150);
SetWindowText(textBoxOutput,buffer);
//AppendWindowText(textBoxOutput,buffer,150) - I haven't found such function;
delete [] buffer;
}
break;
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
HBRUSH pedzel;
pedzel = CreateSolidBrush(RGB(10,250,10));
FillRect(hdc, &ps.rcPaint, pedzel);
EndPaint(hwnd, &ps);
return 0;
}
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
简而言之:该程序创建两个文本框和一个按钮,用于启动将内容从第一个复制到第二个的过程。SetWindowText
功能导致 清洁输出框 ,这显然是不需要的。
杰里·科芬回答后的更新
SendMessage(textBoxOutput,EM_SETSEL,-1,-1); //no difference between passing 0 or -1
SendMessage(textBoxOutput,EM_REPLACESEL,TRUE,(LPARAM)buffer);
令人惊讶的是,它在文本前面。我已经阅读了有关EM_SETSEL的文档,但我仍然想知道为什么它不将原始输入放在最后。
对于文本框(编辑控件),插入符号基本上是一个在同一位置开始和结束的"选择"。
使用SetSel 创建一个在控件中当前最后一个字符之后开始和结束的选定内容,然后使用 ReplaceSel 将该空选定内容替换为新文本。
由于您使用的是原始 Win32 API,因此SetSel
SendMessage(your_control, EM_SETSEL,-1, -1);
。ReplaceSel
将是:
SendMessage(your_control, EM_REPLACESEL, TRUE, string_to_add);
哎呀 - 正如问题后记中所述,这不能按原样工作。您需要从WM_GETTEXTLENGTH
(或GetWindowTextLength
)开始以获取文本的长度,然后将所选内容设置为结尾(即,开头和结尾都等于您刚刚获得的长度),然后替换所选内容。我很抱歉 - 我可能应该知道,在处理这样的事情时,我有一段时间没有做过
- 使用
GetWindowTextLength
查找其中文本的长度。 - 创建一个动态字符数组 (
std::vector<TCHAR>
),该长度加上追加文本的长度加上 null。 - 使用
GetWindowText
将当前文本存储在其中。 - 添加附加的文本(类似
_tcscat
)。 - 使用
SetWindowText
将所有内容放入文本框中。
总结:
int len = GetWindowTextLength(textbox);
std::vector<TCHAR> temp(len + lengthOfAppendedText + 1);
GetWindowText(textbox, temp.data(), temp.size());
_tcscat(temp.data(), appendedText);
SetWindowText(textbox, temp.data());
如果不使用 C++11,请将temp.data()
替换为 &temp[0]
。如果它必须与 C 兼容,它会回到 malloc
和 free
而不是 std::vector
,但考虑到没有进行大小调整,这不是太多额外的工作。
相关文章:
- 文本文件中的单词链表
- 从命令行c++发送文本文件名
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 2D数组来自文本输入,中间有空格
- 如何将内容数组写入文本文件?
- 无法通过空白将文本文件行分隔为矢量
- 我正在使用嵌套的while循环来解析具有多行的文本文件,但由于某种原因,它只通过第一行,我不知道为什么
- C++将文本文件中的数据读取到结构数组中
- 在指针的帮助下,文本文件中单词的频率
- C++ - 将文本追加到仅具有 NTFS"追加数据"权限的文件
- 外壳追加重定向替换现有文本
- 追加到文本文件在循环中无法正常工作
- 在文本文件中覆盖和追加行(C++)
- 如何将文本追加到文本框
- 将文本文件的内容追加到C++中的另一个文件
- C++ 在追加 Windows API 的特定位置插入文本
- C++ 不追加到文本文件
- 用于将后缀追加到无符号长文本的宏
- 读取显示到控制台的文本文件,然后追加文本文件
- 在form中追加文本