Thread: Controls

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    37

    Controls

    Hello there..I need some help with controls in WIN APIs..Actually I just began programming in windows for an assignment and am not used to it. My problem is as follows:




    I need to to display a string to a textbox when a button is ...So I used this piece of code in the "switch(Message)" :

    Code:
    case WM_COMMAND:
    			switch(LOWORD(wParam))
    			{
    				case IDC_ADD:
    				{
    
    			SetDlgItemText(hwnd, IDC_TEXT, "This is a string");
    			
    }break;
    }
    Where IDC_... is defined in a header file and used in a resource file as follows:
    Code:
    IDD_MAIN DIALOG DISCARDABLE  0, 0, 207, 156
    STYLE DS_MODALFRAME | DS_CENTER | WS_POPUP | WS_CAPTION | WS_SYSMENU
    CAPTION "Controls One"
    FONT 8, "MS Sans Serif"
    BEGIN
        LTEXT           "Add",IDC_STATIC,7,10,14,8
        EDITTEXT        IDC_TEXT,25,7,120,14,ES_AUTOHSCROLL
        EDITTEXT        IDC_NUMBER,150,7,21,14,ES_NUMBER
        LTEXT           "times.",IDC_STATIC,177,10,23,8
        LISTBOX         IDC_LIST,7,25,138,106,LBS_NOINTEGRALHEIGHT | 
                        LBS_EXTENDEDSEL | WS_VSCROLL | WS_TABSTOP
        PUSHBUTTON      "&Add",IDC_ADD,150,30,50,14
        PUSHBUTTON      "&Remove",IDC_REMOVE,150,47,50,14
        PUSHBUTTON      "&Clear",IDC_CLEAR,150,63,50,14
        LTEXT           "This item was added",IDC_STATIC,7,141,66,8
        CTEXT           "-",IDC_SHOWCOUNT,77,141,32,8
        LTEXT           "times",IDC_STATIC,114,141,17,8
    END
    BUT the project wont compile ..Displaying the error that STYLE DS_MODALFRAME is wrong.. So I tried giving an id for my button in the following code(discarding the definition in the resource file and in the header for the controls):

    Code:
            //create a default push button
            CreateWindowEx(0,                                      //more or 'extended' styles
                           TEXT("BUTTON"),                         //'class' of control to create
                           TEXT("Roll Dice"),            //the control caption
                           WS_CHILD|WS_VISIBLE|BS_DEFPUSHBUTTON,   //control style: how it looks
                           500,                                     //control position: left
                           250,                                     //control position: top
                           100,                                    //control width
                           30,                                     //control height
                           hwnd,                                   //parent window handle
                           NULL,                                   //control's ID
                           g_hInst,                                //application instance
                           NULL);

    Now the project compiles but the button dont work....CAN ANYONE PLZ HELP ME OUT??



    Compiler : DEV C++
    OS: WIN XP
    Source code obtained from: theForger's Win32 API Tutorial & http://www.foosyerdoos.fsnet.co.uk/

  2. #2
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,273
    Superdooper quick fix...
    Change
    Code:
    NULL,                                   //control's ID
    to
    Code:
    (HMENU)0x69,                                   //control's ID
    Then change
    Code:
    case IDC_ADD:
    to
    Code:
    case 0x69:
    That should make it work, although it's not terribly good form. Use a MessageBox to test.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    37
    Ooohh Thx dude...u saved me hours of search over the net and hopeless compiling...n I raally badly needed time to complete the work..thx again...I guess I might define the IDs in the header.

  4. #4
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    And WHY the solution works....

    or I find debugging easier when I can read IDC_ADD rather and 0x69.


    To use dialogs and controls.

    You should have a resource.h file

    It will have all your ID values in it

    #define IDD_DIALOG1 132

    You will also have a .rc file containing the definitions (sizes, styles ect) of the dialogs / controls.

    If you use the WS_CHILD style then the HMENU paramater becomes the ID of the control.




    So I would have

    Added to the resource.h file and included it in the same file as the callback.
    #define IDC_ADD 40001//define the ADD buttons ID number

    Change the HMENU paramater in your create to
    (HMENU)IDC_ADD //tell the button its ID is 40001


    Also....
    Check that the the 'event' that caused the message to be sent [HWORD(wParam)] was the button being clicked/pressed (not redrawn, resized ect).

    case IDC_ADD:
    if(HIWORD(wParam)==BN_CLICKED)//Button Notification CLICKED

    This is not needed so much with buttons but is required for more complex controls such as listviews, treeviews or datetime pickers.

    MAKEINTRESOURCE() may be required in some functions

    HCURSOR hHourGlass=LoadCursor(NULL,MAKEINTRESOURCE(IDC_WAI T));
    Last edited by novacain; 04-26-2007 at 02:40 AM.
    "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
    Registered User
    Join Date
    Apr 2007
    Posts
    37
    Yes, thats exactly What I've done...Defined the button ID number in the headr file and use the name instead when assigning the ID to the control...But still I cannot create the definitions (sizes, styles ect) of the dialogs / controls in the .rc file..The compiler returns a syntax error for STYLE...So thats why I usxed this code in the .c file
    code:
    Code:
            //create a default push button
            CreateWindowEx(0,                                      //more or 'extended' styles
                           TEXT("BUTTON"),                         //'class' of control to create
                           TEXT("Roll Dice"),            //the control caption
                           WS_CHILD|WS_VISIBLE|BS_DEFPUSHBUTTON,   //control style: how it looks
                           500,                                     //control position: left
                           250,                                     //control position: top
                           100,                                    //control width
                           30,                                     //control height
                           hwnd,                                  //parent window handle
                           (HMENU)IDC_ROLL,               //control's ID
                           g_hInst,                               //application instance
                           NULL);
    its working fine..but still I want to know the other way..that is that of defining the styles in the .rc file
    Last edited by Sober; 04-26-2007 at 09:13 AM.

  6. #6
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    >>defining the styles in the .rc file<<

    Those constants are defined elsewhere in the windows headers - including windows.h in your resource script should make sure they are properly defined.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  7. #7
    Registered User
    Join Date
    Apr 2007
    Posts
    37
    Okay. Now am having quite an issue with a button. I had to change a button's caption once it has been clicked..Thats fine.
    On button click a function is accessed and processing is done
    What I need to do is to use the same button to stop processing.....I need to use something like keydown but I have not been able to get to know whta it should be..

    In fact the processing is in a 'while loop' so I wanted to place an if statement in there to detect the button's click and change a state variable so that the loops stops...can anyone plz guide my search?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. confused about adding controls to main window
    By terracota in forum Windows Programming
    Replies: 4
    Last Post: 11-24-2004, 12:35 PM
  2. Controls
    By osal in forum Windows Programming
    Replies: 7
    Last Post: 06-11-2004, 10:38 AM
  3. Subclassing controls
    By filler_bunny in forum Windows Programming
    Replies: 3
    Last Post: 04-28-2004, 05:43 PM
  4. I need help disabling Keyboard and Mouse input on Edit controls
    By Templario in forum Windows Programming
    Replies: 4
    Last Post: 01-07-2003, 12:59 AM
  5. MFC Controls and Thread Safety :: MFC
    By kuphryn in forum Windows Programming
    Replies: 0
    Last Post: 12-06-2002, 11:36 AM