Advertisement

Windows programming problem...

Started by March 02, 2002 08:02 AM
13 comments, last by shakazed 22 years, 6 months ago
This has happened to me mny times but it seems to be different problems each time. This code shoudl show an ordinary window with some specified attributes. Can someone figure out why the application runs but no window is shown.
  
#include<windows.h>
#include"dx.h"
#include"macros.h"

//Globaler

const char *WINDOW_CLASS_NAME = "Window Class Name";
const char *WINDOW_NAME = "My Application";
const int WINDOW_WIDTH = 640;
const int WINDOW_HEIGHT = 480;
const int WINDOW_X = 100;
const int WINDOW_Y = 100;

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

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
	WNDCLASSEX wc;
	
	wc.cbClsExtra = 0; //0

	wc.cbSize = sizeof(WNDCLASSEX);
	wc.cbWndExtra = 0; //0

	wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH); //Bakgrundsfärg

	wc.hCursor = LoadCursor(NULL, IDC_ARROW);//muspekare

	wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); //Sotra ikonen

	wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION); //Lilla ikonen

	wc.hInstance = hInstance; //Ge klassen ett handtag till denna instans av programmet

	wc.lpfnWndProc = WndProc; //Pekare till meddelande hanteraren

	wc.lpszClassName = WINDOW_CLASS_NAME;
	wc.lpszMenuName = NULL; //meny

	wc.style = CS_HREDRAW|CS_VREDRAW;

	if(!RegisterClassEx(&wc)) //Registrera klassen

		STANDARD_ERROR;

	extern HWND hWnd=CreateWindowEx(NULL,WINDOW_CLASS_NAME, WINDOW_NAME, WS_POPUP|WS_VISIBLE,WINDOW_X,
		WINDOW_Y,WINDOW_WIDTH,WINDOW_HEIGHT,NULL,NULL,hInstance,NULL);
	
	if(hWnd==NULL)
	{
		STANDARD_ERROR;
		return 1;
	}

	while(1)
	{
	}

	return 0;
}


  
sHaKaZeD
Where''s your message loop?

Kippesoep
Advertisement
I''m not sure if this is the main problem but you aren''t proccessing any messages in the infinite while loop.
The Department of Next Life - Get your Next-Life Insurance here!
first: the message-processing-function:

  LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM w,LPARAM l){ switch(msg) {  case WM_DESTROY: // if we got the destroy-message of the window, we want to quit the whole program..   PostQuitMessage(0);   break; } //after EVERY processed or unprocessed message, windows wants to process the message by its normal window-message-handling-function, namely DefWindowProc //the normal returnvalue of the normal window-processing-function is our returnvalue.. return DefWindowProc(hwnd,msg,w,l);}  


then, the mainfunction..

  the WNDCLASS looks ok, the createwindow, too.. so the window-message-loop..MSG msg;// GetMessage waits for a message from the system, and returns true if it got one, and false if it got the PostQuitMessage-message to end the app..while(GetMessage(&msg,0,0,0)){ //just for some key-mappings if you pressed a key, WM_CHAR,WM_DEADCHAR,WM_KEYDOWN,WM_etc.. has to be generated.. possibly other stuff there in converted, but thats what i know.. TranslateMessage(&msg); //actually sends the message to your WndProc, and windows looks then how you react on it with the WndProc returnvalue (thats why for all not own-handled messages we have to return the DefWindowProc-value.. DispatchMessage(&msg);}  



hope it helped a little

we wanna play, not watch the pictures

If that's not the help you're after then you're going to have to explain the problem better than what you have. - joanusdmentia

My Page davepermen.net | My Music on Bandcamp and on Soundcloud

Hm, seems the message loop isn´t the problem. The program should still create the window.

sHaKaZeD
Can we see your latest version please?
Advertisement
Okey, here it is.

  #include<windows.h>#include<windowsx.h>#include"dx.h"#include"macros.h"//Globalerconst char *WINDOW_CLASS_NAME = "Window Class Name";const char *WINDOW_NAME = "My Application";const int WINDOW_WIDTH = 640;const int WINDOW_HEIGHT = 480;const int WINDOW_X = 100;const int WINDOW_Y = 100;LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam){	switch(msg)	{	case WM_DESTROY:		{			PostQuitMessage(0);			return 0;		}break;	default:		DefWindowProc(hWnd, msg,wParam,lParam);	}	return 0;}int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, 				   LPSTR lpCmdLine, int nCmdShow){	WNDCLASSEX wc;		wc.cbClsExtra = 0; //0	wc.cbSize = sizeof(WNDCLASSEX);	wc.cbWndExtra = 0; //0	wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH); //Bakgrundsfärg	wc.hCursor = LoadCursor(NULL, IDC_ARROW);//muspekare	wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); //Sotra ikonen	wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION); //Lilla ikonen	wc.hInstance = hInstance; //Ge klassen ett handtag till denna instans av programmet	wc.lpfnWndProc = WndProc; //Pekare till meddelande hanteraren	wc.lpszClassName = WINDOW_CLASS_NAME;	wc.lpszMenuName = NULL; //meny	wc.style = CS_HREDRAW|CS_VREDRAW;	if(!RegisterClassEx(&wc)) //Registrera klassen		STANDARD_ERROR;	extern HWND hWnd=CreateWindowEx(NULL,WINDOW_CLASS_NAME, WINDOW_NAME, WS_POPUP|WS_VISIBLE,WINDOW_X,		WINDOW_Y,WINDOW_WIDTH,WINDOW_HEIGHT,NULL,NULL,hInstance,NULL);		MSG msg;// GetMessage waits for a message from the system, and returns true if it got one, and false if it got the PostQuitMessage-message to end the app..	while(GetMessage(&msg,0,0,0)) //just for some key-mappings if you pressed a key, WM_CHAR,WM_DEADCHAR,WM_KEYDOWN,WM_etc.. has to be generated.. possibly other stuff there in converted, but thats what i know.. 	{		TranslateMessage(&msg); //actually sends the message to your WndProc, and windows looks then how you react on it with the WndProc returnvalue (thats why for all not own-handled messages we have to return the DefWindowProc-value.. 		DispatchMessage(&msg);	}	return 0;}  


sHaKaZeD
Actually your problem is simple your not calling ShowWindow(hWnd, SW_SHOW)

"Computer games don''t affect kids; I mean if Pac-Man affected us as kids, we''d all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music."
- Kristian Wilson, Nintendo, Inc, 1989
"Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
Try adding

ShowWindow(hWnd,SW_SHOW);

after the call to CreateWindowEx

"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
The ShowWindow function isn´t necessary if you´ve got the WS_VISIBLE flag activated. Tried it though but it didn´t help...hmmm....

sHaKaZeD

This topic is closed to new replies.

Advertisement