Jump to content
  • Advertisement
Sign in to follow this  
Squirell

Window Creation Help

This topic is 5105 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

My window creation code isn't working. I've used this before but it doesn't seem to like me now. What am I doing wrong?
#include <windows.h>

enum ERROR_CODE {OK = 0, FAILED_TO_REGISTER, FAILED_WINDOW_CREATION};

/*  Declare Windows procedure  */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);

HWND hwnd;
int window_width = 800;
int window_height = 600;
DWORD style;
bool done = false;
bool fullscreen = false;

int WINAPI WinMain (HINSTANCE hThisInstance,
                    HINSTANCE hPrevInstance,
                    LPSTR lpszArgument,
                    int nFunsterStil)

{
    WNDCLASS window_class;
    HINSTANCE instance = GetModuleHandle(NULL);

    window_class.style          = CS_OWNDC;
    window_class.cbClsExtra     = 0;
    window_class.cbWndExtra     = 0;
    window_class.hInstance      = instance;
    window_class.hIcon          = LoadIcon(NULL,IDI_APPLICATION);
    window_class.hCursor        = LoadCursor(NULL,IDC_ARROW);
    window_class.hbrBackground  = (HBRUSH)GetStockObject(BLACK_BRUSH);
    window_class.lpszMenuName   = NULL;
    window_class.lpszClassName  = "DH Class";
    window_class.lpfnWndProc    = WindowProcedure; 


    //Register the class with windows
    if(!RegisterClass(&window_class)){
        //Handle the error here
		return FAILED_TO_REGISTER;
    }

    /* The class is registered, let's create the program*/
    hwnd = CreateWindowEx (
           0,                   /* Extended possibilites for variation */
           "DH Class",         /* Classname */
           "Map Editor",       /* Title Text */
           WS_OVERLAPPEDWINDOW, /* default window */
           0,       /* Windows decides the position */
           0,       /* where the window ends up on the screen */
           800,                 /* The programs width */
           600,                 /* and height in pixels */
           HWND_DESKTOP,        /* The window is a child-window to desktop */
           NULL,                /* No menu */
           hThisInstance,       /* Program Instance handler */
           NULL                 /* No Window Creation data */
           );

    if(!hwnd){
        //IT EXITS HERE 
        return FAILED_WINDOW_CREATION;
    }

    ShowWindow (hwnd, nFunsterStil);

    //Finally, when we're done with the class we call UnregisterClass:
    UnregisterClass("DH Class",instance);

    /* The program return-value is 0 - The value that PostQuitMessage() gave */
    return OK;
}


/*  This function is called by the Windows function DispatchMessage()  */

LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)                  /* handle the messages */
    {
        case WM_DESTROY:
            PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
            //Kernel->Quit();
			done = true;
			return 0;
    }

    return 0;
}


Thanks in advance for any help

Share this post


Link to post
Share on other sites
Advertisement
You can take a look at or use my example code:

#include <windows.h>

#define WIN32_LEAN_AND_MEAN
#define WINDOW_NAME "ExampleWindow"
#define CLASS_NAME "ExW"
// Window dimensions
int windowwidth = 800;
int windowheight = 600;

LRESULT CALLBACK WinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
if(msg == WM_SETCURSOR) return 0;
else if(msg == WM_DESTROY) isgamerunning = false;

return DefWindowProc(hWnd, msg, wParam, lParam);
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
HWND hWnd;
MSG msg = { 0 };
WNDCLASSEX wndclass = { 0 };

// Set up window class
wndclass.cbSize = sizeof(WNDCLASSEX);
wndclass.lpszClassName = CLASS_NAME;
wndclass.lpfnWndProc = WinProc;
wndclass.hInstance = hInstance;
wndclass.hCursor = NULL;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);

if(!RegisterClassEx(&wndclass))
{
MessageBox(HWND_DESKTOP, "Failed to register window class. Aborting...", "Error!", MB_OK);
return 0;
}

if(!(hWnd = CreateWindow(CLASS_NAME, WINDOW_NAME, WS_OVERLAPPED | WS_DISABLED, 0, 0, windowwidth, windowheight, NULL, NULL, hInstance, NULL)))
{
MessageBox(HWND_DESKTOP, "Failed to create window. Aborting...", "Error!", MB_OK);
return 0;
}

ShowWindow(hWnd, nCmdShow);

//Put up some game updating loop here
if(PeekMessage(&msg, hWnd, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}


// Clean up and exit the app
UnregisterClass(CLASS_NAME, wndclass.hInstance );
return 0;
}



I hope this helps

Share this post


Link to post
Share on other sites
It's WindowProcedure that's causing the problem. The reason is, when you create the window, you're only handling the WM_DESTROY message, yet your not handling any other message, nor letting windows handle it. Creation of windows does send windows messages. You need a default handler. This should make it work.


switch (message) /* handle the messages */
{
case WM_DESTROY:
PostQuitMessage (0); /* send a WM_QUIT to the message queue */
//Kernel->Quit();
done = true;
return 0;
default:
return DefWindowProc(hwnd, message, wParam, lParam);

}





One other thing. This will just make the window pop open and close, because you're immediately finished with the window after the creation of it. You need to then put in a message handling loop to actually keep the program running.

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!