Thread: open a blank window in C

  1. #1
    Registered User
    Join Date
    Jul 2022
    Posts
    50

    open a blank window in C

    hello,

    I am trying to open a blank window in C


    I have coded word for word from this youtube video:

    Windows GUI Programming with C/C++ ( Win32 API ) | Part -1 | Creating a window - YouTube

    it runs, but nothing happens


    Code:
    #include <windows.h>
    
    
    
    
    
    
    LRESULT CALLBACK WindowProcedure(HWND,UINT,WPARAM,LPARAM);
    
    
    int WINAPI WinMain(HINSTANCE hInst , HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
    {
    
    
        //MessageBox(NULL, "HELLO","My first GUI",MB_OK);
    
    
        WNDCLASSEXW wc = {0};
    
    
        wc.hbrBackground = (HBRUSH) COLOR_WINDOW ;
        wc.hCursor = LoadCursor(NULL, IDC_ARROW);
        wc.hInstance = hInst;
        wc.lpszClassName = L"myWindowClass";
        wc.lpfnWndProc = WindowProcedure;
    
    
    
    
    
    
        if(!RegisterClassW(&wc));
            return -1;
    
    
            CreateWindowW(L"myWindowClass",L"My Window",WS_EX_OVERLAPPEDWINDOW | WS_VISIBLE,100,100,500,500,
                          NULL,NULL,NULL,NULL);
    
    
    
    
        MSG msg = {0};
    
    
        while( GetMessage(&msg,NULL,NULL,NULL) )
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    
    
        return 0;
    
    
    }
    
    
    LRESULT CALLBACK WindowProcedure(HWND HWND,UINT msg,WPARAM wp,LPARAM lp)
    {
        switch ( msg)
        {
        case WM_DESTROY:
            PostQuitMessage(0);
            break;
        default:
            return DefWindowProcW(HWND,msg,wp,lp);
    
    
        }
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > I have coded word for word from this youtube video:

    Well, not exactly.....
    if(!RegisterClassW(&wc));
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jul 2022
    Posts
    50
    can someone tell me what is wrong?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Yes, watch the video again (well you can skip about 20 minutes or so) and look at that line of code in the video vs what you wrote.

    Look VERY carefully.

    You're off by one character.

    > can someone tell me what is wrong?
    No, it's far more educational for you to experience the sudden shock of realisation.
    So you don't make the mistake again.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Jul 2022
    Posts
    50
    Quote Originally Posted by Salem View Post
    Yes, watch the video again (well you can skip about 20 minutes or so) and look at that line of code in the video vs what you wrote.

    Look VERY carefully.

    You're off by one character.

    > can someone tell me what is wrong?
    No, it's far more educational for you to experience the sudden shock of realisation.
    So you don't make the mistake again.
    can you please tell me the character, its just 1

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    No.

    In fact, open a new text file, and watch the video again.
    Then retype the program "word for word" (as you put it), and see if you make the same mistake again.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Jul 2022
    Posts
    50
    Quote Originally Posted by Salem View Post
    No.

    In fact, open a new text file, and watch the video again.
    Then retype the program "word for word" (as you put it), and see if you make the same mistake again.
    ok then =]

  8. #8
    Registered User
    Join Date
    Jul 2022
    Posts
    50
    A clue maybe?
    or what line?

  9. #9
    Registered User
    Join Date
    Dec 2017
    Posts
    1,632
    Salem's first post showed you the line. Look at the very end of the line.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  10. #10
    Registered User
    Join Date
    Jul 2022
    Posts
    50
    lol got it

    that damn
    Code:
    ;

  11. #11
    Registered User
    Join Date
    Jul 2022
    Posts
    50
    Thanks everyone that helped me with this =]

  12. #12
    Registered User
    Join Date
    Jan 2010
    Posts
    206
    Hi there,

    Is this the somewhat classic error where an if statement regards a semi-colon after it as just an empty line, yet the only line it will execute if the condition is valid?

    So the code in question would execute an empty line and then promptly return -1 hence why the window never showed on the first run?

    Thanks

  13. #13
    Registered User
    Join Date
    Jul 2022
    Posts
    50
    hey guys,
    one more thing,

    does the cmd always have to come up with the window?

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Yes, despite your indentation, you wrote
    Code:
    if(!RegisterClassW(&wc)) {
      // do nothing
    }
    
    return -1;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  15. #15
    Registered User
    Join Date
    Jul 2022
    Posts
    50
    maybe I am just new to this, but,


    Is all that coding needed just to open up a blank window? or are there excess coding?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Open URL in new window
    By Magos in forum Windows Programming
    Replies: 1
    Last Post: 04-17-2006, 02:17 AM
  2. Keeping window open
    By jpipitone in forum C Programming
    Replies: 5
    Last Post: 12-01-2003, 12:56 PM
  3. new open/save window???
    By hostensteffa in forum Windows Programming
    Replies: 14
    Last Post: 06-26-2002, 12:12 PM
  4. Redrawing window blank
    By unanimous in forum Windows Programming
    Replies: 1
    Last Post: 04-06-2002, 09:12 PM
  5. How to open window maximized
    By Unregistered in forum Windows Programming
    Replies: 5
    Last Post: 02-04-2002, 04:29 PM

Tags for this Thread