What is wrong with this code?

Started by
7 comments, last by Tigra7 21 years, 8 months ago
Hello everyone, I have posted quite a few of these questions lately, but here goes anyway. The title pretty much explains the question. This is what happens when my program executes: Builds fine(no errors or warnings), then runs it but there is no window at all. When I press ctrl + alt + delete, my program shows up in the client area. The code is made up of a header file with everything in it to make the window and print text in the center of the screen. Then the source file just calls the function. It is below. The header file:
      
#pragma once

int InitClass(char WindowName[]);
LRESULT CALLBACK MsgHandler(HWND, UINT, WPARAM, LPARAM);

// Globals.....I know

RECT WindowRect = {100, 100, 400, 400};
HINSTANCE hInstance; 
int iCmdShow;

InitClass(char WindowName[])
{
	MSG msg;
	HWND hwnd;
	WNDCLASSEX wndclass;

	// Class Properties

	wndclass.cbSize			   = sizeof(wndclass);
	wndclass.hInstance         = hInstance;
	wndclass.lpfnWndProc       = MsgHandler;
	wndclass.lpszClassName     = "Reusable Window";
	wndclass.style             = CS_HREDRAW | CS_VREDRAW;
	
	// Visual properties

	wndclass.hbrBackground     = (HBRUSH) GetStockObject(WHITE_BRUSH);
	wndclass.hCursor           = LoadCursor(NULL, IDC_ARROW);
	wndclass.hIcon             = LoadIcon(NULL, IDI_APPLICATION);
	wndclass.hIconSm           = LoadIcon(NULL, IDI_APPLICATION);
	
	RegisterClassEx(&wndclass);

	hwnd=CreateWindow("Reusable Window", WindowName, WS_OVERLAPPEDWINDOW || WS_VISIBLE,
					   WindowRect.left, WindowRect.top, WindowRect.right, WindowRect.bottom,
					   NULL, NULL, hInstance, NULL);
	
	ShowWindow(hwnd, iCmdShow);
	UpdateWindow(hwnd);

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

	return msg.wParam;
}

LRESULT CALLBACK MsgHandler(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
	static TEXTMETRIC TextInfo;
	static int Length;
	PAINTSTRUCT PaintStruct;
	HDC hdc=NULL;
	int x, y;
	char Message[]="cmoWindow Test";

	switch(iMsg)
	{
	case WM_CREATE:
		hdc=GetWindowDC(hwnd);
		GetTextMetrics(hdc, &TextInfo);
		Length=strlen(Message);
		ReleaseDC(hwnd, hdc);
		break;
	case WM_SIZE:
		GetWindowRect(hwnd, &WindowRect);
		break;
	case WM_PAINT:
		hdc=BeginPaint(hwnd, &PaintStruct);
		SetTextColor(hdc, RGB(0, 0, 255));

		x=(WindowRect.right-WindowRect.left)/2;
		x=x-(Length*TextInfo.tmAveCharWidth)/2;

		y=(WindowRect.bottom-WindowRect.top)/2;
		y=y-(TextInfo.tmHeight)/2;
		TextOut(hdc, x, y, Message, Length);
		EndPaint(hwnd, &PaintStruct);
		break;
	case WM_DESTROY:
		PostQuitMessage(NULL);
		break;
	}
	
	return DefWindowProc (hwnd, iMsg, wParam, lParam);
}
  
And the source:
        
#include <windows.h>

#include "cmoWindow.h"

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, 
					PSTR szCmdLine, int iCmdShow)	
    {
		
		InitClass("Hello World");
		return 0;
	}
    
[edited by - Tigra7 on July 23, 2002 1:50:19 PM] [edited by - Tigra7 on July 23, 2002 1:51:24 PM]
Advertisement
pretty basic problem. you're program doesn't run long enough for you to see anything. InitClass runs and exits almost immediately. you never get to see the hello world text b/c the window closes to fase.

where you have:

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

it's only looping so long as there are messages to process, which is about zero seconds, b/c there aren't any messages to process most of the time. message = mouse click / key press /etc

replace that with:

while(1) {    if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { 		if(msg.message == WM_QUIT) // If the message wasnt to quit			break;            TranslateMessage(&msg);	// Find out what the message does            DispatchMessage(&msg);	// Execute the message    } else {        //this is where your program logic will go    }}     


eventually you'll want to move the above into it's own function called MainLoop() or something.

-me

[edited by - Palidine on July 23, 2002 1:55:56 PM]
Thanks,

But....(Gosh in programming isnt their always a but), the window still doesnt show at all. Same problem as before. I''d like to move into other aspects of game programming other than something as simple as this, but it just bugs me so much that i can''t walk away from it. URGHH!!

- Thanks again though

- Chris
quote:
"It seems as though everyone has a quote... "Bloat like a butterfly stink like a .... .... shoot" ... I tried"
The only problem that I can see is on your CreateWindow line. You''ve put WS_OVERLAPPEDWINDOW || WS_VISIBLE, which will evaluate to 0 or 1 cos you''ve used the comparitive-or rather than bitwise-or. Change || to | and it''ll be fine.....I think.
Top 3 List of issues with your code:

3) You''re using a logical or instead of a bitwise or in your CreateWindow window styles parameter.

2) The hInstance and iCmdShow globals are undefined when the code gets to InitClass. Either set the globals you defined to the values that are passed into WinMain (in WinMain) or (better yet) pass them to the InitClass function as parameters.

But the #1 problem in your code is....

1) Since you''re not setting every possible member of the window class to something you should clear out the structure before using it, ala:

memset(&wndclass, 0, sizeof(wndclass));

Then fill in the members you do use, then RegisterClassEx(&wndclass).


You also might want to reconsider implementing global functions in .h files (while its legal it will cause nothing but trouble in the long run).
Wow, I think I''m starting to really get into this.

I ran it after changing everything that I needed to. Thanks everyone. When the window popped up and it printed the colored text, I was amazed at what I had done. It is such a small thing to do, but after all of last nights work and it actually working, it had me hooked. I thought I would get into this for a hobby, or to make some money to save up for an Alienware, but oh so much more has gone through my head lately. I''m already thinking about college even though it is 6 years away. Thanks a lot!! I think this is what I want to do for the rest of my life! (Sorry if I induce any feelings that I''m ahead of myself. I actually think that I am ahead of myself too. But I''ll keep working.)

This bit was really corny, but I just wanted the thank you guys. This is the fifth post in these forums I have done on this stupid problem.


- Chris
i HIGHLY suggest not doing what Palidine suggested. its wrong, and only meant for games and other apps who want 100% of the cpu. a correct windows app will use the while loop you had originally.
Well since the reason of this application was to build a small text adventure with a few graphics, I guess you could call this a game. It works both ways. I don't see anything wrong with using 100% of the CPU. Is it bad to be doing this?

p.s.- What were you doing up that early? 3:00 am!? I just woke up!!

- Chris
quote:
"We are afraid of the unknown" - TV.... where else?




[edited by - Tigra7 on July 24, 2002 12:16:24 PM]
Actually, Palindine had it write when it comes to writing a real-time game. The way you originally had it, the wpplication would have blocked at the GetMessage() until a message came through the pipe.

100% of the CPU is fine, but if your writing something that does not need a super high framerate, put a Sleep(1); in the main loop. Since you won''t have anything in the app that blocks, you will still reliquish control to other apps.

This is extremely usefull for debugging on a snigle processor machine as well, since your app may prevent other debug utils from running well.


Stephen Manchester
Senior Technical Lead
Virtual Media Vision, Inc.
stephen@virtualmediavision.com
(310) 930-7349
Stephen ManchesterSenior Technical LeadVirtual Media Vision, Inc.stephen@virtualmediavision.com(310) 930-7349

This topic is closed to new replies.

Advertisement