-
Window Error
I've been trying to follow a book to get me started on windows programming with C++ and i have an error message when trying to make a blank window:
CXX0030: Error: expression cannot be evaluated
I've looked on msdn and the description there didn't help me fix my problem. Here is my source code, any help is appreciated
EDIT: forgot to mention this error occurs on hwnd
#include <windows.h>
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) {
static TCHAR szAppName[] = TEXT("Hello Word");
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.style = CS_HREDRAW || CS_VREDRAW;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;
if(!RegisterClass (&wndclass)) {
MessageBox (NULL, TEXT("This Program Requires Windows"), szAppName, MB_ICONERROR);
return 0;
}
hwnd = CreateWindow (szAppName, TEXT ("The Hello Program"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
}
-
Re: Window Error
Your program so far is fine except that it is incomplete.
You must define the WndProc function.
-
Re: Window Error
Ah ok cool thanks :). I've been trying to run it half way through to make sure that it's all working so far and didn't really realise everything has to be put in. Thanks for the help
-
Re: Window Error
I'm still getting this error even though i have what i thought was completed code to create a window. Here is the code i now have:
#include <windows.h>
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) {
static TCHAR szAppName[] = TEXT("Hello Word");
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.style = CS_HREDRAW || CS_VREDRAW;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;
if(!RegisterClass (&wndclass)) {
MessageBox (NULL, TEXT("This Program Requires Windows"), szAppName, MB_ICONERROR);
return 0;
}
hwnd = CreateWindow (szAppName, TEXT ("The Hello Program"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
ShowWindow(hwnd, nShowCmd);
UpdateWindow(hwnd);
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) {
HDC hdc;
PAINTSTRUCT ps;
RECT rect;
hdc = BeginPaint (hwnd, &ps);
GetClientRect (hwnd, &rect);
EndPaint(hwnd, &ps);
return DefWindowProc (hwnd, message, wparam, lparam);
}
-
Re: Window Error
this line:
Code:
wndclass.style = CS_HREDRAW || CS_VREDRAW;
Should be:
Code:
wndclass.style = CS_HREDRAW | CS_VREDRAW;
You have the logical OR you need the binary OR to combine style flags.
-
Re: Window Error
This line is missing
Code:
wndclass.lpfnWndProc = WndProc;
Also, your WndProc function is not proper.
It should have a switch statement to check the message parameter
Code:
switch (message)
{
case WM_PAINT:
hdc = BeginPaint (hwnd, &ps);
GetClientRect (hwnd, &rect);
EndPaint(hwnd, &ps);
break;
default:
return DefWindowProc (hwnd, message, wparam, lparam);
}
-
Re: Window Error
Well you don't need to switch the message in WndProc, you're application will still run, since DefWindowProc will handle all the messages in Window's default way, but if you don't handle WM_DESTROY with a call to PostQuitMessage in there you're process will keep running even after you press the close button.
Also you have no message pump.
in WinMain you need:
Code:
MSG msg;
...WNDCLASSEX and window creation here...
BOOL bError;
while ((bError = GetMessage(&msg, NULL, 0, 0)) != 0)
{
if (bError == -1)
{
// There was an error, handle it
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
GetMessage will get the the messages in you application's queue and then TranslateMessage and DispatchMessage will translate the message(if translating is needed) and send the message to your WndProc, respectively.
and once you close at the end of WinMain you should return the wParam of msg.
Code:
return static_cast<int>(msg.wParam);