gpt4 book ai didi

c++ - 如何将文本附加到文本框?

转载 作者:太空狗 更新时间:2023-10-29 23:36:44 24 4
gpt4 key购买 nike

我认为下面的代码应该是不言自明的。

#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 函数导致清理输出框,这显然不是我们想要的。

Jerry Cofinn 回答后更新

SendMessage(textBoxOutput,EM_SETSEL,-1,-1); //no difference between passing 0 or -1
SendMessage(textBoxOutput,EM_REPLACESEL,TRUE,(LPARAM)buffer);

令人惊讶的是,它前置文本。我读过 the documentation关于 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)开始获取文本的长度,然后将选择设置为结尾(即开头和结尾都等于你刚得到的长度),然后替换选择。抱歉 - 在处理我有一段时间没有做过的此类事情时,我可能应该知道最好不要凭内存。

关于c++ - 如何将文本附加到文本框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12537456/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com