// include the basic windows header file
#include <windows.h>
#include <windowsx.h>
// the WindowProc function prototype LRESULT CALLBACK WindowProc(HWND hWnd,
UINT message,
WPARAM wParam,
LPARAM lParam);
// the entry point for any Windows program int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow) {
// the handle for the window, filled by a function
HWND hWnd;
// this struct holds information for the window class
WNDCLASSEX wc;
// clear out the window class for use
ZeroMemory(&wc, sizeof(WNDCLASSEX));
// fill in the struct with the needed information
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
wc.lpszClassName = L"WindowClass1";
// register the window class
RegisterClassEx(&wc);
// create the window and use the result as the handle
hWnd = CreateWindowEx(NULL,
L"WindowClass1", // name of the window class
L"Our First Windowed Program", // title of the window
WS_OVERLAPPEDWINDOW, // window style
300, // x-position of the window
300, // y-position of the window
500, // width of the window
400, // height of the window
NULL, // we have no parent window, NULL
NULL, // we aren't using menus, NULL
hInstance, // application handle
NULL); // used with multiple windows, NULL
// display the window on the screen
ShowWindow(hWnd, nCmdShow);
// enter the main loop:
// this struct holds Windows event messages
MSG msg;
// wait for the next message in the queue, store the result in 'msg'
while(GetMessage(&msg, NULL, 0, 0))
{
// translate keystroke messages into the right format
TranslateMessage(&msg);
// send the message to the WindowProc function
DispatchMessage(&msg);
}
// return this part of the WM_QUIT message to Windows
return msg.wParam; }
// this is the main message handler for the program LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
// sort through and find what code to run for the message given
switch(message)
{
// this message is read when the window is closed
case WM_DESTROY:
{
// close the application entirely
PostQuitMessage(0);
return 0;
} break;
}
// Handle any messages the switch statement didn't
return DefWindowProc (hWnd, message, wParam, lParam); }
=============== 我使用CodeBlock,这段代码来自Direct X教程。
我收到以下错误:
||In function 'int WinMain(HINSTANCE, HINSTANCE, LPSTR, int)':|
error: cannot convert 'const wchar_t [13]' to 'LPCSTR {aka const char*}' in assignment|
|49|warning: converting to non-pointer type 'DWORD {aka long unsigned int}' from NULL [-Wconversion-null]|
|49|error: cannot convert 'const wchar_t*' to 'LPCSTR {aka const char*}' for argument '2' to 'HWND__* CreateWindowExA(DWORD, LPCSTR, LPCSTR, DWORD, int, int, int, int, HWND, HMENU, HINSTANCE, LPVOID)'|
||=== Build finished: 2 errors, 1 warnings ===|
答案 0 :(得分:25)
您的项目没有定义UNICODE
预处理程序符号,因此Windows API函数指向期望char *
而不是wchar_t *
的字符串。改变
L"WindowClass1"
到
"WindowClass1"
对剩余的字符串文字执行相同的操作。或者,将它们更改为_T("WindowClass1")
,这将根据定义的UNICODE
符号扩展为正确的字符串文字类型。
我的建议是转到项目属性并将Character Set
设置更改为Unicode
,然后明确使用所有Windows API函数的宽字符版本。例如,代替CreateWindow
,请致电CreateWindowW
。
编辑:
我建议的项目设置仅适用于Visual Studio,不知道如何在Code :: Blocks中执行此操作。
答案 1 :(得分:8)
1)如果要使用UNICODE进行编译,请更改选项。如果您是从IDE编译,请设置以下属性 配置属性 - &gt;一般 - &gt;项目默认值 - &gt;字符集 - &gt;使用Unicode字符集。
如果从命令行编译,请使用选项/ DUNICODE / D_UNICODE
如果您不想使用UNICODE进行编译,请按照步骤2&amp; 3下面。在字符集中,不要选择UNICODE。
2)之前
#include <windows.h>
添加
#include <tchar.h>
3)改变
wc.lpszClassName = L"WindowClass1";
到
wc.lpszClassName = _T("WindowClass1");
如果你想用UNICODE进行编译,你可以通过做#1来完成,但最好做到所有3。
如果您想在没有UNICODE的情况下进行编译,请执行#2&amp; #3 - 不要做#1。
答案 2 :(得分:1)
我在单个源代码文件中使用它,因为我不想更改当前的项目配置
#ifndef UNICODE
#define UNICODE
#define UNICODE_WAS_UNDEFINED
#endif
#include <Windows.h>
#ifdef UNICODE_WAS_UNDEFINED
#undef UNICODE
#endif