https://codebus.cn/bestans/textbox-sample
这个可以先获取文本框的id,然后设置为""
我试着写了一下,通过按钮实现textbox内容清空
#include <graphics.h>
#include <conio.h>
int main()
{
initgraph(640, 480); // create a window
setbkcolor(WHITE); // set background color
settextcolor(BLACK); // set text color
// draw the text box
rectangle(100, 100, 300, 150);
outtextxy(105, 110, "Enter your name:");
// draw the button
rectangle(200, 170, 260, 200);
settextstyle(10, 0, "Arial");
outtextxy(205, 180, "Submit");
// draw the clear button
rectangle(270, 170, 330, 200);
settextstyle(10, 0, "Arial");
outtextxy(275, 180, "Clear");
// get user input
char name[20];
int i = 0;
while (1)
{
if (MouseHit()) // if mouse is clicked
{
MOUSEMSG msg = GetMouseMsg();
if (msg.uMsg == WM_LBUTTONDOWN && msg.x > 200 && msg.x < 260 && msg.y > 170 && msg.y < 200) // if submit button is clicked
break;
else if (msg.uMsg == WM_LBUTTONDOWN && msg.x > 270 && msg.x < 330 && msg.y > 170 && msg.y < 200) // if clear button is clicked
{
for (int j = 0; j < i; j++)
outtextxy(105 + j * 8, 125, " "); // erase the characters
i = 0;
name[0] = '\0';
}
}
char c = getch();
if (c == '\r') // if enter is pressed
break;
else if (c == '\b') // if backspace is pressed
{
if (i > 0)
{
i--;
outtextxy(105 + i * 8, 125, " "); // erase the character
}
}
else if (i < 18) // if a character is entered
{
name[i] = c;
name[i + 1] = '\0';
outtextxy(105 + i * 8, 125, &c); // display the character
i++;
}
}
// display the input
settextstyle(20, 0, "Arial");
outtextxy(100, 200, "Hello, ");
outtextxy(190, 200, name);
closegraph(); // close the window
return 0;
}
该回答引用ChatGPT
如有疑问,可以回复我!
为了在点击 OK 之后清空文本框内容,你可以在 On_btnOk_Click() 函数中添加代码来清空文本框内容。
首先,你需要给 EasyTextBox 类添加一个新的函数来清空文本框内容。在 EasyTextBox 类中添加如下函数:
void Clear()
{
text[0] = 0; // 将第一个字符设置为空字符,从而清空文本框内容
Show(); // 重新显示文本框
}
然后,在 On_btnOk_Click() 函数中调用 Clear() 函数来清空文本框内容。修改 On_btnOk_Click() 函数如下:
void On_btnOk_Click()
{
if (wcscmp(L"123", txtPwd.Text()))
MessageBox(GetHWnd(), L"密码错误", L"错误", MB_OK);
else
{
wchar_t s[100] = L"Hello, ";
wcscat_s(s, 100, txtName.Text());
MessageBox(GetHWnd(), s, L"Hello", MB_OK);
// 清空文本框内容
txtName.Clear();
txtPwd.Clear();
}
}
现在,当用户点击 OK 按钮时,文本框的内容将被清空。
在这个代码的基础上,创建一个按钮和一个文本框。点击按钮时,文本框中的文本将被清除。
具体来说,我们需要定义一个新的类EasyButton,该类需要具有Create和Show方法,类似于EasyTextBox。我们还需要为EasyButton定义一个Check方法,该方法检查给定的x和y坐标是否在按钮范围内,并在用户单击按钮时调用。当单击按钮时,它应该调用Clear方法,该方法清除文本框中的文本,并重新显示文本框。
首先,我们需要在EasyTextBox类中添加一个成员变量来表示文本框中的按钮。由于我们只需要一个简单的按钮,所以我们可以用一个RECT结构体来表示它的坐标和大小。因此,我们可以将EasyTextBox类改为以下形式:
class EasyTextBox {
private:
int left = 0, top = 0, right = 0, bottom = 0; // 控件坐标
wchar_t* text = NULL; // 控件内容
size_t maxlen = 0; // 文本框最大内容长度
RECT buttonRect = { 0 }; // 按钮矩形
public:
void Create(int x1, int y1, int x2, int y2, int max) {
//...
// 在创建文本框时,同时创建按钮
buttonRect = { x2 - 50, y1, x2 - 10, y1 + 20 };
// 绘制用户界面
Show();
}
// ...
};
现在我们已经在EasyTextBox类中添加了一个表示按钮的RECT结构体。接下来我们需要在Show()函数中绘制这个按钮。我们可以使用fillrectangle()函数和outtextxy()函数绘制按钮的矩形和文字,代码如下:
void Show() {
//...
// 绘制文本框
fillrectangle(left, top, right, bottom);
outtextxy(left + 10, top + 5, text);
// 绘制按钮
setfillcolor(LIGHTGRAY);
fillrectangle(buttonRect.left, buttonRect.top, buttonRect.right, buttonRect.bottom);
settextcolor(BLACK);
outtextxy(buttonRect.left + 10, buttonRect.top + 5, L"Clear");
//
}
现在我们已经在EasyTextBox类中添加了一个表示按钮的RECT结构体,并在Show()函数中绘制了这个按钮。接下来我们需要在OnMessage()函数中处理按钮的点击事件。
当用户点击按钮时,我们需要将文本框中的内容清空。因此,在处理鼠标点击事件时,我们需要判断用户是否点击了按钮,并在这种情况下清空文本框中的内容。修改后的OnMessage()函数如下:
void OnMessage() {
//...
ExMessage msg;
while (binput) {
while (binput && peekmessage(&msg, EM_MOUSE | EM_CHAR, false)) {
//...
// 处理鼠标点击事件
if (msg.message == WM_LBUTTONDOWN) {
if (Check(msg.x, msg.y)) {
// 鼠标点击文本框,进入文本输入状态
binput = true;
}
else if (msg.x >= buttonRect.left && msg.x <= buttonRect.right &&
msg.y >= buttonRect.top && msg.y <= buttonRect.bottom) {
//