C++ MFC如何解释SetWindowTextW(LPCTSTR)?
在MFC中,没有定义为C++ MFC如何解释SetWindowTextW(LPCTSTR)?,c++,visual-studio,mfc,C++,Visual Studio,Mfc,在MFC中,没有定义为CWnd::SetWindowTextA/CWnd::SetWindowTextW的方法,但以下代码将根据Unicode设置正确编译和运行: //UNICODE is defined BOOL CMyDialog::OnInitDialog() { CDialogEx::OnInitDialog(); //this line won't compile as expected //SetWindowTextA(L"ANSI"); //th
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
<任何一个C++类声明一个方法,叫做“代码> StWistWOWTEXT/<代码>,获取所有由预处理器隐式转换的方法。< /P>
我没有安装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'd,而不是宏替换,但是您知道了
如果你有足够大的Windows应用程序,那么你定义的一个C++类中有一个与Windows API匹配的方法或成员变量的几率非常高。它也得到了同样的治疗