Jump to content

CreateWindow not working


devrandom
 Share

Recommended Posts

Hello,

I created this simple code, but it always returns 1.

Where's the problem?

#include <stdio.h>
#include <windows.h>

int main(void)
{
    HINSTANCE instance;
    HWND handle;

    handle = CreateWindow (
                                "MainWClass",  // Class name
                                "Window!!",    // Title
                                WS_MINIMIZEBOX|// \
                                WS_CAPTION|    //  \__> style
                                WS_POPUP|      //  /
                                WS_SYSMENU,    // /
                                CW_USEDEFAULT, // Left
                                CW_USEDEFAULT, // Top
                                250,           // Width
                                250,           // Height
                                (HWND) NULL,
                                (HMENU) NULL,
                                instance,
                                NULL
                             );

    if (!handle)
    {
        return 1;
    }

    ShowWindow(handle, SW_SHOW);

    while (1)
    {
        UpdateWindow(handle);
    }

    return 0;
}

Note: it is C, not C++... Is this the problem?

I know only C, how can I create windows in C with Windows APIs?

Thank you :)

Edited by devrandom
Link to comment
Share on other sites

You aren't initialising instance for a start. How about setting up a simple error catching mechanism. If you just use return GetLastError(); then look up the code on msdn then we would be a bit closer to finding out, saying that it just failed is not really enough.

Mat

Thank you :)

It's fine to use C. The problem is that you are attempting to use "MainWClass" but I don't see where you registered this class anywhere.

Yes, I think this is the problem, I just modified a bit the MSDN example that evidently isn't working :S

How I can create a class? I looked at RegisterClass but being at the beginning with the Windows APIs, I have no idea on how to create the structure to pass as parameter for that function...

Is there any example on how to do this?

Sorry for my bad english :)

Edited by devrandom
Link to comment
Share on other sites

How I can create a class? I looked at RegisterClass but being at the beginning with the Windows APIs, I have no idea on how to create the structure to pass as parameter for that function...

Something like this should work (untested):

WNDCLASS class = {0};
class.style = 0;
class.hInstance = GetModuleHandle(NULL);
class.lpfnWndProc = (WNDPROC) myWindowProcedure;
class.lpszClassName = (LPCTSTR) _T("MyWindowClass");
// etc
Edited by danielkza
Link to comment
Share on other sites

_T is a macro that will create a string that is either ANSI or Unicode depending on your platform.

I'm surprised anyone still uses it and doesn't just move to Unicode.

I usually use Unicode exclusively, but for the example I thought I'd stick to the definition on MSDN.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...