Programming for windows

This is a discussion on Programming for windows within the Windows Programming forums, part of the Platform Specific Boards category; Where does the program code go? Does it go at the very end of the file after all of the ...

  1. #1
    Registered User Cosmic Diva's Avatar
    Join Date
    Mar 2002
    Posts
    37

    Question Programming for windows

    Where does the program code go? Does it go at the very end of the file after all of the winmain stuff? or does it go in between or below this:

    // TODO: Place code here.

    return 0;
    "A computer is this amazingly fast.....but stupid machine." - C++ for dummies

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Your program will begin execution at your WinMain function...

  3. #3
    Registered User Cosmic Diva's Avatar
    Join Date
    Mar 2002
    Posts
    37
    I still don't understand. WHERE do I start coding?

    (code)
    int APIENTRY WinMain(HINSTANCE hInstance,
    HINSTANCE hPrevInstance,
    LPSTR lpCmdLine,
    int nCmdShow)
    (HERE)

    {
    // TODO: Place code here.

    (OR HERE?)

    return 0;
    }
    (/coding)


    Do I still use int main() ofterwards or just start coding with things like cout <<"Some text here" ?
    "A computer is this amazingly fast.....but stupid machine." - C++ for dummies

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    No....as I said, WinMain() is your code's point of entry for Win32 Applications.....its main() if you want to write console apps

    Alos you cant use iostreams on a Win32 app......read Programming Windows by C Petzold or Windows Programming from the Ground Up by H Schildt.

    Also go to;

    [list=1][*]Sunlight's Site [*]Ken's Site [/list=1]
    Last edited by Fordy; 04-19-2002 at 12:41 AM.

  5. #5
    Anal comment spacer DominicTrix's Avatar
    Join Date
    Apr 2002
    Posts
    120
    I found this place pretty useful too...

    Forgers Win32 Tutorial

  6. #6
    Registered User Cosmic Diva's Avatar
    Join Date
    Mar 2002
    Posts
    37

    What's wrong now???!

    I STILL don't get it. Now what's wrong with my code? I get 2 errors.: confused:

    #include "stdafx.h"

    int APIENTRY WinMain(HINSTANCE hInstance,
    HINSTANCE hPrevInstance,
    LPSTR lpCmdLine,
    int nCmdShow)
    {
    cout << "My first windows program!";

    return 0;
    }
    "A computer is this amazingly fast.....but stupid machine." - C++ for dummies

  7. #7
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    You can't use cout. That only outputs on command line (or a simulation of it). Instead, you have to explicitly create your own window and write to it.

  8. #8
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    You should check out the places mentioned by DominicTrix & Fordy. Winprog's tutorials are a useful introductory read if you are starting out.

    'cout' - there is no std output unless you make a console wnd -using 'main'. With 'WinMain' you are more or less seting yourself up to do a gui app - which requires use of the win32 API or a class library that encapsulates it.

    For your no-money-back-guaranteed 'hello world' windows gui:
    Code:
    #include <windows.h>
    int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
    {
    MessageBox(0,"hello world","Message Box - A simple window",MB_OK);
    return 0;
    }
    Don't try to build on this as it does nothing - go to the sites suggested by Fordy, or do a search, to find explicit examples you feel more comfortable with.

    (BTW thanks for the endorsement, Fordy - the cheque's in the post )

  9. #9
    Registered User Cosmic Diva's Avatar
    Join Date
    Mar 2002
    Posts
    37

    Exclamation

    OOOOOOOOOOOOOOOOOOOOOOOOOO!

    I was thinking about it all wrong. Does anyone know where I can find more info about creating a window and writting to it? I already checked the links, but I may have missed it.

    This is a separate question. I started making this progrram in visual basic 5. It has 4 forms. 2 are what the user will work with and the other 2 were dialong boxes. My question is if I were to write this program in C++ and make each form a separate C++ file, do I have to include the windows header in all the C++ files, or just one? I plan to write the whole thing in 1 window since it's small anyway, but I was just wondering.
    "A computer is this amazingly fast.....but stupid machine." - C++ for dummies

  10. #10
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Originally posted by Cosmic Diva
    OOOOOOOOOOOOOOOOOOOOOOOOOO!

    I was thinking about it all wrong. Does anyone know where I can find more info about creating a window and writting to it? I already checked the links, but I may have missed it.

    This is a separate question. I started making this progrram in visual basic 5. It has 4 forms. 2 are what the user will work with and the other 2 were dialong boxes. My question is if I were to write this program in C++ and make each form a separate C++ file, do I have to include the windows header in all the C++ files, or just one? I plan to write the whole thing in 1 window since it's small anyway, but I was just wondering.
    Jeez....read the links I gave earlier......if you arent prepared to read to get the info then dont bother going any further.......

  11. #11
    Registered User Cosmic Diva's Avatar
    Join Date
    Mar 2002
    Posts
    37

    Angry

    I just asked a question. If you don't want to answer it, don't. I'm sorry if I haven't been programming as long as you Mr. smarty pants. Considering that I'm a beginner and don't know what I'm looking at it's easy for me to miss some coding.
    "A computer is this amazingly fast.....but stupid machine." - C++ for dummies

  12. #12
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Originally posted by Cosmic Diva
    I just asked a question. If you don't want to answer it, don't. I'm sorry if I haven't been programming as long as you Mr. smarty pants. Considering that I'm a beginner and don't know what I'm looking at it's easy for me to miss some coding.
    So read the link until you understand......both of those links offer good starting info....I wouldnt have given them if I thought they wouldnt help a beginner......There is a lot of understanding needed to create and write to a window....you need to understand messages, GDI and loads of other stuff to work with windows, and the only way to get that is to sit back and read...........

    Sorry, I didnt mean to snap......but you need to walk before running....

  13. #13
    Registered User Invincible's Avatar
    Join Date
    Feb 2002
    Posts
    210
    Mr. smarty pants
    hehe she told you

    I like her already....

    My advice is limited, but I can tell you that these are the minimum requirements for a window.

    1. an HWND variable (handle to your window)
    2. an HINSTANCE variable (handle to the instance)
    3. a WinMain function (api entry)
    4. a WNDCLASS struct (filled with your windows properties)
    5. a CreateWindow Function
    6. a ShowWindow function
    7. a GetMessage function
    8. a TranslateMessage funtion
    9. a DispatchMessage function
    0. a CALLBACK function (to handle messages)

    The Callback function is probably the most difficult concept to grasp. Basically, its a catch-all for the messages your window recieves from functions 7, 8, & 9 in a message loop .

    These functions all take a pointer to a MSG structure.

    The loop would look something like this:
    Code:
    MSG msg;
    
    while(GetMessage(&msg, NULL, 0, 0)
    {
         TranslateMessage(&msg);
         DispatchMessage(&msg)
    }
    Most confusing of all, the callback function is not declared in the main loop, instead it's declared as a long pointer to a function in your WNDCLASS struct like this:
    Code:
    WNDCLASS WndClass;
    WndClass.lpfnWndClass = WndProc;
    And here is the badboy function that does it all:
    Code:
    LRESULT CALLBACK WndProc( HWND hWnd, 
                              UINT nMsg, 
                              WPARAM wParam, 
                              LPARAM lParam);
    What makes it all so confusing? All the Types, Functions, and Stuctures have already been defined in the API. They are very complex and have been designed to be used in very specific ways. If you can understand this much then you are well on your way to programming Windows.
    "The mind, like a parachute, only functions when open."

  14. #14
    Anal comment spacer DominicTrix's Avatar
    Join Date
    Apr 2002
    Posts
    120
    Hey Cosmic, I am pretty much a total beginner to Win32 too (as of this spring holiday). It took a while adjusting to the different concepts but sitting down and working through the Forgers tutorial (and several others) put me on the right track. You just have to sit through it for a several hours (or more) until you get the hang of it.

    You can also download the documentation for most of the API functions etc. here:

    CodingZone Win32 downloads
    (you want the "Win API reference file", you may have to sign up as a member before you download).

    Hope this helps

    dt

Popular pages Recent additions subscribe to a feed

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