- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我有一个 EditBox HWND tbLog
,以及以下函数(不起作用):
void appendLogText(char* newText)
{
int len = GetWindowTextLength(tbLog);
char* logText = new char[len + strlen(newText) + 1];
GetWindowText(tbLog, logText, len);
strcat(logText, newText);
SendMessage(tbLog, WM_SETTEXT, NULL, (LPARAM)TEXT(logText));
delete[] logText;
}
我这样调用它:
appendLogText("Put something in the Edit box.\r\n");
appendLogText("Put something else in the Edit box.\r\n");
首先,TEXT()
到底做了什么?我尝试过使用/不使用它:(LPARAM)logText
和 (LPARAM)TEXT(logText)
据我所知没有区别。
其次,我的追加函数做错了什么?如果我注释掉我的 delete
行,那么在我第一次运行附加函数时,我的 Editbox 中会出现垃圾,然后是消息。第二次运行时,程序崩溃了。如果我没有将其注释掉,那么即使是第一次它也会崩溃。
最佳答案
我会用 C 重写函数,如下所示:
void appendLogText(LPCTSTR newText)
{
DWORD l,r;
SendMessage(tbLog, EM_GETSEL,(WPARAM)&l,(LPARAM)&r);
SendMessage(tbLog, EM_SETSEL, -1, -1);
SendMessage(tbLog, EM_REPLACESEL, 0, (LPARAM)newText);
SendMessage(tbLog, EM_SETSEL,l,r);
}
保存和恢复现有选择很重要,否则对于想要选择和复制控件外的某些文本的任何人来说,控件会变得非常烦人。
此外,使用 LPCTSTR 可确保在使用多字节或 unicode 字符集构建时可以调用该函数。
TEXT 宏不合适,应该用来包装字符串文字:
LPCTSTR someString = TEXT("string literal");
Windows NT 操作系统本身是 unicode,因此构建多字节应用程序效率低下。在字符串文字上使用 TEXT() 并使用 LPTSTR 代替“char*”有助于转换为 unicode。但实际上,在 Windows 上显式切换到 unicode 编程可能是最有效的:代替 char、strlen、std::string,使用 wchar_t、std::wstring、wsclen 和 L“字符串文字”。
将项目build设置切换为 Unicode 将使所有 Windows API 函数都需要 unicode 字符串。
我很晚才注意到,将 -1 作为 EM_SETSEL 的 WPARAM 传递只会取消选择任何选择,但不会移动插入点。所以答案应该修改为(也未经测试):
void appendLogText(HWND hWndLog, LPCTSTR newText)
{
int left,right;
int len = GetWindowTextLength(hWndLog);
SendMessage(hWndLog, EM_GETSEL,(WPARAM)&left,(LPARAM)&right);
SendMessage(hWndLog, EM_SETSEL, len, len);
SendMessage(hWndLog, EM_REPLACESEL, 0, (LPARAM)newText);
SendMessage(hWndLog, EM_SETSEL,left,right);
}
关于c++ - 在 C++ 中将文本附加到 Win32 EditBox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7200137/
我想在警报对话框中使用多个编辑文本,输入应由用户填写,并在单击警报消息的确定按钮后保存信息。 最佳答案 为此警报创建一个单独的布局,其中包含所有编辑文本并使用它。 关于android - 如何在警告对
我正在 Borland C++ Builder 中创建简单的应用程序。 我的代码是这样的: void __fastcall TForm1::Button3Click(TObject *Sender)
我从桌面选择了多个文件。 CFileDialog fileDlg(TRUE, _T("*.txt"), NULL,
1]无输入的4位数字输入框: 2] 4位数字输入框输入: 随着用户继续输入 4 位数的 pin 输入框变成了点,如果用户想要删除最后一位按下后退按钮,用户输入将被删除,点再次转换为输入框。如果用户中间
我目前有这个: 是否可以去除黑色描边并只突出显示?它是否违反了任何关于 Material 设计的谷歌指南 最佳答案 这完全是一个 android Lollipop 错误。它发生在 EditText 的
如何在多行 EditBox 中切换自动换行? 我可以使用或不使用它来创建它,但我不知道应该使用哪个消息来切换包装模式。 最佳答案 EditBox 不支持切换自动换行。没有可以发送的消息,也不能更改窗口
我使用这段代码在 XML 中创建了一个编辑框: 文本框呈现如下: 我怎样才能得到原生的 android EditText 焦点时橙色边框,白色背景等
我正在尝试创建一个 EditText 框,如下所示,它的左侧有一个图像,右侧有一个可编辑的文本区域,但我似乎完全不知道如何在机器人:|我可以找到将图像放在背景中(这样文本可以覆盖它)以及如何制作圆角的
如何使用 Win32 WinAPI 创建一个 EditBox,使其看起来像在 Visual C# 或 VB 中的 VS 设计器中放置一个编辑框(具有漂亮的顶部边框等)?这是一张图片,展示了它的外观以及
我有一个如下定义的编辑框 我添加了一个背景图片,它是一个自定义的编辑框。现在的问题是,当我在其中输入文本时,因为我使用了 wrap_co
是否有任何 WinAPI 可以在编辑框中获取选定的文本?取光标的初始位置和最终位置?我告诉编辑框的 HWND 和函数返回所选文本的函数? 最佳答案 我不知道执行此操作的单个函数。 发送EM_GETSE
我想知道如何制作一个简单的函数来使光标在 EditBox 中出现和消失,这是一个简单的函数,我传递窗口的 HWND 和 EditBox 的名称只是为了消失和出现光标? 谢谢(: 最佳答案 HideCa
我有一个小问题困扰了我几个小时。 在我的 WinForms (.NET 3.5) 应用程序中,我在运行时在 TableLayoutPanel 中创建了一些 ComboBoxes (DropDownSt
我有一个编辑框,我在其中定义了一个 android:drawableLeft 属性。 我希望 EditBox 看起来像这样(来自 Instagram 应用): 请注意,图片已正确居中,并且图片的上/右
我有一个带有 2 个编辑文本框的简单应用程序。当在第一个框中输入输入时,将执行一组计算。如果将输入输入另一个,则会发生一组不同的计算。 因此,如果在第一个框 etBox1 中键入了一个数字,并且用户离
我的图层上有一个编辑框。 var ebox = new cc.EditBox(cc.p(200, 30)); ebox.setPosition(size.width / 2 - 50, size.he
我需要将搜索图像镜头作为提示放入 EditText 中。可以在 Android 中实现吗? 最好的问候,, 最佳答案 我总是通过更改可绘制对象的权限来解决这个问题。 您可以在布局 xml 文件上执行此
我有一个 EditBox HWND tbLog,以及以下函数(不起作用): void appendLogText(char* newText) { int len = GetWindowTextL
我有 C#.NET WF 中的简单计算器。但是当我向编辑框中插入数字以外的内容时,我的程序崩溃了。我应该如何避免这种崩溃? private void doMath() { int n1 = I
我在 QML 中创建了一个带有一些文本框的 QtQuick 应用程序。我想在我的 C++ 代码中使用这些文本框的值。那么如何从 C++ 代码中获取这些值呢? 最佳答案 它可以是这样的: QML 文件:
我是一名优秀的程序员,十分优秀!