Jump to content
  • Advertisement
Sign in to follow this  
Whelzorn

help with windows programming (register failed)

This topic is 5410 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am having a problem where I can compile my code, but running it results in the "window registration failed" message box. Here is my code:
#include <windows.h> //includes
#include <stdio.h>

#define IDC_Listbox		40001
#define IDC_Quit		40002
#define IDC_Apples		40003
#define IDC_Grapes		40004
#define IDC_Candy		40005
#define IDC_Checkout	40006
#define IDC_Title		40007

HWND		Listbox				= NULL;
HWND		Quit				= NULL;
HWND		Apples				= NULL;
HWND		Grapes				= NULL;
HWND		Candy				= NULL;
HWND		Checkout			= NULL;
HWND		Title				= NULL;
LPCTSTR		lpszApplicationName = "LengthWidth";
LPCSTR		lpszTitle			= "LengthWidthTitle";


LRESULT CALLBACK fnMessageProcessor(HWND, UINT, WPARAM, LPARAM); //callback functions
void vShowText(HWND		hChildHandle, char *szText);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    LPSTR lpCmdLine, int nCmdShow)
{
    WNDCLASSEX wc;
    HWND hwnd;
    MSG msg;

    wc.cbSize        = sizeof(wc);
    wc.style         = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc   = fnMessageProcessor;
    wc.cbClsExtra    = 0;
    wc.cbWndExtra    = 0;
    wc.hInstance     = hInstance;
    wc.hIcon         = LoadIcon( NULL, IDI_APPLICATION );
    wc.hCursor       = LoadCursor( NULL, IDC_ARROW );
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW);
    wc.lpszMenuName  = NULL;
    wc.lpszClassName = lpszApplicationName;
    wc.hIconSm       = LoadIcon( NULL, IDI_APPLICATION );

    if(!RegisterClassEx( &wc ) == 0 )
    {
        MessageBox(NULL, "Window Registration Failed", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

	int sizel;
	int sizew;
	sizew = GetSystemMetrics(SM_CXSCREEN);
	sizel = GetSystemMetrics(SM_CYSCREEN);

    hwnd = CreateWindow(
        lpszApplicationName,
        lpszTitle,
        WS_POPUP | WS_BORDER,
        sizew/2-200, sizel/2-150, 400, 300,
        NULL, NULL, hInstance, NULL);

    Listbox = CreateWindowEx(
        WS_EX_CLIENTEDGE,
        "LISTBOX",
        NULL,
        WS_CHILD | WS_VISIBLE | LBS_NOTIFY | WS_VSCROLL | WS_BORDER,
        25, 45, 350, 230,
        hwnd,(HMENU)IDC_Listbox,hInstance,NULL);

	Quit = CreateWindow(
        "BUTTON",
        "X",
        WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
        380, 0, 20, 20,
        hwnd,(HMENU)IDC_Quit,hInstance,NULL);

	Apples = CreateWindow(
        "BUTTON",
        "Apples",
        WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
        25, 275, 100, 20,
        hwnd,(HMENU)IDC_Apples,hInstance,NULL);

	Grapes = CreateWindow(
        "BUTTON",
        "Grapes",
        WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
        145, 275, 100, 20,
        hwnd,(HMENU)IDC_Grapes,hInstance,NULL);

	Candy = CreateWindow(
        "BUTTON",
        "Candy",
        WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
        275, 275, 100, 20,
        hwnd,(HMENU)IDC_Candy,hInstance,NULL);

	Checkout = CreateWindow(
        "BUTTON",
        "Checkout",
        WS_CHILD | WS_VISIBLE | WS_BORDER | BS_PUSHBUTTON,
        140, 25, 100, 20,
        hwnd,(HMENU)IDC_Checkout,hInstance,NULL);

	Title = CreateWindow(
        "STATIC",
        "Ch9Proj1.exe",
        WS_CHILD | WS_VISIBLE | WS_BORDER,
        0, 0, 380, 20,
        hwnd,(HMENU)IDC_Title,hInstance,NULL);

    //if(hwnd == NULL)
    //{
    //    MessageBox(NULL, "Window Creation Failed!", "Error!",
    //        MB_ICONEXCLAMATION | MB_OK);
    //   return 0;
    //}

    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);

    while( TRUE )
    {
		if (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE))
		{
			if (!GetMessage(&msg, NULL, 0, 0))
				break;
        TranslateMessage(&msg);
        DispatchMessage(&msg);
		}
    }
    return(msg.wParam);
}

LRESULT CALLBACK fnMessageProcessor(HWND hwnd, UINT imsg, WPARAM wParam, LPARAM lParam)
{

	char szText[256];
	static count = 0;
	int AppleQ = 0;
	int GrapeQ = 0;
	int CandyQ = 0;

    switch(imsg) // my message handler
    {
        case WM_CLOSE: //so the program is closeable
            DestroyWindow(hwnd);
			break;
        case WM_DESTROY:
            PostQuitMessage(0);
			break;
		case WM_COMMAND:
			switch(LOWORD(wParam))
			{
			case IDC_Quit:
				exit(0);
			
			case IDC_Apples:
				AppleQ++;
				sprintf(szText,"added apple. Now %d",AppleQ,"apples");
				//vShowText(IDC_Listbox,szText);
				break;		

			case IDC_Grapes:
				GrapeQ++;
				break;

			case IDC_Candy:
				CandyQ++;
				break;

			case IDC_Checkout:
				break;
			}
			break;
        default:
            return DefWindowProc(hwnd, imsg, wParam, lParam); //so the program doesnt close itself
    }
    return(0L);
}

void vShowText(HWND		hChildHandle, char *szText)
{
		int Line;
		SendMessage(hChildHandle,LB_ADDSTRING,0,(LPARAM) szText);
		Line = SendMessage(hChildHandle,LB_GETCOUNT,0,0);
		SendMessage(hChildHandle,LB_SETCURSEL,Line-1,0);
		SendMessage(hChildHandle,LB_SETCURSEL,-1,0);
}

any ideas? thanks in advance

Share this post


Link to post
Share on other sites
Advertisement
Quote:
if(!RegisterClassEx( &wc ) == 0 )


take a close look at the start and the end of that line!

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
lol wow... thanks alot, that one had me stumped for some reason... it works now ;)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
aah, sorry, but I have an issue on top of that... now a compile error occurs...

c:\Documents and Settings\cplus\My Documents\Visual Studio Projects\woodman4\window.cpp(163): error C2664: 'vShowText' : cannot convert parameter 1 from 'int' to 'HWND'

that is my origional culprit... any help with that?

Share this post


Link to post
Share on other sites
sorry, the AP is me...

note on above post: this only occurs if I change
//vShowText(IDC_Listbox,szText);
to vShowText(IDC_Listbox,szText);
(remove commenting)

however I need this line in operation

Share this post


Link to post
Share on other sites
You are using the ID of the listbox. The SendMessage function expects an HWND (the handle of the listbox). You should do:

vShowText(Listbox,szText); // <- remove the IDC_ part

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!