Thread: Getting a button to perform a function

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    40

    Getting a button to perform a function

    Ok so I've created a windows program, it has a main window and two child windows, one of which is a button. What I want to do is for the button to run a function, and then display the results of that funtion in the other child window. (It will be some text just)

    So basically what I'm looking for help with is creating a window that can hold text, and a button that runs a function. I think I've worked out the how to get the button to run a function, but I'm unsure how to link it all up.

    Code:
        switch(msg)
        {
            case BN_CLICKED
                function();
            break;
    I hope that including this in the message loop will get the button to run a function. But I just don't know how to make that function print text to the other window.

  2. #2
    Registered User
    Join Date
    Mar 2005
    Location
    Juneda
    Posts
    291
    By default a button click event sends a WM_COMMAND message to the button parent's window and the button identifier as the loworder value of wParam; you just have to search for that message and check if it comes from the desired button:

    Code:
    switch(msg)
        {
        case WM_COMMAND:
            {
            switch(LOWORD(wParam))
                {
                case YOURBUTTONID:
                    {
                    some_function();
                    MessageBox(window_handler,text_to_display,title,MB_OK);
                    }
                break;
                }
            }
        break;
        }
    MessageBox is the "...window that can hold text...", take a look to any win32 manual to see extra info about that function; the way you perform your function and catch its return I think is not related to win32 programming, just the language you're using.

    Hope that helps

  3. #3
    Registered User
    Join Date
    Dec 2009
    Posts
    40
    Ok yes that does help a lot. I'm very tired today and struggling to get this done, thats why I came to ask for help.

    Is messagebox not a pop up window? What I am looking to do is have a window that does not pop up separately. But contains the text, and will change to the text that the function produces each time the button is pressed. Sorry if this is poorly explained.

  4. #4
    Registered User
    Join Date
    Dec 2009
    Posts
    40
    Ok so I've just discovered the DrawText function, I'm going to play around with that some to see if I can get it to work with the window I want Sorry for the silly questions, I've had to jump in at the deep end and try and learn the hard way.

  5. #5
    Registered User
    Join Date
    Mar 2005
    Location
    Juneda
    Posts
    291
    Yes MessageBox is a popup, I though you where searching for that.

    If you only want to display some text on any part of your window you can draw it by the way you have said: DrawText, TextOut, TabbedTextOut, ExtTextOut. Maybe you can combine that with an ownerdraw control (a static ownerdraw, i.e.), or maybe you want to create your own control entirely (custom control); or if the button controls some state of the program you can show it on a status bar as well.

    The easiest way it can be saving a part of your window for displaying the text: just define an area (RECT) that will be the display area, and only refresh (InvalidateRect) that part erasing the background (to delete the previous text) and redraw (DrawText) with the new text (also you can add edges, colors, etc).

    Niara

  6. #6
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    I use static controls or read only edit controls to display simple text info like this.

    Code:
    //button's BN_CLICKED handler
    
    //clear previous result
    SetDlgItemText(hWnd, IDC_RESULT_EDIT, "Processing....");
    
    //do processing and report success/failure
    if(SomeFunction() == SUCCESS)
    {
              SetDlgItemText(hWnd, IDC_RESULT_EDIT, "Success!");
    }
    else SetDlgItemText(hWnd, IDC_RESULT_EDIT, "Failure!");
    Quote Originally Posted by Niara View Post
    By default a button click event sends a WM_COMMAND message to the button parent's window and the button identifier as the loworder value of wParam; you just have to search for that message and check if it comes from the desired button:
    You should also check the TYPE of incomming msg is a BN_CLICKED (not one of the other button msgs send thru WM_COMMAND).

    ie (HIWORD(wParam) == BN_CLICKED)

    For example, you probably do not want to run that code if a user is using the TAB key to select a control / cycle thru the controls (generatating both a BN_SETFOCUS msg and a BN_KILLFOCUS msg).
    Last edited by novacain; 04-24-2010 at 11:22 PM.
    "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

  7. #7
    Registered User
    Join Date
    Mar 2005
    Location
    Juneda
    Posts
    291
    You should also check the TYPE of incomming msg is a BN_CLICKED (not one of the other button msgs send thru WM_COMMAND).
    Yes you have reason, but if you setup the button without BS_NOTIFY style you won't get any of those messages (focus, paint, ...), only the click event; I was assuming that case.

    Niara

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  2. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 07:18 AM
  3. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 09:10 AM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM