Win32 EditControl多行文本框自动换行,并在添加新行时自动将光标移到末尾

【文本框属性设置】
设为多行文本框:Multiline=True
允许按回车键换行:Want Return=True
自动换行:Auto HScroll=False
在最后一行按回车键,自动向上滚动:Auto VScroll=True
显示垂直滚动条:Vertical Scroll=True

visual studio里面使用以_s结尾的函数时,凡是参数名是maxcount的,都要减1,否则程序会闪退
_snprintf_s函数的正确用法:
_snprintf_s(str, sizeof(str), sizeof(str) - 1, "fmt"); // C语言专用
_snprintf_s(str, sizeof(str) - 1, "fmt"); // C++专用, 和上面的语句等效

【程序代码】

main.c:

#include <stdio.h>
#include <tchar.h>
#include <time.h>
#include <Windows.h>
#include <CommCtrl.h>
#include <WindowsX.h>
#include "resource.h"

#pragma comment(lib, "comctl32.lib")
#pragma comment(linker, "\"/manifestdependency:type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' language='*' publicKeyToken='6595b64144ccf1df'\"")

void add_new_line(HWND hedit, HWND hstatic)
{
	char str[100];
	char *p, *q;
	int i, len, size;
	time_t t;
	struct tm tm;
	
	// 根据当前时间生成新行内容
	time(&t);
	localtime_s(&tm, &t);
	strftime(str, sizeof(str), "当前时间: %Y-%m-%d %H:%M:%S", &tm);

	len = GetWindowTextLengthA(hedit) + 2 + strlen(str); // 新文本长度 = 原有文本长度 + 换行符长度 + 新行长度
	size = len + 1; // 新文本长度 + 字符串结束符长度
	p = (char *)malloc(size);
	if (p != NULL)
	{
		// 新添加一行
		GetWindowTextA(hedit, p, size); // 获取原有文本
		if (p[0] != '\0')
			strcat_s(p, size, "\r\n"); // 添加换行符
		strcat_s(p, size, str); // 添加新行
		SetWindowTextA(hedit, p); // 设置新文本

		// 计算总行数
		i = 1;
		for (q = strstr(p, "\r\n"); q != NULL; q = strstr(q + 2, "\r\n"))
			i++;
		_snprintf_s(str, sizeof(str), sizeof(str) - 1, "行数: %d", i);
		SetWindowTextA(hstatic, str);
		free(p);

		// 光标移动到末尾
		SetFocus(hedit);
		Edit_Scroll(hedit, i, 0);
		Edit_SetSel(hedit, len, len);
	}
}

LRESULT CALLBACK DlgProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	int wmId;
	HWND hedit, hstatic;

	switch (uMsg)
	{
	case WM_COMMAND:
		wmId = LOWORD(wParam);
		switch (wmId)
		{
		case IDOK:
			hedit = GetDlgItem(hDlg, IDC_EDIT1);
			hstatic = GetDlgItem(hDlg, IDC_STATIC2);
			add_new_line(hedit, hstatic);
			break;
		case IDCANCEL:
			EndDialog(hDlg, 0);
			break;
		}
	case WM_INITDIALOG:
		break;
	}
	return 0;
}

int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
	int ret;

	InitCommonControls();
	ret = DialogBox(hInstance, MAKEINTRESOURCE(IDD_DIALOG1), NULL, DlgProc);
	return ret;
}

【程序运行结果】

如果您使用的是 FarPoint 的 WinForms 控件,可以考虑在单元格中使用自定义绘制来解决此问题。您可以从 FpSpread 控件的 CellType 属性中选择 Custom,然后在程序中实现自定义绘制逻辑。 在自定义绘制逻辑中,您可以遍历单元格文本中的每个字符,并在必要时手动插入换行符(例如在左括号和右括号之间不进行换行)。然后,您可以使用 Graphics 对象进行文本绘制,并将其输出到单元格中。 下面是一些示例代码,用于在 FarPoint FpSpread 控件的自定义单元格类型中实现包含自定义绘制逻辑的单元格: ```csharp using FarPoint.Win.Spread.CellType; using FarPoint.Win.Spread.DrawingSpace; using System.Drawing; public class CustomWrapCellType : TextCellType { public override void Paint(Graphics g, Rectangle r, object value) { string text = value as string; if (text == null) return; // 计算行高和列宽 int lineHeight = (int)g.MeasureString(text, this.Font).Height; int columnWidth = r.Width; // 遍历每个字符并插入换行符 for (int i = 0; i < text.Length; i++) { char c = text[i]; if (c == '(' && i < text.Length - 1 && text[i + 1] == ')') { // 避免在括号内部换行 continue; } if (c == ' ' && i < text.Length - 1 && text[i + 1] == '(') { // 避免在括号前面换行 continue; } if (c == ')' && i > 0 && text[i - 1] == '(') { // 避免在括号后面换行 continue; } if (c == '\r' || c == '\n') { // 如果文本中已经包含换行符,则不需要再插入 continue; } if (g.MeasureString(text.Substring(0, i + 1), this.Font).Width > columnWidth) { // 当前行已经超出列宽,需要插入换行符 text = text.Insert(i, Environment.NewLine); i += Environment.NewLine.Length; } } // 绘制文本 using (StringFormat sf = new StringFormat()) { sf.Alignment = StringAlignment.Near; sf.LineAlignment = StringAlignment.Near; sf.FormatFlags = StringFormatFlags.NoWrap; RectangleF rect = new RectangleF(r.Left, r.Top, columnWidth, lineHeight); g.DrawString(text, this.Font, new SolidBrush(this.ForeColor), rect, sf); } } } ``` 使用以上代码,您可以将包含自定义绘制逻辑的单元格类型应用于 FpSpread 控件中特定的单元格。例如: ```csharp FpSpread spread = new FpSpread(); SheetView sheet = spread.ActiveSheet; CustomWrapCellType cellType = new CustomWrapCellType(); sheet.Cells[0, 0].CellType = cellType; sheet.Cells[0, 0].Value = "这是一段包含括号的文本(不会换行)"; sheet.Cells[1, 0].CellType = cellType; sheet.Cells[1, 0].Value = "这是一段包含括号的文本,(但是括号前后不会换行)"; sheet.Cells[2, 0].CellType = cellType; sheet.Cells[2, 0].Value = "这是一段较长的文本,将会自动换行,但不会在括号内部换行。如果文本中已经包含换行符,则不会再插入。"; ``` 以上代码将在 FpSpread 控件的第一列中显示三个单元格,每个单元格包含不同形式的文本,并使用自定义绘制逻辑进行换行处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

巨大八爪鱼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值