【发布时间】:2016-03-28 17:36:36
【问题描述】:
你好,最近我一直在用 C++ 编程。请注意,我使用 -std=c++11 进行编译,并在 MingW 编译器中使用 Code::Blocks,因为某些程序需要它。
我的问题很简单:谁能给我提供一个函数来获取输入栏的文本?我问这个是因为我似乎找不到 WM_GETTEXT 或 GetWindowText 的工作实现。
编辑:
我有这些错误:
||=== 构建:在 ElitezLua 中发布(编译器:GNU GCC 编译器)===| C:\Users\PC\Desktop\ElitezLua\Main.cpp||在函数 'std::string GetText(HWND, int)':| C:\Users\PC\Desktop\ElitezLua\Main.cpp|35|error: cannot convert 'wchar_t*' to 'LPSTR {aka char*}' for argument '2' to 'int GetWindowTextA(HWND, LPSTR, int) '|
||=== 构建失败:1 个错误,0 个警告(0 分钟,4 秒)===|
使用此代码:
string GetText(HWND Box, int THN) {
int Length = GetWindowTextLength(GetDlgItem(Box, THN));
wchar_t * Text = new wchar_t[Length + 1];
return GetWindowText(GetDlgItem(Box, THN), Text, (Length + 1));
};
(错误在返回线上)
【问题讨论】:
-
您使用的是什么用户界面?
-
什么意思?我不明白,刚接触编程。
-
呃.. Win32 GUI 项目?
-
您的问题与
GetWindowText无关。问题是you are passing Unicode information when your project is set for non-Unicode。将您的项目更改为 Unicode,您会没事的。 -
停止使用蹩脚的工具。 Code::Blocks 仍默认为 MBCS 字符编码。 2016 年! Free Dev Tools - Visual Studio Community 2015 提供了更好的工具。目前,调用显式 API 版本(宽字符与 ANSI),即
GetWindowTextW而不是GetWindowText。