Thread: How do you program a windows application?

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    8

    How do you program a windows application?

    okay, so in the tutorials it teaches you how to do all this programming for a console application, and i've got most of that down, however, when looking at some of the source codes for windows applications, as well as a simple windows application from dev-C++, i can see that it's all different? do you use the same commands? if so, how? it looks completely different!

    thanks in advance

    Chris

  2. #2
    Registered User
    Join Date
    Feb 2002
    Posts
    465
    there is a different structure to a windows program than there is to a normal console program. one of the main differences is the addition of the windows procedure.

    the windows procedure is a function that handles the various messages sent to your program by windows. windows programming is all about getting messages and handling them. messages can be anything, ranging from window creation, to keyboard or mouse input, to drawing the window, and so on.

    windows has a default message handler, but it really doesnt do much, so to get any real work out of your program you have to handle your own messages. to do this you write a window procedure. the prototype for it is as follows:

    Code:
         LRESULT CALLBACK WndProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam );
    the first parameter is the handle to your window, the second is the ID of the message sent. the last two are extra information about the message ( for example, on a WM_LBUTTONDOWN message [left mouse click] the lParam holds the mouse coordinates ).

    one thing to keep in mind is that YOU never call the WndProc function in your program. instead, windows calls this function whenever a message is sent to your program, and it uses it to decide what to do with the message.


    next, instead of main() you now have WinMain(). inside of WinMain, you create a WNDCLASS ( or WNDCLASSEX ) and set it up. this is the information about your program window. then you register the class and create the window. after doing that, you show the window and enter the message loop.

    the message loop is another important part of a windows program. instead of putting a lot of code in WinMain, programmers normally keep their application specific code out of WinMain. normally i will write a class that handles the application, and just run something like app.AppMain() in my message loop. other than that, the message loop does nothing but get the messages and send them to the window procedure function. a message loop normally looks something like this:

    Code:
    BOOL done = FALSE;
    
    while ( !done )
    {
        if ( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
        {
              if ( msg.message == WM_QUIT )
                     done == TRUE;
    
             TranslateMessage(&msg);
             DispatchMessage(&msg);
        }
    }
    the PeekMessage function looks at windows' message queue and puts the message into your MSG variable (here, msg). there is also GetMessage, but it doesnt return until a message appears, so it doesnt allow for idle time processing. in other words the program doesnt do anything while there are no messages being sent.

    and that is a simple windows program. for setting up a WNDCLASS, registering it, and creating a window, you should just read up on some of those tutorials. they could probably explain it better than me.

    i hope you have a better understanding of how it works now.
    Last edited by ...; 10-16-2003 at 08:21 PM.
    I came up with a cool phrase to put down here, but i forgot it...

  3. #3
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  4. #4
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    PeekMesage() is for games.
    It returns even if there is no message. This causes 100% CPU useage as the app constantly polls the OS for messages.

    GetMessage() is used for normal apps. It waits until there is a message for the app before returning.
    This means that there is less pressure on the CPU but the app can not do other things (like update its screen and object position ie time dependant drawing ect) when there is no message from the user / OS.
    "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

  5. #5

  6. #6
    Registered User
    Join Date
    Oct 2003
    Posts
    8
    well ... thanks for all that info, im sure it'll come in handy later on, but lol i didn't understand ANY of it. I'm sure however that the link x squared posted will help as well as the one that dante posted!

    thanks guys!

  7. #7
    Registered User
    Join Date
    Feb 2002
    Posts
    465
    Originally posted by novacain
    PeekMesage() is for games.
    It returns even if there is no message. This causes 100% CPU useage as the app constantly polls the OS for messages.

    GetMessage() is used for normal apps. It waits until there is a message for the app before returning.
    This means that there is less pressure on the CPU but the app can not do other things (like update its screen and object position ie time dependant drawing ect) when there is no message from the user / OS.

    sorry, im a pure game programmer. everything i write is in referance to game programming. sometimes i forget that there are other programs in the world :P
    I came up with a cool phrase to put down here, but i forgot it...

  8. #8
    Registered User
    Join Date
    Oct 2003
    Posts
    8
    okay, so i was reading up on some of the links there that you guys posted, and I was wondering, howcome there is so much code required to make a simple window, however, there is so little code to make a simple windows program that gives a message?

  9. #9
    Registered User Dante Shamest's Avatar
    Join Date
    Apr 2003
    Posts
    970
    Because to show a simple message, you only need to call MessageBox(). A Windows DLL loaded at runtime handles all the messages sent to it.

    But when you're creating a brand new window using CreateWindowEx() you have to handle the messages yourself, specify its size yourself, specify the caption yourself...etc...etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  2. how to make a windows application
    By crvenkapa in forum C++ Programming
    Replies: 3
    Last Post: 03-26-2007, 09:59 AM
  3. Program works on Windows XP and 2000, not on 98 or ME
    By MidnightlyCoder in forum C++ Programming
    Replies: 7
    Last Post: 03-10-2006, 03:36 PM
  4. Running C program in Linux or Windows
    By kepler in forum C Programming
    Replies: 4
    Last Post: 09-30-2003, 08:31 AM
  5. Windows Application Handles
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 06-15-2002, 05:09 PM