Thread: How can I go on ?

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    7

    How can I go on ?

    Hi!

    I have made a lot of console programs in dev-cpp.
    I now want to go on and make windows applications.
    Below is a sample code from dev-cpp creating a simple
    window. But how can I go on further? How can I do
    such a simple thing as print some text in the window?


    Code:
    /* Creation of a simple Windows API program */
    
    #include <windows.h>
    
    
    /*  Declare Windows procedure  */
    LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
    
    /*  Make the class name into a global variable  */
    char szClassName[ ] = "WindowsApp";
    
    
    int WINAPI
    WinMain (HINSTANCE hThisInstance,
             HINSTANCE hPrevInstance,
             LPSTR lpszArgument,
             int nFunsterStil)
    
    {
        HWND hwnd;               /* This is the handle for our window */
        MSG messages;            /* Here messages to the application are saved */
        WNDCLASSEX wincl;        /* Data structure for the windowclass */
    
        /* The Window structure */
        wincl.hInstance = hThisInstance;
        wincl.lpszClassName = szClassName;
        wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
        wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
        wincl.cbSize = sizeof (WNDCLASSEX);
    
        /* Use default icon and mouse-pointer */
        wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
        wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
        wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
        wincl.lpszMenuName = NULL;                 /* No menu */
        wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
        wincl.cbWndExtra = 0;                      /* structure or the window instance */
        /* Use Windows's default color as the background of the window */
        wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
    
        /* Register the window class, and if it fails quit the program */
        if (!RegisterClassEx (&wincl))
            return 0;
    
        /* The class is registered, let's create the program*/
        hwnd = CreateWindowEx (
               0,                   /* Extended possibilites for variation */
               szClassName,         /* Classname */
               "Windows App",       /* Title Text */
               WS_OVERLAPPEDWINDOW, /* default window */
               CW_USEDEFAULT,       /* Windows decides the position */
               CW_USEDEFAULT,       /* where the window ends up on the screen */
               544,                 /* The programs width */
               375,                 /* and height in pixels */
               HWND_DESKTOP,        /* The window is a child-window to desktop */
               NULL,                /* No menu */
               hThisInstance,       /* Program Instance handler */
               NULL                 /* No Window Creation data */
               );
    
        /* Make the window visible on the screen */
        ShowWindow (hwnd, nFunsterStil);
    
        /* Run the message loop. It will run until GetMessage() returns 0 */
        while (GetMessage (&messages, NULL, 0, 0))
        {
            /* Translate virtual-key messages into character messages */
            TranslateMessage(&messages);
            /* Send message to WindowProcedure */
            DispatchMessage(&messages);
        }
    
        /* The program return-value is 0 - The value that PostQuitMessage() gave */
        return messages.wParam;
    }
    
    
    /*  This function is called by the Windows function DispatchMessage()  */
    
    LRESULT CALLBACK
    WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
        switch (message)                  /* handle the messages */
        {
            case WM_DESTROY:
                PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
                break;
            default:                      /* for messages that we don't deal with */
                return DefWindowProc (hwnd, message, wParam, lParam);
        }
    
        return 0;
    }

  2. #2
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    Ok, to output text onto the window is extremely easy. It require the usage of the GDI(Graphical Device Interface). Here is an example of just outputting text:
    Code:
    LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
    {
            HDC hdc;
            PAINTSTRUCT ps;
            switch(msg)
            {
                       case WM_PAINT:
                            hdc=BeginPaint(hwnd,&ps);
                            TextOut(hdc,x,y,"Outputted text.",15);
                            EndPaint(hwnd,&ps);
                            return 0;
                            
                       case WM_DESTROY:
                            PostQuitMessage(0);
                            return 0;
            }
            return DefWindowProc(hwnd,msg,wParam,lParam);
    }
    That's my window procedure and I add the WM_PAINT case statement. Then I have to get a handle on the drawing context so I call the BeginPaint function. Then you call the TextOut function (MSDN Documentation) Then once you are done with your drawing operations you call the EndPaint function. Oh and see the MSDN Documentation for the parameters needed for the TextOut function. That should get you a little further but I suggest getting a book on it, such as Charles Petzold's Programming Windows Fifth Edition. That book covers almost ALL the basics and even some advanced topics of Win32 programming and it goes into good detail on the GDI. You should learn and understand the basic window code before you move on though.
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  3. #3
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    >>But how can I go on further?<<

    A popular tutorial is provided by theForger. Search this board for other threads relating to starting out in win32, winapi or window programming and you should dig up some other links to other sites or books that may be of interest.

    As jmd15 has pointed out, Petzold's Windows Programming is very good and msdn is very useful as a primary reference.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    7

    thanks

    Ok! thank you both! / Buzzer

Popular pages Recent additions subscribe to a feed