gpt4 book ai didi

c++ - MFC 如何解释 SetWindowTextW(LPCTSTR)?

转载 作者:行者123 更新时间:2023-11-30 02:17:23 25 4
gpt4 key购买 nike

在 MFC 中没有定义为 CWnd::SetWindowTextA/CWnd::SetWindowTextW 的方法,但是下面的代码将根据 Unicode 设置正确编译和运行:

//UNICODE is defined
BOOL CMyDialog::OnInitDialog()
{
CDialogEx::OnInitDialog();

//this line won't compile as expected
//SetWindowTextA(L"ANSI");

//this line compiles, but CWnd::SetWindowTextW doesn't exits
//SetWindowTextW ends up calling CWnd::SetWindowText
SetWindowTextW(L"Unicode");
return TRUE;
}

//UNICODE is not defined
BOOL CMyDialog::OnInitDialog()
{
CDialogEx::OnInitDialog();

//this line compiles, but CWnd::SetWindowTextA doesn't exits!
//SetWindowTextA ends up calling CWnd::SetWindowText
SetWindowTextA("ANSI");

//this line won't compile as expected
//SetWindowTextW(L"Unicode");
return TRUE;
}

根据宏,SetWindowText 映射到 SetWindowTextA/SetWindowTextW 是有意义的。但我不明白 wnd->SetWindowTextA/wnd->SetWindowTextW 是如何映射回 CWnd::SetWindowText 的。

最佳答案

这是 WinUser.h 中宏声明的副作用。它不仅适用于 Windows API 的全局函数声明,还适用于出现在代码中的任何其他名为 SetWindowText 的标识符:全局、局部或类作用域。

#ifdef UNICODE
#define SetWindowText SetWindowTextW
#else
#define SetWindowText SetWindowTextA
#endif // !UNICODE

因此,任何声明名为 SetWindowText 的方法的 C++ 类都会得到预处理器隐式转换的所有该方法。

我没有安装 MFC,但我知道 ATL 上的 CWindow 类存在这个方法,定义如下。

    class CWindow
{
public:
...

BOOL SetWindowText(_In_z_ LPCTSTR lpszString) throw()
{
ATLASSERT(::IsWindow(m_hWnd));
return ::SetWindowText(m_hWnd, lpszString);
}

...
};

但是在编译时,上面的代码(用于调试版本)将被预处理器转换成类似下面的代码:

BOOL SetWindowTextW(  LPCTSTR lpszString) throw()
{
(void)( (!!((::IsWindow(m_hWnd)))) || (1 != _CrtDbgReportW(2, L"c:\\program files...
return ::SetWindowTextW(m_hWnd, lpszString);
}

具有讽刺意味的是,LPCTSTR 方法参数是 typedef 而不是宏替换,但您明白了。

如果您有一个足够大的 Windows 应用程序,您自己定义的现有 C++ 类之一很可能具有与 Windows API 匹配的方法或成员变量。而且它得到了同样的待遇。

关于c++ - MFC 如何解释 SetWindowTextW(LPCTSTR)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53624512/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com