So I finally decided to take on some Windows Programming, and everything is going fine, except this:

Edit: using this tutorial http://bobobobo.wordpress.com/2008/01/31/how-to-create-a-basic-window-in-c/

#include <windows.h>

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
	WNDCLASS myWindow;

	myWindow.cbClsExtra = 0;
	myWindow.cbWndExtra = 0;
	myWindow.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
	myWindow.hCursor = LoadCursor(NULL, IDC_ARROW);
	myWindow.hIcon = NULL;
	myWindow.hInstance = hInstance;
	myWindow.lpfnWndProc = WndProc;
	myWindow.lpszClassName = TEXT("Levi");
	myWindow.lpszMenuName = 0;
	myWindow.style = CS_VREDRAW | CS_HREDRAW;

	RegisterClass(&myWindow);

	HWND myHandle = CreateWindow(TEXT("Levi"), TEXT("My Window"), WS_OVERLAPPEDWINDOW, 10, 10, 200, 200, NULL, NULL, hInstance, NULL);


	

}

error C2065: 'WndProc' : undeclared identifier

Every tutorial Ive found uses WndProc but its telling me other wise.

Any Ideas? Thanks

Dani AI

Generated

The compiler error means the window procedure name you put into myWindow.lpfnWndProc doesn't exist where the compiler can see it. As noted, you need a WndProc; was close but the posted prototype had typos and a different name. Fixes: either implement WndProc before WinMain, or put a correct forward declaration above WinMain.

A minimal correct WndProc (use this exact name if you assign lpfnWndProc = WndProc) looks like:

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch (msg) {
    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;
    }
    return DefWindowProc(hwnd, msg, wParam, lParam);
}

You also need the standard message loop and to show/update the window after CreateWindow(...). Typical code to put inside WinMain after CreateWindow:

MSG msg;
ShowWindow(myHandle, nShowCmd);
UpdateWindow(myHandle);
while (GetMessage(&msg, NULL, 0, 0)) {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
}
return (int)msg.wParam;

Troubleshooting notes: the function name must match exactly (case-sensitive in C++). CALLBACK matters because it sets the calling convention; mismatching signature will either give compile/link errors or runtime trouble. If you prefer, use WNDCLASSEX + RegisterClassEx (remember to set cbSize) and, on modern MSVC projects, be aware of UNICODE settings — that affects string literals and WinMain vs wWinMain.

So I finally decided to take on some Windows Programming, and everything is going fine, except this:

Edit: using this tutorial http://bobobobo.wordpress.com/2008/01/31/how-to-create-a-basic-window-in-c/

#include <windows.h>

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
	WNDCLASS myWindow;

	myWindow.cbClsExtra = 0;
	myWindow.cbWndExtra = 0;
	myWindow.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
	myWindow.hCursor = LoadCursor(NULL, IDC_ARROW);
	myWindow.hIcon = NULL;
	myWindow.hInstance = hInstance;
	myWindow.lpfnWndProc = WndProc;
	myWindow.lpszClassName = TEXT("Levi");
	myWindow.lpszMenuName = 0;
	myWindow.style = CS_VREDRAW | CS_HREDRAW;

	RegisterClass(&myWindow);

	HWND myHandle = CreateWindow(TEXT("Levi"), TEXT("My Window"), WS_OVERLAPPEDWINDOW, 10, 10, 200, 200, NULL, NULL, hInstance, NULL);


	

}

error C2065: 'WndProc' : undeclared identifier

Every tutorial Ive found uses WndProc but its telling me other wise.

Any Ideas? Thanks

You forgot the window procedure event handler

This may help....
cprogramming.com (Helps with c++ too)

declare WinProc function...
LRESULT CALLBACK WinProc(HWND hwnd.UINT msg,WPARAM wparam,LPARAM lparam);

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.