TCHAR[]、LPWSTR、LPTSTR 和 GetWindow 文本函数

发布于 2024-08-21 15:44:17 字数 691 浏览 4 评论 0原文

因此,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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

述情 2024-08-28 15:44:17

哇,让我们看看从哪里开始。

首先,WTitle 的声明需要如下所示:

TCHAR WTitle[255];

接下来,如果 cout 不能正常写入,那是因为你处于 Unicode 模式,所以你需要这样做:

wcout << WTitle;

或者为了更好地适应整个 tchar 框架,你可以添加这个(实际上,我很惊讶这还不是 tchar.h 的一部分):

#ifdef _UNICODE
    #define tcout wcout
#else
    #define tcout cout
#endif

然后使用:

tcout << WTitle;

Wow, let's see where to start from.

First off, the declaration of WTitle needs to look like this:

TCHAR WTitle[255];

Next, if cout is not working write, it's because you are in Unicode mode so you need to do this:

wcout << WTitle;

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):

#ifdef _UNICODE
    #define tcout wcout
#else
    #define tcout cout
#endif

and then use:

tcout << WTitle;
会傲 2024-08-28 15:44:17

好的,首先是一些定义。

“T”类型是计算结果为 CHAR(单字节)或 WCHAR(双字节)的定义,具体取决于您是否在构建设置中定义了 _UNICODE 符号。目的是让您使用一组源代码同时针对 ANSI 和 UNICODE。

定义:

TCHAR title[100];
TCHAR * pszTitle;

...等价。第一个定义了 100 个 TCHAR 的缓冲区。第二个定义了一个指向一个或多个 TCHAR 的指针,但不将其指向缓冲区。此外,

sizeof(title) == 100   (or 200, if _UNICODE symbol is defined)
sizeof(pszTitle) == 4  (size of a pointer in Win32)

如果您有这样的函数:

void foo(LPCTSTR str);

...您可以将上述两个变量中的任何一个传递给:

foo(title);    // passes in the address of title[0]
foo(pszTitle); // passes in a copy of the pointer value

好的,所以您获得数字的原因可能是因为您确实定义了UNICODE(所以字符很宽),并且您正在使用 cout,它特定于单字节字符。使用 wcout 代替:

wcout << title;

最后,这些不起作用:

TCHAR[4] Test == __T("TEST")   ("==" is equality comparison, not assignment)
if (WTitle == Test) do smth    (you're comparing pointers, use wcscmp or similar)

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:

TCHAR title[100];
TCHAR * pszTitle;

...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,

sizeof(title) == 100   (or 200, if _UNICODE symbol is defined)
sizeof(pszTitle) == 4  (size of a pointer in Win32)

If you have a function like this:

void foo(LPCTSTR str);

...you can pass either of the above two variables in:

foo(title);    // passes in the address of title[0]
foo(pszTitle); // passes in a copy of the pointer value

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:

wcout << title;

Finally, these won't work:

TCHAR[4] Test == __T("TEST")   ("==" is equality comparison, not assignment)
if (WTitle == Test) do smth    (you're comparing pointers, use wcscmp or similar)
寻找我们的幸福 2024-08-28 15:44:17

简短回答:除非您为 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 of TCHAR and wcout instead of cout

Long version:

The TCHAR type exists to allow for code to be compiled in multiple string modes. For example supporting ASCII and Unicode. The TCHAR 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.

热情消退 2024-08-28 15:44:17

使用 _tcscmp 或变体(它接受要比较的字符数)。 http://msdn.microsoft.com/en-us/library/e0z9k731。 aspx

就像:

if (_tcscmp(WTitle, Test) == 0) {
    // They are equal! Do something.
}

Use _tcscmp or a variant (which takes in the number of characters to compare). http://msdn.microsoft.com/en-us/library/e0z9k731.aspx

Like:

if (_tcscmp(WTitle, Test) == 0) {
    // They are equal! Do something.
}
仄言 2024-08-28 15:44:17

在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文