Can't seem to get my program to use HWND!

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi Guys,
I have been using MSVC 2008 and Win32 for about 2-3 years and I have run into one of the weirdest problems.
Now, I should probably say: I usually code in C++, but this time I decided to code in just plain old C.
The problem is that the compiler doesnt seem to like the fact that I am declaring a handle to window like this:

<div style="color:Black;background-color:White; <pre>
HWND hWnd = CreateWindowEx(WS_EX_OVERLAPPEDWINDOW | WS_EX_APPWINDOW, (LPCWSTR)(MainWindowClassAtom),
AppTitle, WS_OVERLAPPEDWINDOW, 0, 0, g_width, g_height, NULL, NULL, hInstance, 0);
[/code]

When I try to compile, I get the error:
C2275: HWND: Illegal use of type as expression

I have looked up how HWND is defined and tried to boil down my variable as far as void* and still nothing.
Please help!


P.S. Here is the complete code file:

<div style="color:Black;background-color:White; <pre>
#include <windows.h>
#include <tchar.h>

ATOM MainWindowClassAtom = 0;
<span style="color:Blue; int g_width = 640, g_height = 480;
LPCWSTR AppTitle = _T(<span style="color:#A31515; "MyApp in C");

LRESULT CALLBACK
MainWindowProc(HWND hWnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
<span style="color:Blue; switch(iMsg)
{
<span style="color:Blue; case WM_CREATE:
{
<span style="color:Blue; return 0;
}

<span style="color:Blue; case WM_CLOSE:
{
DestroyWindow(hWnd);
<span style="color:Blue; return 0;
}

<span style="color:Blue; case WM_DESTROY:
{
PostQuitMessage(0);
<span style="color:Blue; return 0;
}

<span style="color:Blue; case WM_PAINT:
{
PAINTSTRUCT *ps = malloc(<span style="color:Blue; sizeof(PAINTSTRUCT));
BeginPaint(hWnd, ps);
EndPaint(hWnd, ps);
<span style="color:Blue; return 0;
}
}

<span style="color:Blue; return DefWindowProc(hWnd, iMsg, wParam, lParam);
}

<span style="color:Blue; int WINAPI
WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, <span style="color:Blue; int nShowCmd)
{
WNDCLASSEX *wnd = malloc(<span style="color:Blue; sizeof(WNDCLASSEX));

wnd->cbSize = <span style="color:Blue; sizeof(WNDCLASSEX);
wnd->cbClsExtra = 0;
wnd->cbWndExtra = 0;
wnd->style = CS_HREDRAW | CS_VREDRAW;
wnd->lpfnWndProc = MainWindowProc;
wnd->lpszMenuName = NULL;
wnd->lpszClassName = _T(<span style="color:#A31515; "MainWindowClass");
wnd->hInstance = hInstance;
wnd->hbrBackground = (HBRUSH)(COLOR_WINDOW +1);
wnd->hCursor = LoadCursor(NULL, IDC_ARROW);
wnd->hIcon = NULL;
wnd->hIconSm = NULL;
MainWindowClassAtom = RegisterClassEx(wnd);

<span style="color:Blue; if(MainWindowClassAtom == 0)
{
MessageBox(NULL, _T(<span style="color:#A31515; "UNABLE TO REGISTER MAIN CLASS!"), _T(<span style="color:#A31515; "FATAL ERROR!"), MB_OK | MB_ICONERROR);
<span style="color:Blue; return -1;
}

<span style="color:Blue; void hwnd = CreateWindowEx(WS_EX_OVERLAPPEDWINDOW | WS_EX_APPWINDOW, (LPCWSTR)(MainWindowClassAtom),
AppTitle, WS_OVERLAPPEDWINDOW, 0, 0, g_width, g_height, NULL, NULL, hInstance, 0);

<span style="color:Blue; if(hWnd == 0)
{
MessageBox(NULL, _T(<span style="color:#A31515; "UNABLE TO CREATE WINDOW!"), _T(<span style="color:#A31515; "FATAL ERROR!"), MB_OK | MB_ICONERROR);
<span style="color:Blue; return -1;
}

ShowWindow(hWnd, SW_SHOW);
UpdateWindow(hWnd);

MSG *msg = malloc(<span style="color:Blue; sizeof(MSG));

<span style="color:Blue; while(msg->message != WM_QUIT)
{
<span style="color:Blue; if((PeekMessage(msg, NULL, NULL, NULL, PM_NOREMOVE)) != 0)
{
<span style="color:Blue; if((GetMessage(msg, NULL, NULL, NULL)) == 0)
{
<span style="color:Blue; break;
}

TranslateMessage(msg);
DispatchMessage(msg);
}
}

<span style="color:Blue; return msg->wParam;
}
[/code]
<br/>
<br/>


View the full article
 
Back
Top