Thread: figuring out what the code means

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

    figuring out what the code means

    Hello all,

    I have typed this code from a YouTube video, which opens up a blank window, but I have no I idea what it means,

    Can anyone let me know what these codes mean

    Code:
    #include <windows.h>
    
    
    
    
    
    
    LRESULT CALLBACK WindowProcedure(HWND,UINT,WPARAM,LPARAM);
    //hwnd is a handle to the window.
    
    
    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
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    955
    The short answer is that it creates a window and then processes messages sent to it. This is a basic skeleton of a Windows application.

    A "message" is something that Windows sends to a window (or to the application) whenever "something" happens, like the mouse moves or is clicked or a key is pressed or a window is moved or resized or closed. It's "event driven" programming, if you want to look up that term.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,669
    > Can anyone let me know what these codes mean
    You mean like the presenter of the YT video you watched?

    In amongst the "ums" and "ahs" and the terrible typing, they should have been explaining what it's all for.
    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.

  4. #4
    Registered User
    Join Date
    Jan 2010
    Posts
    216

    Post

    As others have said it's a standard run through of a window creation.

    It starts by making a forward declaration of a window message handling function:

    Code:
    LRESULT CALLBACK WindowProcedure(HWND,UINT,WPARAM,LPARAM);
    //hwnd is a handle to the window.
    Then it enters the part where Windows will essentially access your program:

    Code:
    int WINAPI WinMain(HINSTANCE hInst , HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
    {
    ...etc
    Then it declares a window class (note this needs the window message handling function), then registers it, and then shows the window.

    Then it enters a while loop that handles messages sent by Windows and sends them on to the window message handling function.

    And finally is the full definition of the window message handling function:

    Code:
    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);
        }
    }
    I'm guessing this just shows a blank window with no menu and simply waits for you to click the X button top-right to close the window.

  5. #5
    Registered User
    Join Date
    Jul 2022
    Posts
    78
    can someone explain this code for me:

    Code:
        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 there is, can tell me what the window variable name is

  6. #6
    Registered User
    Join Date
    Jul 2022
    Posts
    78
    Code:
        wc1.hbrBackground = (HBRUSH) COLOR_WINDOW ; //set window background colour
        wc1.hCursor = LoadCursor(NULL, IDC_ARROW); //set cursor type
    is these comments right?

  7. #7
    Registered User
    Join Date
    Jan 2010
    Posts
    216
    Yes they are.

  8. #8
    Registered User
    Join Date
    Jan 2010
    Posts
    216

    Post

    Quote Originally Posted by WaterSerpentM View Post
    can someone explain this code for me:

    Code:
        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 there is, can tell me what the window variable name is
    It's a window class. You must register a window class before you can create a window. The background colour is set using the HBRUSH part, and then the cursor type after that (in your code). IDC_ARROW is the most common, probably the cusor type you're using right now. The hInstance part is (if I remember correctly) a handle to the application your making that your window will run in. Its variable type is of type HINSTANCE.

    Windows Data Types (BaseTsd.h) - Win32 apps | Microsoft Learn

    Scroll down until you find it. It's not all that helpful to be honest, just know that the HINSTANCE hInst in your code is very important and is something your window class will want. Then the class is given a name. In this case it's "myWindowClass".

    Note the L before it means the string will be converted into a wide character string. The last part:

    Code:
    wc.lpfnWndProc = WindowProcedure;
    Determines which message handling function you want to use to handle messages sent to the window. You have to make this yourself although most IDE's (the place where you write your code) I imagine make one for you as an example.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 09-04-2019, 07:56 AM
  2. Replies: 11
    Last Post: 04-22-2012, 10:41 AM
  3. figuring out code
    By Thegame16 in forum C Programming
    Replies: 16
    Last Post: 09-23-2011, 06:47 PM
  4. What does this code means?
    By junkeat90 in forum C++ Programming
    Replies: 6
    Last Post: 01-14-2008, 05:03 AM
  5. What does this code means??
    By dianazheng in forum C Programming
    Replies: 13
    Last Post: 10-12-2004, 09:45 PM

Tags for this Thread