input/output

This is a discussion on input/output within the Windows Programming forums, part of the Platform Specific Boards category; Hi, At first, I know I'm newbe/dumbuser etc, so there’s no need to tell me that . Well, by problem ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    19

    input/output

    Hi,
    At first, I know I'm newbe/dumbuser etc, so there’s no need to tell me that . Well, by problem is: I am looking for a simple tutorial of first taking users input and later displaying it (With Win32 forms) I have only found one tutorial about that, but it was to mixed up...I would like just a tutorial of taking input from one field and then displaying it.
    Thanx in advance.

  2. #2
    Registered User
    Join Date
    Aug 2004
    Posts
    77
    So what you are looking for is a program that accepts users intput (say from a text box) then echo's that input back out (say on a label)?

    Assuming thats the case, its not really enough info. I mean there are a tone of ways to do that. Are you planning on using MFC, the original C API, some non-standard one like QT or GTK?

    If you want the straight C API I can throw something together easily enough.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    19
    Well, I'm more or less (more) a newbe, so I rather much don't care how it's down as long as it is =) Well, probably the easyest way would be the best.

  4. #4
    fuh
    fuh is offline
    Attack hamster fuh's Avatar
    Join Date
    Dec 2002
    Posts
    176
    I think he just wants a basic tutorial that will allow him to take input and record it as output. If so, I can recommend a few tutorials:

    EDIT: I took too long gathering links and both of you posted before me.
    Stupid things pop singers say

    "I get to go to lots of overseas places, like Canada."
    - Britney Spears

    "I love what you've done with the place!"
    -Jessica Simpson upon meeting the Secretary of Interior during tour of the White House

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    19
    Thanx for the links, but there was not the kind of tutorial I'd want...I couldnt find one where you could print the text user has inserted by form but one of them, but that's the onw I talked about in my first post.

  6. #6
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,265
    Well, I assume that you already know how to create the form. We can go through this step by step:

    First we need to get the text the user entered. To do this, we first need a handle to the text field window.

    Code:
    HWND hwndEdit;
    hwndEdit = GetDlgItem(g_hwndDlg,IDC_MY_EDIT_BOX);
    if(!hwndEdit) {
      /* We failed! */
    }
    note: In the above code g_hwndDlg is the HWND of the dialog window which the text box is on, and IDC_MY_EDIT_BOX is the control ID of the edit box.

    Now we need to get the text from that edit box:
    Code:
    char buff[128];
    SendMessage(hwndEdit,WM_GETTEXT,(WPARAM)sizeof(buff),(LPARAM)buff);
    Lastly, we just need to print out what was entered:
    Code:
    MessageBox(NULL,buff,"This is what was entered",MB_OK);
    This should be enough to get you started. Let me know if you are still having trouble.

  7. #7
    Registered User
    Join Date
    Aug 2004
    Posts
    77
    Here's some stuff to think over, hopefully I'll get enough comments for it to make sense. This program creates a window with two text boxes, anything typed in the top text box will be copied into the bottom text box.

    Code:
    //Windows.h is a header file you will need for virtually all Windows
    //programs.   Be sure to always include it
    #include<windows.h>
    
    //These defines are used later to identify the child windows
    #define CWE_OUTTEXT    1
    #define CWE_INTEXT    2
    
    //Prototype for the function that will recieve all messages for the
    //windows
    LRESULT CALLBACK WindowFunc(HWND, UINT, WPARAM, LPARAM);
    
    //Many windows API's want to know the instance of the running
    //application, for convenince I'm saving it in a global variable
    HINSTANCE Instance;
    
    //Windows does not start with the standard main(), instead it
    //uses WinMain() with this prototype, just like main you don't
    //need to prototype it:
    int WINAPI WinMain(HINSTANCE hThisInst, HINSTANCE hPrevInst, LPSTR lpszArgs, int nWinMode)
    {
        MSG msg;
        WNDCLASS wcl;
        HWND Handle;
        char Caption[512];
        char ClassName[512];
    
        //Setting some temporary variables to hold the class and
        //caption names for the window (more on these later)
        Caption[0] = '\0';
        ClassName[0] = '\0';
        strcat(Caption, "Windows Application Shell");
        strcat(ClassName, "WinAppShell");
    
        //Copying the instance value (its passd as a parameter to
        //WinMain()
        Instance = hThisInst;
    
        //All windows are assigned to a particular windows class
        //The class holds many values that describe the window
        //such as the background color (wcl.hbrBackground),
        //the icon (wcl.hIcon) and the look of the mouse cursor
        //(wcl.hCursor)
        wcl.hInstance = hThisInst;
        wcl.lpszClassName = ClassName;
        wcl.lpfnWndProc = WindowFunc;
        wcl.style = 0;
        wcl.hIcon = LoadIcon(NULL, IDI_APPLICATION);
        wcl.hCursor = LoadCursor(NULL, IDC_ARROW);
        wcl.lpszMenuName = NULL;
        wcl.cbClsExtra = 0;
        wcl.cbWndExtra = 0;
        wcl.hbrBackground = (HBRUSH) GetStockObject(LTGRAY_BRUSH);
    
        //Attempt to register the window class for use in creating
        //the window
        if(RegisterClass(&wcl) == 0)
        {
            return 0;
        }
    
        //If the class was successfully registered, we can create
        //the main window.
        Handle = CreateWindow(ClassName, Caption, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 160, 155, HWND_DESKTOP, NULL, hThisInst, NULL);
    
        //The main window needs to be initialized and drawn on the
        //screen, these function calls take care of that
        ShowWindow(Handle, nWinMode);
        UpdateWindow(Handle);
    
        //So that the window can recieve messages (messages are how
        //all user input and other things are reported to the window)
        //we need to establish a message loop.
        while(GetMessage(&msg, NULL, 0, 0))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    
        //When the message loop ends, the window has closed and the
        //program can end
        return msg.wParam;
    }
    
    //The class defines this function as being the recipient of all
    //of the window's messages (wcl.lpfnWndProc).  All of the message
    //details are passed as parameters to this function
    LRESULT CALLBACK WindowFunc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
        char String[512];
    
        //The message variable holds the type of message passed
        //Its common to put it in a switch statement to handle all
        //of the events passed
        switch(message)
        {
            case (WM_CREATE) :
            {//The WM_CREATE message is passed when the window
            //is created, but not yet placed on the screen
            //At this point we are going to create all of the
            //child windows.  You can see that the defines
            //made above are passed as arguments, that is to
            //assign those integers as how we would like to
            //identify the specific child windows
                CreateWindow("edit", "", WS_CHILD | WS_BORDER | WS_VISIBLE | ES_LEFT, 0, 0, 150, 50, hwnd, (HMENU)CWE_INTEXT, Instance, NULL);
                CreateWindow("edit", "", WS_CHILD | WS_BORDER | WS_VISIBLE | ES_LEFT | ES_READONLY, 0, 75, 150, 50, hwnd, (HMENU)CWE_OUTTEXT, Instance, NULL);
                break;
            }
            case (WM_COMMAND) :
            {//The WM_COMMAND message is a message from one of the child
            //windows wParam has information on which child and what it
            //wants to tell the main window
                if (LOWORD(wParam) == CWE_INTEXT)
                {//wParam holds two values, we can split the valuesout with
                //the LOWORD() and HIWORD() macros.  The LOWORD() value has
                //the ID of the child that sent the message
                    if (HIWORD(wParam) == EN_CHANGE)
                    {//The HIWORD() has the type of message that was sent
                        SendDlgItemMessage(hwnd, CWE_INTEXT, WM_GETTEXT, 512, (LPARAM)String);
                        SetDlgItemText(hwnd, CWE_OUTTEXT, String);
                    }
                }
                break;
            }
            case (WM_DESTROY) :
            {//The WM_DESTROY message is sent when the window is being
            //destroyed by windows
                //PostQuitMessage() sends a signal to the message loop that
                //the window is closing and the loop can close to allow
                //the application to quit
                PostQuitMessage(0);
                break;
            }
            default :
            {//Windows sends many many types of events, any that we don't
            //choose to process can be handled by a default routine
                return DefWindowProc(hwnd, message, wParam, lParam);
            }
        }
        return 0;
    }
    Here's some background information, since Windows programs are far more complicated than command line programs. Generally how the WinMain() function works is to establish any top level windows that will exist by creating the window classes needed and then the windows themselves. Then the WinMain() function enters a message loop where it gets message from the operating system with GetMessage() and hands them off to the windows callback function with DispatchMessage(). Thats its main job, it can handle other tasks but they are probably beyond the beginner stage so nevermind them for now.

    The callback function is responsible for handling any and all messages passed to the window. It does not need to explicitly handle each one, it can use the DefWindowProc() function to deal with any messages that aren't important to the window. I only handled a select few of the messages that will be passed, there are many more for example WM_CHAR for key presses, WM_LBUTTONDOWN and WM_RBUTTONDOWN for mouse clicks.

    You may notice that when I created the main window a special windows class was needed, and you can see the class named passed as the first parameter to CreateWindow(). However, when I created the child windows (the two text boxes) I didn't create a class for them. Thats because windows keeps a list of common classes that are used for child windows. In this case I used "edit" for text boxes, there is also "button" for buttons and "static" for labels. These classes take care of all of teh details of those window types, including the callback functions.

    In the WM_COMMAND message, normally you would look at LOWORD(wParam) and HIWORD(wParam) in switch statements as well, sicne I was only interested in one value for each of those I just used if statements to save space. Any time text boxes have their values changed they send a EN_CHANGE message to their parent window (in this case its our top level window). So I'm looking for that event to know when to copy the text to the second textbox. Different child window classes will report different events, you'll need to read up on each type to get a listing of the events they send.

    You'll see that I get the text from the top text box with SendDlgItemMessage(). It sounds kind of odd, but to get information from a child window you actually send that child a message to request the information. In this case I set WM_GETTEXT to retrieve the text of the edit box. Most of these common proceures have nice wrapper functions that are easier to read, for example when I set the text instead of sending a WM_SETTEXT message I use the wrapper function SetDlgItemText().

    The WM_DESTROY message is sent when the user requests the window to close, in this case its sent when you click on the top right X button to close the window. In this event any cleanup the program needs should be done, free()ing memory and releasing any other resources before closing down. Then you call the PostQuitMessage() to end the message loop and allow the program to close.

    As you can see events come from user input (the user clicks the X to close the window and sends teh WM_DESTROY message0 as well as from inside the program (the child window sends a WM_COMMAND when it needs to talk to the parent).

    Not sure how well that covers things, hope it helps.

  8. #8
    Registered User
    Join Date
    Aug 2004
    Posts
    77
    As an FYI, thats all using the straight C Windows API, no MFC or anything like that. So it may be a pretty old method of doing things, to be honest I'm not sure if many people still do that stuff anymore.

    I also left out a lot of detail, I'll admit to being lazy there and not wanting to type out the first couple chapters to a book. If you have questions about it I'll de my best to answer. Most of the function calls I use can be found on the MSDN site (msdn.microsoft.com), you can get full descriptions of the arguments passed to them if you are interested in all that.

  9. #9
    Registered User Sake's Avatar
    Join Date
    Jan 2005
    Posts
    89
    >>I know I'm newbe/dumbuser etc, so there’s no need to tell me that
    This attitude serves no purpose except to make you feel worse about yourself. There's no such thing as a dumb question. Questions are the first step toward understanding. If you think of yourself as a "newbie" or a "dumb user" then you'll be cautious about asking questions for fear of looking stupid, and that's counter-productive.

    It sounds cheesy, but to make the most of yourself, you have to believe in your potential.
    Kampai!

  10. #10
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,265
    Good code Exile, and well explained too. Once suggestion though is to change:
    Code:
    while(GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    to
    Code:
    BOOL ret;
    ...
    while(ret = GetMessage(&msg, NULL, 0, 0))
    {
        if(ret == -1)
        {
            /* Handle error here */
            break;
        }
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    GetMessage() returns -1 on error, your code would ignore that.

  11. #11
    Registered User
    Join Date
    Aug 2004
    Posts
    77
    Thats a good point bithub, I have a bad habit of forgetting a lot of the error detection. In my defence most of these calls will only fail when you give them bad input. But I admit thats a weak arguement, error checking should always be done.

    Thanks for reminding me, as well as keeping the learners straight.

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    19
    Hi, thanx guys, I finally understood how to do it

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with input/output files
    By aldin176 in forum C++ Programming
    Replies: 5
    Last Post: 11-06-2006, 09:04 AM
  2. Input/Output Streams
    By mikeprogram in forum C++ Programming
    Replies: 6
    Last Post: 12-23-2005, 02:58 PM
  3. input/output
    By JCK in forum C++ Programming
    Replies: 4
    Last Post: 11-30-2002, 12:07 PM
  4. FILE input/output and ARRAY
    By poorman in forum C Programming
    Replies: 1
    Last Post: 03-05-2002, 04:17 PM
  5. Input/Output
    By PaulMelia in forum C Programming
    Replies: 3
    Last Post: 12-02-2001, 08:13 AM

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