TCHAR[]、LPWSTR、LPTSTR 和 GetWindow 文本函数
因此,GetWindowText 在 MSDN 上的声明如下:
int GetWindowText(
HWND hWnd,
LPTSTR lpString,
int nMaxCount
);
但是,为了使代码正常工作,我们必须将第二个参数声明为,
TCHAR[255] WTitle;
然后调用函数 GetWindowText(hWnd,Wtitle,255);
LPTSTR 是一个指向 tchar 数组的指针,因此声明 LPTSTR 与声明 TCHAR[] 类似?但它不是这样工作的。 当使用 TCHAR[] 时,程序返回有效的 GetWindowText 结果(它是一个等于标题中符号数量的整数)。问题是:如何从 TCHAR[] 中获取确切的标题? 代码
TCHAR[255] WTitle;
cout<< WTitle;
类似or 的
cout<< *Wtitle;
返回数字。如何将其与给定字符串进行比较?
TCHAR[4] Test= __T("TEST")
if (WTitle == Test) do smth
也不起作用。
So the GetWindowText is declared on MSDN as follows:
int GetWindowText(
HWND hWnd,
LPTSTR lpString,
int nMaxCount
);
However for the code to work we have to declare the second parameter as
TCHAR[255] WTitle;
and then call the function GetWindowText(hWnd,Wtitle,255);
The LPTSTR is a pointer to an array of tchar, so declaring LPTSTR is similar to declaring TCHAR[]? It doesn't work this way though.
When using TCHAR[] the program returns valid GetWindowText result (it is an integer equal to the number of symbols in the title). The question is : how can I get the exact title out of TCHAR[] ? Code like
TCHAR[255] WTitle;
cout<< WTitle;
or
cout<< *Wtitle;
returns numbers. How can I compare this with a given string?
TCHAR[4] Test= __T("TEST")
if (WTitle == Test) do smth
doesn't work also.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
哇,让我们看看从哪里开始。
首先,WTitle 的声明需要如下所示:
接下来,如果 cout 不能正常写入,那是因为你处于 Unicode 模式,所以你需要这样做:
或者为了更好地适应整个 tchar 框架,你可以添加这个(实际上,我很惊讶这还不是 tchar.h 的一部分):
然后使用:
Wow, let's see where to start from.
First off, the declaration of WTitle needs to look like this:
Next, if cout is not working write, it's because you are in Unicode mode so you need to do this:
Or to fit better with the whole tchar framework, you can add this (actually, I'm surprised that this is not already part of tchar.h):
and then use:
好的,首先是一些定义。
“T”类型是计算结果为 CHAR(单字节)或 WCHAR(双字节)的定义,具体取决于您是否在构建设置中定义了 _UNICODE 符号。目的是让您使用一组源代码同时针对 ANSI 和 UNICODE。
定义:
...不等价。第一个定义了 100 个 TCHAR 的缓冲区。第二个定义了一个指向一个或多个 TCHAR 的指针,但不将其指向缓冲区。此外,
如果您有这样的函数:
...您可以将上述两个变量中的任何一个传递给:
好的,所以您获得数字的原因可能是因为您确实定义了UNICODE(所以字符很宽),并且您正在使用 cout,它特定于单字节字符。使用 wcout 代替:
最后,这些不起作用:
OK, a few definitions first.
The 'T' types are definitions that will evaluate to either CHAR (single byte) or WCHAR (double-byte), depending upon whether you've got the _UNICODE symbol defined in your build settings. The intent is to let you target both ANSI and UNICODE with a single set of source code.
The definitions:
...are not equivalent. The first defines a buffer of 100 TCHARs. The second defines a pointer to one or more TCHARs, but doesn't point it at a buffer. Further,
If you have a function like this:
...you can pass either of the above two variables in:
OK, so the reason you're getting numbers is probably because you do have UNICODE defined (so characters are wide), and you're using cout, which is specific to single-byte characters. Use wcout instead:
Finally, these won't work:
简短回答:除非您为 Win98 编码,否则请使用
wchar_t
代替TCHAR
,使用wcout
代替cout
Long版本:
TCHAR
类型的存在是为了允许以多种字符串模式编译代码。例如支持 ASCII 和 Unicode。TCHAR
类型将根据设置有条件地编译为适当的字符类型。所有新的 Win 系统都是基于 Unicode 的。当 ASCII 字符串传递给操作系统函数时,它们会被转换为 unicode 并调用实际函数。因此,最好在整个应用程序中只使用 Unicode。
Short answer: Unless you're coding for Win98, use
wchar_t
instead ofTCHAR
andwcout
instead ofcout
Long version:
The
TCHAR
type exists to allow for code to be compiled in multiple string modes. For example supporting ASCII and Unicode. TheTCHAR
type will conditionally compile to the appropriate character type based no the setting.All new Win systems are Unicode based. When ASCII strings are passed to OS functions, they are converted to unicode and the call the real function. So it's best to just use Unicode throughout your application.
使用 _tcscmp 或变体(它接受要比较的字符数)。 http://msdn.microsoft.com/en-us/library/e0z9k731。 aspx
就像:
Use _tcscmp or a variant (which takes in the number of characters to compare). http://msdn.microsoft.com/en-us/library/e0z9k731.aspx
Like:
在 C 中,wchar_t 是某些整数类型(通常是短整型)的 typedef。在 C++ 中,它必须是自己的独立类型——但 Microsoft 的编译器默认为它使用 typedef。要使其成为自己的单独类型,您需要使用
/Zc:wchar_t
编译器开关。顺便说一句,我不知道这是否能完全解决问题——我不确定该库是否有真正的 wchar_t 重载作为本机类型,将它们打印为字符而不是短整数。不过,一般来说,我建议不要乱用 Microsoft 的“T”变体——正确使用它们是一件痛苦的事情,而且它们的主要目的是提供与 16 位 Windows 的兼容性。鉴于距离该系列的最后一次发布已经过去了大约 10 年,在新代码中忽略它可能是安全的,除非您确实确定至少有一些客户真正使用它。
In C, wchar_t is a typedef for some integer type (usually short int). In C++, it's required to be a separate type of its own -- but Microsoft's compilers default to using a typedef for it anyway. To make it a separate type of its own, you need to use the
/Zc:wchar_t
compiler switch. Offhand, I don't know if that will entirely fix the problem though -- I'm not sure if the library has real overloads for wchar_t as a native type to print those out as characters instead of short ints.Generally speaking, however, I'd advise against messing with Microsoft's "T" variants anyway -- getting them right is a pain, and they were intended primarily to provide compatibility with 16-bit Windows anyway. Given that it's now been about 10 years since the last release in that line, it's probably safe to ignore it in new code unless you're really sure at least a few of your customers really use it.