Bootstrap

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

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);
}

;