读书人

WNDCLASS的lpszClassName成员赋值解决

发布时间: 2013-11-02 19:41:10 作者: rapoo

WNDCLASS的lpszClassName成员赋值
给WNDCLASS的lpszClassName成员赋值,在vs2008下报错
error C2440: '=' : cannot convert from 'const char [8]' to 'LPCWSTR'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

网上的程序都是直接用 "example"这种方式赋值.
我看了一下LPCWSTR 是 wchar_t * 类型的,
定义wchar_t型数组赋值成 "example"也报错.
究竟该怎样赋值?


#include <windows.h>
#include <stdio.h>

LRESULT CALLBACK WindowProc(
HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam
);

int WINAPI WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow
)
{
WNDCLASS wndcls;
wndcls.cbClsExtra = 0;
wndcls.cbWndExtra = 0;
wndcls.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
wndcls.hCursor = LoadCursor(NULL, IDC_HAND);
wndcls.hIcon = LoadIcon(NULL, IDI_QUESTION);
wndcls.lpfnWndProc = WindowProc;
wndcls.lpszClassName = "clsname"; //怎样给类名赋值?
wndcls.lpszMenuName = NULL;
wndcls.style = CS_VREDRAW | CS_HREDRAW;

RegisterClass(&wndcls);

}

LRESULT CALLBACK WindowProc(
HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam
)
{

return 0;
}

WNDCLASS lpszClassName
[解决办法]
wndcls.lpszClassName = _T("clsname"); //怎样给类名赋值?
[解决办法]
问题描述的很清楚,char*的字串不能复制给unicode的字串变量。

用_T(“xx”)或者L"xx"或者Text等等。

一般比较喜欢头文件包含“TCHAR.H”,然后使用TCHAR定义字符或者字符串,它自己会根据Project的字符编码自己进行选择。

读书人网 >C++

热点推荐