使用Edit控件作为消息输出窗口的实现

本文详细介绍了如何在Windows中创建Edit控件,以及如何使用show_msg函数向该控件显示消息,包括消息窗口的刷新策略。着重展示了如何处理常量字符串的显示和消息计数控制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、创建Edit控件

int x0=2,y0=2,control_width=240,control_height=200;
HWND hWndMsg = CreateWindow(L"edit", L"",
		WS_CHILD | WS_VISIBLE | WS_VSCROLL | ES_AUTOVSCROLL | ES_MULTILINE,
		x0, y0, control_width, control_height, hWndMain, NULL, NULL, NULL);

2、建立输出消息的函数

// 显示消息到消息窗口
int mTextCounts = 0;
void show_msg(char * msg)
{
	char EditText[4096] = "";

	strcat_s(EditText, msg);

	int len_in = strlen(EditText);
	len_in++;
	int nwLen = MultiByteToWideChar(CP_ACP, 0, EditText, len_in, NULL, 0);
	LPWSTR lpszPath = new WCHAR[len_in];
	MultiByteToWideChar(CP_ACP, 0, EditText, len_in, lpszPath, nwLen);


	mTextCounts++;								//消息窗口接受消息总数目    
	if (mTextCounts >= 100)
	{
		mTextCounts = 0;
		SendMessage(hWndMsg, WM_SETTEXT, true, (LPARAM)lpszPath);
	}
	SendMessage(hWndMsg, EM_SETSEL, -2, -1);
	SendMessage(hWndMsg, EM_REPLACESEL, true, (LPARAM)lpszPath);
	SendMessage(hWndMsg, WM_VSCROLL, SB_BOTTOM, true);
}

3、调用函数进行显示


#define MAX_StringNum	260
char	app_msg[MAX_StringNum];	// 系统运行信息
sprintf_s(app_msg, "Window Created.\r\n");
show_msg(app_msg);

4、重载,实现常量字符串显示

void show_msg(const char msg[]);

void show_msg(const char msg[])
{
	char EditText[4096] = "";

	strcat_s(EditText, msg);

	int len_in = strlen(EditText);
	len_in++;
	int nwLen = MultiByteToWideChar(CP_ACP, 0, EditText, len_in, NULL, 0);
	LPWSTR lpszPath = new WCHAR[len_in];
	MultiByteToWideChar(CP_ACP, 0, EditText, len_in, lpszPath, nwLen);


	mTextCounts++;								//消息窗口接受消息总数目    
	if (mTextCounts >= 100)
	{
		mTextCounts = 0;
		SendMessage(hWndMsg, WM_SETTEXT, true, (LPARAM)lpszPath);
	}
	SendMessage(hWndMsg, EM_SETSEL, -2, -1);
	SendMessage(hWndMsg, EM_REPLACESEL, true, (LPARAM)lpszPath);
	SendMessage(hWndMsg, WM_VSCROLL, SB_BOTTOM, true);
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值