![](/img/trans.png)
[英]IntelliSense: argument of type “const char *” is incompatible with parameter of type “LPCWSTR”
[英]2 IntelliSense: argument of type “const char *” is incompatible with parameter of type “LPCWSTR”
Stackflow成員----我是c ++ WINAPI的初學者---當我在我的第一個WIN32 GUI中進行編碼時,出現了一些錯誤[輸出] ---是否有任何推薦的書籍需要用C ++進行改進---我的代碼:
#include <windows.h>
const char g_szClassName[] = ("myWindowClass");
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
switch(msg) {
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow) {
WNDCLASSEX wc;
HWND hwnd;
MSG Msg;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName = NULL;
wc.lpszClassName = (g_szClassName);
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if(!RegisterClassEx(&wc)) {
MessageBox(NULL,
L"Window Registration Failed!",
L"Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
// Creating the Window
hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
(g_szClassName),
L"The title of my window",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 240, 120, NULL, NULL, hInstance, NULL);
if(hwnd == NULL) {
MessageBox(NULL,
L"Window Creation Failed!",
L"Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
// Step 3: The Message Loop
while(GetMessage(&Msg, NULL, 0, 0) > 0) {
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}
輸出:
3 IntelliSense: argument of type "const char *" is incompatible with parameter of type "LPCWSTR" c:\Users\Youssef\Documents\Visual Studio 2012\Projects\Win32Project2\Win32Project2\Win32Project2.cpp 46 2 Win32Project2
2 IntelliSense: a value of type "const char *" cannot be assigned to an entity of type "LPCWSTR" c:\Users\Youssef\Documents\Visual Studio 2012\Projects\Win32Project2\Win32Project2\Win32Project2.cpp 34 19 Win32Project2
Error 1 error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "stdafx.h"' to your source? c:\users\youssef\documents\visual studio 2012\projects\win32project2\win32project2\win32project2.cpp 65 1 Win32Project2
您遇到的編譯問題與字符串類型和Windows程序有關。
請注意,Windows程序有兩種字符集“構建類型”。 第一個是MBCS
,第二個是Unicode
。 默認情況下(假設您使用的是Visual Studio),構建類型為Unicode
,這意味着各種Windows API函數將處理寬字符串,而不是char
或基於char
的字符串。
解決問題的最簡單方法是對字符數組使用TCHAR
對要從Windows API函數發送和接收的字符串文字使用_T()
宏。 這將確保您的應用程序將針對Unicode以及MBCS進行編譯(盡管現在已經不贊成使用MBCS程序了)。
這行:
wc.lpszClassName = (g_szClassName);
產生一個問題,因為您正在編譯Unicode應用程序,並且WNDCLASSEX
此成員采用LPCTSTR
(指向TCHAR
的指針),而不是const char *
。
https://msdn.microsoft.com/zh-cn/library/windows/desktop/ms633577%28v=vs.85%29.aspx
所以對於這個問題
const char g_szClassName[] = ("myWindowClass");
更改應為:
#include <tchar.h> // in case it isn't included
//...
TCHAR g_szClassName[] = _T("myWindowClass");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.