• Advertisement
Sign in to follow this  

Simple Code = huge prob

This topic is 4176 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 have tryed to make a reguler exemple window. however i keep falling for my own error that sais, that if the window doesnt equal to window type defination dont register it, and also give error for it... i have no idea what`s wrong here, i am looking at this code for days and i am at a dead end...

//Mainwindow.cpp
//Main window defination


#define WIN32_LEAN_AND_MEAN
#define WINDOW_CLASS_NAME "WINCLASS1"
#include <windows.h>
#include <windowsx.h>
#include <stdio.h>
#include <math.h>



LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{

	switch(msg)
	{
	case WM_CREATE:
		MessageBox(NULL, "Error Text",
		"Header Text",
		MB_OK);
		break;
	case VK_ESCAPE:
	case WM_DESTROY:
	case WM_QUIT:
		PostQuitMessage(0);
		break;

	default:break;


	}

return DefWindowProc(hwnd, msg, wparam, lparam);
}



int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hprevinstance, LPSTR lpcmdline, int ncmdshow)
{
	WNDCLASSEX illawind;
	HWND hwnd;
	MSG msg;


illawind.lpfnWndProc = WindowProc;
illawind.cbClsExtra = sizeof(WNDCLASSEX);
illawind.hIcon = LoadIcon(NULL, IDI_WINLOGO);
illawind.hInstance = hinstance;
illawind.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
illawind.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
illawind.hCursor = LoadCursor(NULL, IDC_ARROW);
illawind.cbClsExtra = 0;
illawind.cbWndExtra = 0;
illawind.lpszMenuName = NULL;
illawind.lpszClassName = WINDOW_CLASS_NAME;
illawind.style = CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW;

if(!RegisterClassEx(&illawind))
{
	MessageBox(NULL,
		       "Unable To register window",
			   "Illa Error #1",
			   MB_OK);
}
return (0);


if(!(hwnd = CreateWindowEx(NULL,
          "Ground Zero",
		  "Ground Zero",
		  WS_OVERLAPPEDWINDOW | WS_VISIBLE,
		  0, 0,
		  400, 400,
		  NULL,
		  NULL,
		  hinstance,
		  NULL)))
		  return (0);


while(GetMessage(&msg, NULL, 0, 0))
{
	TranslateMessage(&msg);
	DispatchMessage(&msg);
}

return (msg.wParam);


}


Share this post


Link to post
Share on other sites
Advertisement
You can use source tags to make your code a little more viewable:

//Mainwindow.cpp
//Main window defination


#define WIN32_LEAN_AND_MEAN
#define WINDOW_CLASS_NAME "WINCLASS1"
#include <windows.h>
#include <windowsx.h>
#include <stdio.h>
#include <math.h>



LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{

switch(msg)
{
case WM_CREATE:
MessageBox(NULL, "Error Text",
"Header Text",
MB_OK);
break;
case VK_ESCAPE:
case WM_DESTROY:
case WM_QUIT:
PostQuitMessage(0);
break;

default:break;


}

return DefWindowProc(hwnd, msg, wparam, lparam);
}



int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hprevinstance, LPSTR lpcmdline, int ncmdshow)
{
WNDCLASSEX illawind;
HWND hwnd;
MSG msg;


illawind.lpfnWndProc = WindowProc;
illawind.cbClsExtra = sizeof(WNDCLASSEX);
illawind.hIcon = LoadIcon(NULL, IDI_WINLOGO);
illawind.hInstance = hinstance;
illawind.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
illawind.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
illawind.hCursor = LoadCursor(NULL, IDC_ARROW);
illawind.cbClsExtra = 0;
illawind.cbWndExtra = 0;
illawind.lpszMenuName = NULL;
illawind.lpszClassName = WINDOW_CLASS_NAME;
illawind.style = CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW;

if(!RegisterClassEx(&illawind))
{
MessageBox(NULL,
"Unable To register window",
"Illa Error #1",
MB_OK);
}
return (0);


if(!(hwnd = CreateWindowEx(NULL,
"Ground Zero",
"Ground Zero",
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
0, 0,
400, 400,
NULL,
NULL,
hinstance,
NULL)))
return (0);


while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}

return (msg.wParam);

Share this post


Link to post
Share on other sites

int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hprevinstance, LPSTR lpcmdline, int ncmdshow)
{
WNDCLASSEX illawind;
HWND hwnd;
MSG msg;


illawind.lpfnWndProc = WindowProc;
illawind.cbClsExtra = sizeof(WNDCLASSEX); // This line is your problem
illawind.hIcon = LoadIcon(NULL, IDI_WINLOGO);
illawind.hInstance = hinstance;
illawind.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
illawind.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
illawind.hCursor = LoadCursor(NULL, IDC_ARROW);
illawind.cbClsExtra = 0;
illawind.cbWndExtra = 0;
illawind.lpszMenuName = NULL;
illawind.lpszClassName = WINDOW_CLASS_NAME;
illawind.style = CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW;




See how you have 2 cbClsExtra? You're overwriting that parameter, and not defining the size of the structure.

illawind.cbClsExtra = sizeof(WNDCLASSEX);

should read

illawind.cbSize = sizeof(WNDCLASSEX);

Share this post


Link to post
Share on other sites
wow thank you, now i get you :)

however it seems to be that i have another problem registering the window perheps?

becuse now i dont have the failed to register window error, yet it doesnt work nothing happen

Share this post


Link to post
Share on other sites

if(!RegisterClassEx(&illawind))
{
MessageBox(NULL,
"Unable To register window",
"Illa Error #1",
MB_OK);
}
return (0);

You just return after registering it.

Share this post


Link to post
Share on other sites
Do what reana1 stated, but your window is still going to fail. You need to associate the WNDCLASSEX structure with your window via the lpszClassName.

Your WNDCLASSEX is labeled WINCLASS1 whereas your Window is labeled "Ground Zero". Make them the same.

*EDIT*

Sorry, forget that last part that I just deleted. I had some debug code in my test program causing that.

Share this post


Link to post
Share on other sites
ok now its working :) thank you guys
however i wish to know , why when i remove the fail to register window error message , it started working, i mean the return still reminds (0);
so how come when i remove the message part its working ?

Share this post


Link to post
Share on other sites
Quote:
Original post by Nikita21
ok now its working :) thank you guys
however i wish to know , why when i remove the fail to register window error message , it started working, i mean the return still reminds (0);
so how come when i remove the message part its working ?


You shouldn't remove that message. The main point reana1 was trying to make was that you were always calling a return right after that function. Thus, immediately finishing your program. The return should go inside the scope of the if test, not outside, so you'd only return when the function failed.

Because if for some reason the program crashes, and you don't return, you're going to most likely get into serious trouble with a lot of other errors appearing.


// Incorrect: I don't care what happens, I'm returning regardless
if(!RegisterClassEx(&illawind))
{
MessageBox(NULL,
"Unable To register window",
"Illa Error #1",
MB_OK);
}
return (0);
//Better: I'll only return if registering fails
if(!RegisterClassEx(&illawind))
{
MessageBox(NULL,
"Unable To register window",
"Illa Error #1",
MB_OK);
return (0);
}

Share this post


Link to post
Share on other sites
silly me, i dont even notice what am writing,

by putting the return after the

{
... // fail program error
}
return 0;


the program always self terminated

however when i put it in the the
{
...// fail program error
return 0;
}

then the program only get killed when the window fail to register and after giving an error, you are so right you guys, i havent noticed it when wrote it :)

Share this post


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

  • Advertisement