Newbie question about creating a window

This is a discussion on Newbie question about creating a window within the Windows Programming forums, part of the Platform Specific Boards category; Hi there, I'm working on my first non-console program and I'm stuck. This is the skeleton code I start with ...

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    14

    Newbie question about creating a window

    Hi there,

    I'm working on my first non-console program and I'm stuck. This is the skeleton code I start with :

    Code:
    #include <windows.h>
    
    const char g_szClassName[] = "myWindowClass";
    
    // Step 4: the Window Procedure
    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 DefWindowProc(hwnd, msg, wParam, lParam);
        }
        return 0;
    }
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
        LPSTR lpCmdLine, int nCmdShow)
    {
        WNDCLASSEX wc;
        HWND hwnd;
        MSG Msg;
    
        //Step 1: Registering the Window Class
        wc.cbSize        = sizeof(WNDCLASSEX);
        wc.style         = 0;
        wc.lpfnWndProc   = WndProc;
        wc.cbClsExtra    = 0;
        wc.cbWndExtra    = 0;
        wc.hInstance     = hInstance;
        wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
        wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
        wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
        wc.lpszMenuName  = NULL;
        wc.lpszClassName = g_szClassName;
        wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);
    
        if(!RegisterClassEx(&wc))
        {
            MessageBox(NULL, "Window Registration Failed!", "Error!",
                MB_ICONEXCLAMATION | MB_OK);
            return 0;
        }
    
        // Step 2: Creating the Window
        hwnd = CreateWindowEx(
            WS_EX_CLIENTEDGE,
            g_szClassName,
            "The title of my window",
            WS_OVERLAPPEDWINDOW,
            CW_USEDEFAULT, CW_USEDEFAULT, 240, 120,
            NULL, NULL, hInstance, NULL);
    
        if(hwnd == NULL)
        {
            MessageBox(NULL, "Window Creation Failed!", "Error!",
                MB_ICONEXCLAMATION | MB_OK);
            return 0;
        }
    
        ShowWindow(hwnd, nCmdShow);
        UpdateWindow(hwnd);
    
        // Step 3: The Message Loop
        while(GetMessage(&Msg, NULL, 0, 0) > 0)
        {
            TranslateMessage(&Msg);
            DispatchMessage(&Msg);
        }
        return Msg.wParam;
    }
    When I try to compile it (using VS2k5), I get the following errors:

    1>simplewindow.cpp(40) : error C2440: '=' : cannot convert from 'const char [14]' to 'LPCWSTR'
    1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
    1>simplewindow.cpp(46) : error C2664: 'MessageBoxW' : cannot convert parameter 2 from 'const char [28]' to 'LPCWSTR'
    1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
    1>simplewindow.cpp(57) : error C2664: 'CreateWindowExW' : cannot convert parameter 2 from 'const char [14]' to 'LPCWSTR'
    1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
    1>simplewindow.cpp(62) : error C2664: 'MessageBoxW' : cannot convert parameter 2 from 'const char [24]' to 'LPCWSTR'
    1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast


    Supposedly, I have to add a capital L before each string, like so: L"bla bla" however, when I do so, I only manage to get this additional error:

    simplewindow.cpp(3) : error C2440: 'initializing' : cannot convert from 'const wchar_t [14]' to 'const char []'
    1> There is no context in which this conversion is possible


    Could anyone explain the problem and how to fix it to me?

    Thank you for your time.

    Gozu


    ps: Bold added for clarity's sake. (to make my comments stand out from the code)

  2. #2
    Registered User
    Join Date
    Jan 2007
    Posts
    14
    The problem was solved, To quote Griffin1977's message:

    The quick answer is to ensure the proprocessor define UNICODE is not defined in your code. In project properties C/C++->Preprocesser->preprocessor defines, often it is set as part of the "project defaults" so you need to uncheck the "inherit from parent or project defaults" box.

    Having this defined will mean all the windows functions take 16-bit unicode wide-chars (the ...W functions) instead of normal 8-bit chars (the ....A functions).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 10:38 AM
  2. Pong is completed!!!
    By Shamino in forum Game Programming
    Replies: 11
    Last Post: 05-26-2005, 11:50 AM
  3. Question about creating a specific child window type
    By PJYelton in forum Windows Programming
    Replies: 8
    Last Post: 03-28-2005, 10:58 PM
  4. Window menu question
    By axr0284 in forum Windows Programming
    Replies: 0
    Last Post: 03-08-2005, 09:01 AM
  5. struct question
    By caduardo21 in forum Windows Programming
    Replies: 5
    Last Post: 01-31-2005, 04:49 PM

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