Need help Creating Basic Window

This is a discussion on Need help Creating Basic Window within the Windows Programming forums, part of the Platform Specific Boards category; I need help with the following. When executed know window is visible. Can someone help me, please? It is based ...

  1. #1
    Registered User
    Join Date
    Apr 2013
    Posts
    13

    Need help Creating Basic Window

    I need help with the following. When executed know window is visible. Can someone help me, please? It is based on a tutorial from msdn. I tried to give more structure by placing some of the code into functions. The compiled and executed msdn's example and it works. Mine doesn't.
    Code:
    #ifndef UNICODE
    #define UNICODE
    #endif
    #include <windows.h>
    LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
    int initialize(WNDCLASS* wc, HINSTANCE hInstance);
    int makeApp(HWND &hwnd, WNDCLASS wc);
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, 
                        LPSTR pCmdLine, int nCmdShow)
    {
        // Declare window class for the main window
        WNDCLASS wc = {};
        HWND hwnd;
        
        // Register , create, and show window
        if(!initialize(&wc, hInstance))
            return FALSE;
        if(!makeApp(hwnd, wc))
            return FALSE;
        ShowWindow(hwnd, nCmdShow);
        
        // Declare message struct
        MSG msg;
        // Create the message loop
        while(GetMessage(&msg, NULL, 0, 0))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);   
        }
        
        return 0;
    }
    int initialize(WNDCLASS *wc, HINSTANCE hInstance)
    {
     const wchar_t CLASS_NAME[]  = L"Sample Window Class";
     //wc.sytle = 0;
     (*wc).lpfnWndProc = WindowProc;
     //wc.cbClsExtra = NULL;
     //wc.cbWndEXtra = NULL;
     (*wc).hInstance = hInstance;
     (*wc).lpszClassName = CLASS_NAME;
     RegisterClass(wc);
        
    }
    int makeApp(HWND &hwnd, WNDCLASS wc)
    {
     hwnd = CreateWindowEx(
     0,    // Optional window styles
     wc.lpszClassName,   // Window Class
     L"Learn to Program Windows", // Window Text
     WS_OVERLAPPEDWINDOW,  // Window style
     
     // Size and position
     CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
     NULL,  // Parent window
     NULL,   // Menu
     wc.hInstance,  // Instance handle
     NULL   // Additional application data
     );
     
     if(hwnd == NULL)
     {
      return 0;
     }
     else
     {
      return 1;
     } 
    }
    
    LRESULT WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
    {
     switch(uMsg)
     {
      case WM_PAINT:
                {
                    PAINTSTRUCT ps;
                        HDC hdc = BeginPaint(hwnd, &ps);
                    
                        // All painting occurs here, between BeginPaint and EndPaint
                    
                        FillRect(hdc, &ps.rcPaint, (HBRUSH) (COLOR_WINDOW+1));
                    
                    EndPaint(hwnd, &ps);
                    return 0;
                }
      case WM_DESTROY:
                {
                    PostQuitMessage(0);
                    return 0;
                }
     }
     return DefWindowProc(hwnd, uMsg, wParam, lParam);
    }

  2. #2
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,856
    Do you know which line fails?

    Did you use GetLastError() to find out why?

    >>int makeApp(HWND &hwnd, WNDCLASS wc)

    HWND is a HANDLE, which is a VOID pointer. So you do not need the '&' (which is probably your problem as the assignment / validation might be failing.)
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    419
    Code:
    LRESULT WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
    should be
    Code:
    LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
    Also, the function
    Code:
    int initialize(WNDCLASS *wc, HINSTANCE hInstance)
    needs a return value:
    Code:
    return RegisterClass(wc);

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    419
    My suggestions above aren't all of the problem. Still doesn't work after those corrections.

    Quote Originally Posted by novacain View Post
    >>int makeApp(HWND &hwnd, WNDCLASS wc)

    HWND is a HANDLE, which is a VOID pointer. So you do not need the '&' (which is probably your problem as the assignment / validation might be failing.)
    Isn't that C++ syntax, for passing by reference (as opposed to passing by value or pointer)?
    It's apparantly to allow makeApp() to set the value of hwnd, which was declared in WinMain().

  5. #5
    Registered User
    Join Date
    Jun 2009
    Posts
    87
    Here's one way I got it to work.
    Code:
    // #ifndef UNICODE
    // #define UNICODE
    // #endif
    #include <windows.h>
    
    WNDCLASS wc = {};
    HWND hwnd;
    const char szNAME[]  = "Sample Window Class";
    
    LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
    int initialize(WNDCLASS* wc, HINSTANCE hInstance);
    int makeApp(HINSTANCE hInstance, WNDCLASS wc);
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
                         LPSTR pCmdLine, int nCmdShow)
    {
        // Declare window class for the main window
        // WNDCLASS wc = {};
        // HWND hwnd;
    
        // Register , create, and show window
        if(!initialize(&wc, hInstance))
           return FALSE;
        if(!makeApp(hInstance, wc))
           return FALSE;
        ShowWindow(hwnd, nCmdShow);
    
        // Declare message struct
        MSG msg;
        // Create the message loop
        while(GetMessage(&msg, NULL, 0, 0))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    
        return 0;
    }
    
    int initialize(WNDCLASS *wc, HINSTANCE hInstance)
    {
        // const char szNAME[]  = "Sample Window Class";
        //wc.sytle = 0;
        (*wc).lpfnWndProc = WindowProc;
        //wc.cbClsExtra = NULL;
        //wc.cbWndEXtra = NULL;
        (*wc).hInstance = hInstance;
        (*wc).lpszClassName = szNAME;
        RegisterClass(wc);
        return (TRUE);
    
    }
    
    int makeApp(HINSTANCE hInstance, WNDCLASS wc)
    {
        hwnd = CreateWindowEx(
               0,    // Optional window styles
               wc.lpszClassName,   // Window Class
               // szNAME,
               "Learn to Program Windows", // Window Text
               WS_OVERLAPPEDWINDOW,  // Window style
    
               // Size and position
               CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
               NULL,  // Parent window
               NULL,   // Menu
               wc.hInstance,  // Instance handle
               // hInstance,
               NULL   // Additional application data
    );
    
        if(hwnd == NULL)
        {
           return 0;
        }
        else
        {
           return 1;
        }
    }
    
    LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
    {
        switch(uMsg)
        {
           case WM_PAINT:
           {
               PAINTSTRUCT ps;
               HDC hdc = BeginPaint(hwnd, &ps);
    
               // All painting occurs here, between BeginPaint and EndPaint
    
               FillRect(hdc, &ps.rcPaint, (HBRUSH) (COLOR_WINDOW+1));
    
               EndPaint(hwnd, &ps);
               return 0;
           }
           case WM_DESTROY:
           {
               PostQuitMessage(0);
               return 0;
           }
        }
        return DefWindowProc(hwnd, uMsg, wParam, lParam);
    }

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    419
    Referring to zordon's code, this seems to fix it:

    Code:
    LRESULT WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
    change to
    Code:
    LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
    In the function initialize():
    Code:
    RegisterClass(wc);
    change to
    Code:
    return RegisterClass(wc);
    Those are the same two changes I mentioned previously.

    But also in the function initialize():
    Code:
    const wchar_t CLASS_NAME[]  = L"Sample Window Class";
    change to
    Code:
    static const wchar_t CLASS_NAME[]  = L"Sample Window Class";

  7. #7
    Registered User
    Join Date
    Apr 2013
    Posts
    13
    Okay, thanks everyone for the replies. I appreciate it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. creating a blog with very basic C# understanding
    By Cybermouse in forum C# Programming
    Replies: 0
    Last Post: 06-05-2012, 06:48 PM
  2. Creating a child window in a parent window
    By vopo in forum Windows Programming
    Replies: 8
    Last Post: 10-06-2007, 04:15 PM
  3. few basic window questions
    By apsync in forum Windows Programming
    Replies: 1
    Last Post: 07-16-2005, 12:32 PM
  4. Basic Window application Help
    By The Brain in forum Windows Programming
    Replies: 2
    Last Post: 02-11-2005, 03:02 PM
  5. Problem with creating new window, from another window
    By Garfield in forum Windows Programming
    Replies: 6
    Last Post: 01-11-2004, 01:10 PM

Tags for this Thread


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