Results 1 to 7 of 7

Thread: Does not execute it for which reason?

  1. #1

    Thread Starter
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827

    Post Does not execute it for which reason?

    I have written the following code to create a simple window. But the problem is that whenever I click execute button. It does not execute(run) it. It does nothing.

    Please Note:

    I am using Visual C++ 6 Enterprise, and every other program is executed except this one. I also copied and pasted the code from www.winpro.org to create a simple window and it works fine. Please find any error if you can.

    Code:
    #include <windows.h>
    static char myclass[] = "t";
    LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
    {
    	switch(msg)
    	{
    	case WM_CLOSE:
    		DestroyWindow(hwnd);
    		break;
    	case WM_DESTROY:
    		PostQuitMessage(0);
    		break;
    	default:
    		return WndProc(hwnd, msg, wparam, lparam);
    	}
    	return 0;
    }
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
    {
    	WNDCLASSEX wc;
    	MSG message;
    	HWND hwnd;
    
    	wc.cbClsExtra = 0;
    	wc.cbWndExtra = 0;
    	wc.hbrBackground = (HBRUSH) GetStockObject(GRAY_BRUSH);
    	wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    	wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    	wc.hInstance = hInstance;
    	wc.lpfnWndProc = WndProc;
    	wc.lpszMenuName = myclass;
    	wc.lpszMenuName = NULL;
    	wc.style = NULL;
       
    
    	if(!RegisterClassEx(&wc))
    	{
    		return 0;
    	}
    
    	hwnd = CreateWindowEx(
    			WS_EX_CLIENTEDGE,
    			myclass,
    			"This is win",
    			WS_OVERLAPPEDWINDOW,
    			0,
    			0,
    			100,
    			100,
    			NULL,
    			NULL,
    			hInstance,
    			NULL);
    	if(hwnd == NULL)
    	{
    		return 0;
    	}
    
    	ShowWindow(hwnd, nShowCmd);
    	UpdateWindow(hwnd);
    
    	while(GetMessage(&message, NULL, 0,0))
    	{
    		TranslateMessage(&message);
    		DispatchMessage(&message);
    	}
    
    	return message.wParam;
    }
    Thanks!
    Last edited by abdul; May 4th, 2001 at 07:53 PM.
    Baaaaaaaaah

  2. #2
    ChimpFace9000
    Guest
    Before anything, please edit your post and put it in the tags.

  3. #3
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    You made a bit of a typo in your windowclass with the classname and you also missed out some properties of the class, which was messing up the RegisterClassEx() call. You also had an infinite recursion in your WndProc() function, which I've commented out.... here's some revised code. It still doesn't work for me, there is something wrong with the CreateWindowEx() call, but if you check for errors with GetLastError it says its successful. Anyway this is better but still not fixed:
    Code:
    #include <windows.h> 
    static char myclass[] = "t"; 
    LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) 
    { 
    	switch(msg) 
    	{ 
    	case WM_CLOSE: 
    		DestroyWindow(hwnd); 
    		break; 
    	case WM_DESTROY: 
    		PostQuitMessage(0); 
    		break; 
    	//default: 
    		//return WndProc(hwnd, msg, wparam, lparam); 
    	} 
    	return 0; 
    } 
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) 
    { 
    	WNDCLASSEX wc; 
    	MSG message; 
    	HWND hwnd; 
    	
    	wc.cbClsExtra = 0; 
    	wc.cbWndExtra = 0; 
    	wc.hbrBackground = (HBRUSH) GetStockObject(GRAY_BRUSH); 
    	wc.hCursor = LoadCursor(NULL, IDC_ARROW); 
    	wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); 
    	wc.hInstance = hInstance; 
    	wc.lpfnWndProc = WndProc; 
    	wc.lpszClassName = myclass; 
    	wc.lpszMenuName = NULL; 
    	wc.style = NULL; 
    	wc.cbSize = sizeof(wc);
    	wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION); 
    	
    
    	if(!RegisterClassEx(&wc)) 
    	{ 
    		return 0; 
    	} 
    	
    	hwnd = CreateWindowEx( 
    		WS_EX_CLIENTEDGE, 
    		myclass, 
    		"This is win", 
    		WS_OVERLAPPEDWINDOW, 
    		1, 
    		1, 
    		100, 
    		100, 
    		NULL, 
    		NULL, 
    		hInstance, 
    		NULL); 
    	if(hwnd == NULL) 
    	{ 
    		DWORD err = GetLastError();		
    		return 0; 
    	} 
    	
    	ShowWindow(hwnd, nShowCmd); 
    	UpdateWindow(hwnd); 
    	
    	while(GetMessage(&message, NULL, 0,0)) 
    	{ 
    		TranslateMessage(&message); 
    		DispatchMessage(&message); 
    	} 
    	
    	return message.wParam; 
    }
    Harry.

    "From one thing, know ten thousand things."

  4. #4

    Thread Starter
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827

    Here it is

    Well, it gives an error while creatinging the hwnd of the window. I used this code:

    Code:
    #include <windows.h> 
    static char myclass[] = "t"; 
    LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) 
    { 
    	switch(msg) 
    	{ 
    	case WM_CLOSE: 
    		DestroyWindow(hwnd); 
    		break; 
    	case WM_DESTROY: 
    		PostQuitMessage(0); 
    		break; 
    	//default: 
    		//return WndProc(hwnd, msg, wparam, lparam); 
    	} 
    	return 0; 
    } 
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) 
    { 
    	WNDCLASSEX wc; 
    	MSG message; 
    	HWND hwnd; 
    	
    	wc.cbClsExtra = 0; 
    	wc.cbWndExtra = 0; 
    	wc.hbrBackground = (HBRUSH) GetStockObject(GRAY_BRUSH); 
    	wc.hCursor = LoadCursor(NULL, IDC_ARROW); 
    	wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); 
    	wc.hInstance = hInstance; 
    	wc.lpfnWndProc = WndProc; 
    	wc.lpszClassName = myclass; 
    	wc.lpszMenuName = NULL; 
    	wc.style = NULL; 
    	wc.cbSize = sizeof(wc);
    	wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION); 
    	
    
    	if(!RegisterClassEx(&wc)) 
    	{ 
    		MessageBox(NULL, "Could Not Create Class","NO!",MB_OK);
    		return 0; 
    	} 
    	
    	hwnd = CreateWindowEx( 
    		WS_EX_CLIENTEDGE, 
    		myclass, 
    		"This is win", 
    		WS_OVERLAPPEDWINDOW, 
    		1, 
    		1, 
    		100, 
    		100, 
    		NULL, 
    		NULL, 
    		hInstance, 
    		NULL); 
    	if(hwnd == NULL) 
    	{ 
    		MessageBox(NULL, "Could Not Create hwnd","NO!",MB_OK);		
    		return 0; 
    	} 
    	
    	ShowWindow(hwnd, nShowCmd); 
    	UpdateWindow(hwnd); 
    	
    	while(GetMessage(&message, NULL, 0,0)) 
    	{ 
    		TranslateMessage(&message); 
    		DispatchMessage(&message); 
    	} 
    	
    	return message.wParam; 
    }
    Any suggestion what do I do with the hwnd?
    Baaaaaaaaah

  5. #5
    Megatron
    Guest
    Try this.
    Code:
    #include <windows.h>
    
    LRESULT CALLBACK WinProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
    {
    	switch( uMsg ) {
    
    		case WM_DESTROY:
    			PostQuitMessage(0);
    			return 0;
    		default:
    			return DefWindowProc(hWnd, uMsg, wParam, lParam);
    	}
    }
    
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) 
    {
    	WNDCLASSEX wcx;
    	MSG msg;
    	HWND		hWnd_Main;
    
    	wcx.cbSize = sizeof(WNDCLASSEX); 
    	wcx.style = 0; 
    	wcx.lpfnWndProc = WinProc; 
    	wcx.cbClsExtra = 0; 
    	wcx.cbWndExtra = 0; 
    	wcx.hInstance = hInstance; 
    	wcx.hIcon = NULL;
    	wcx.lpszMenuName = NULL;
    	wcx.hCursor = LoadCursor(NULL, IDC_ARROW); 
    	wcx.hbrBackground = HBRUSH(COLOR_BTNFACE+1);
    	wcx.lpszClassName = "MyApp"; 
    	wcx.hIconSm = NULL; 
    	
    	RegisterClassEx(&wcx);
    
    	hWnd_Main = CreateWindowEx(NULL, "MyApp", "App name", WS_THICKFRAME | WS_OVERLAPPEDWINDOW, 100, 100, 600, 400, NULL, NULL, hInstance, NULL);
    	ShowWindow(hWnd_Main, nShowCmd);
    
    	while(GetMessage(&msg, NULL, 0, 0)) {
    	   TranslateMessage(&msg); 
    	   DispatchMessage(&msg); 
    	}
    
    	UnregisterClass("MyApp", hInstance);
    	return msg.wParam;
    }

  6. #6

    Thread Starter
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827

    That works

    That work now but what changes did you make?
    Baaaaaaaaah

  7. #7
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    The main change I see is that the classname has been changed to a string literal rather than a variable. Why did you make it a static char[] by the way? I don't see why you've made it static, but at file scope

    Also, the right default return value for WinProc is there.
    Harry.

    "From one thing, know ten thousand things."

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width