A simple window

This is a discussion on A simple window within the Windows Programming forums, part of the Platform Specific Boards category; Hi guys, I know this is pathetic, but I was having trouble when I tried to use CreateWindowEx(). Although I ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    32

    A simple window

    Hi guys, I know this is pathetic, but I was having trouble when I tried to use CreateWindowEx(). Although I did it before, I kept getting "Window creation failed" error box I used to error check.

    Code:
    #include "stdafx.h"
    #include "Resource.h"
    
    LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, 
    						 WPARAM wParam, LPARAM lParam)
    {
    	switch(msg)
    	{
    	case WM_CREATE:
    		{
    			HMENU hMenu, hSubMenu;
    			hMenu = CreateMenu();
    
    			hSubMenu = CreateMenu();
    			AppendMenu(hSubMenu, MF_STRING, 9001, "E&xit");
    			AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, "&File");
    
    			hSubMenu = CreateMenu();
    			AppendMenu(hSubMenu, MF_STRING, 9002, "Greetings");
    			AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, "&Hi");
    
    			SetMenu(hwnd, hMenu);
    		}
    		break;
    	case WM_COMMAND:
    		switch(LOWORD(wParam))
    		{
    		case 9001:
    			SendMessage(hwnd, WM_CLOSE, 0, 0);
    			break;
    		case 9002:
    			MessageBox(NULL, "Greetings", "Hi there", MB_OK);
    			break;
    		}
    		break;
    	case WM_CLOSE:
    		DestroyWindow(hwnd);
    		break;
    	case WM_DESTROY:
    		PostQuitMessage(0);
    		break;
    	default:
    		DefWindowProc(hwnd, msg, wParam, lParam);
    		break;
    	}
    	return 0;
    }
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, 
    				   LPSTR lpCmdLine, int nShowCmd)
    {
    	HWND hwnd;
    	MSG msg;
    	WNDCLASSEX wcex;
    	const char g_szClassName[] = "Window Class Name";
    
    	wcex.cbSize = sizeof(WNDCLASSEX);
    	wcex.cbClsExtra = 0;
    	wcex.cbWndExtra = 0;
    	wcex.hbrBackground = (HBRUSH) (COLOR_HIGHLIGHTTEXT +1);
    	wcex.hCursor = LoadCursor(NULL, IDC_CROSS);
    	wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    	wcex.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
    	wcex.hInstance = hInstance;
    	wcex.lpfnWndProc = WndProc;
    	wcex.lpszClassName = g_szClassName;
    	wcex.lpszMenuName = NULL;
    	wcex.style = 0;
    
    	if(!RegisterClassEx(&wcex))
    	{
    		MessageBox(NULL, "Window class registration failed", "Error", MB_OK);
    		return 0;
    	}
    
    	hwnd = CreateWindowEx(WS_EX_CLIENTEDGE, g_szClassName, "My window",
    		WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 180, 120,
    		NULL, NULL, hInstance, NULL);
    
    	if(!hwnd)
    	{
    		MessageBox(NULL, "Window creation failed", "Error", MB_OK);
    		return 0;
    	}
    
    	ShowWindow(hwnd, nShowCmd);
    	UpdateWindow(hwnd);
    
    	while(GetMessage(&msg, NULL, 0, 0) > 0)
    	{
    		TranslateMessage(&msg);
    		DispatchMessage(&msg);
    	}
    
    	return msg.wParam;
    }
    Your help will be very much appreciated.

  2. #2
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217
    Maybe the class name cannot have spaces

  3. #3
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Your window procedure needs to return the result of the DefWindowProc call, not break (and therefore return zero).

    Also, it would be wise to replace those literal numbers in your WM_COMMAND handler with properly defined constants; those raw numbers are meaningless to everyone and anyone and, in a week - perhaps longer - they'll be meaningless to you, too.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,660
    Yep - if you return 0 on WM_NCCREATE, window creation fails.

    gg

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    32
    thanks for the tips, guys

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Just starting Windows Programming, School me!
    By Shamino in forum Windows Programming
    Replies: 17
    Last Post: 02-22-2008, 07:14 AM
  2. Why only 32x32? (OpenGL) [Please help]
    By Queatrix in forum Game Programming
    Replies: 2
    Last Post: 01-23-2006, 01:39 PM
  3. Adding colour & bmps to a Win 32 Window??
    By carey_sizer in forum Windows Programming
    Replies: 4
    Last Post: 09-04-2004, 05:55 PM
  4. OpenGL and Windows
    By sean345 in forum Game Programming
    Replies: 5
    Last Post: 06-24-2002, 10:14 PM
  5. opengl code not working
    By Unregistered in forum Windows Programming
    Replies: 4
    Last Post: 02-14-2002, 09:01 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21