Example Code issues
Not sure what's wrong with the following but I'm getting a few errors when trying to build. Maybe something unicode? Not sure how to address it since I'm just beginning to learn C++. Hopefully I can get some wisdom to get this working and avoid making the same mistake later.
Errors:
-------
Error 1 error C2440: '=' : cannot convert from 'char [14]' to 'LPCWSTR' c:\src\hello\hello.cpp 69 Hello
Error 2 error C2664: 'CreateWindowExW' : cannot convert parameter 2 from 'char [14]' to 'LPCWSTR' c:\src\hello\hello.cpp 102 Hello
Error 3 error C2664: 'DrawTextW' : cannot convert parameter 2 from 'char [28]' to 'LPCWSTR' c:\src\hello\hello.cpp 134 Hello
Code:
-----
#include "stdafx.h"
#define MAX_LOADSTRING 100
// Global Variables:
HINSTANCE hInst; // current instance
char szTitle[] = "Hello, World!"; // The title bar text
char szWindowClass[] = "Hello, World!"; // The title bar text
// Forward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
MSG msg;
// Initialize global strings
MyRegisterClass(hInstance);
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_APPLICATION);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance,
(LPCTSTR)IDI_APPLICATION);
return RegisterClassEx(&wcex);
}
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
hInst = hInstance; // Store instance handle in our global variable
hWnd = CreateWindow(
szWindowClass, // Name of the window class to use for this window
// registered in MyRegisterClass
szTitle, // Title of the application
WS_OVERLAPPEDWINDOW, // style that Windows should make our window with
// (this is the 'default' window style for windowed apps)
20, // Starting X of the window
20, // Starting Y of the window
640, // Width of the window
480, // Height of the window
NULL, // Handle of our parent window (Null, since we have none)
NULL, // Handle to our menu (Null, since we don't have one)
hInstance, // Instance of our running application
NULL); // Pointer to window creation data (we provide none)
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
char szHello[] = "Hello, you crazy world you!";
switch (message)
{
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code here...
RECT rt;
GetClientRect(hWnd, &rt);
DrawText(hdc, szHello, strlen(szHello), &rt,
DT_CENTER | DT_VCENTER | DT_SINGLELINE );
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
Quote:Original post by jflanglois from VS 2005 issues
You're project is set up to use Unicode (which is not your fault, it is the default). To change it to Multi-Byte, go to "Project->Properties->Configuration Properties->General->Character Set" and set that to "Use Multi-Byte Character Set".
Go to Project, 'Your project' Properties (at the bottom), then click on the 'Configuration Properties'. Under the first page, or the General section, go to the 'Character Set' option and set it to 'Not Set' then recompile.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement