Dynamic button controls [WIN32 API/c++/VS 2010]

This is a discussion on Dynamic button controls [WIN32 API/c++/VS 2010] within the Windows Programming forums, part of the Platform Specific Boards category; I'm developing an application that finds devices on a network and creates a window in the main window for each ...

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    32

    Dynamic button controls [WIN32 API/c++/VS 2010]

    I'm developing an application that finds devices on a network and creates a window in the main window for each device and their processor cores. I want the core buttons to toggle the number of cores being used on a given processor.

    To create these windows, I detect the number of processors in the system, then create a new class instance in a vector. I do the same for the cores. In each processor class, there is a vector for each core and a vector for the buttons associated with it.

    My problem is that while I have the buttons being created, I cannot figure out how to assign controls for them in the case switch. How would I define the commands for these buttons? I am assuming that I don't know the number of processors or machines, and I am creating these buttons when declaring a new class for the found processor.

  2. #2
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,859
    When you create the buttons you will supply an ID as the HMENU param.

    The WM_COMMAND msg will contain this ID as the loword of the wparam, so you should be able to match the BN_CLICKED by control IDs.
    "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

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    32
    novacain, would I then declare the button like this:

    Code:
    HWND btn = CreateWindowExW(NULL,
            L"BUTTON",
            L"SET MANUAL",
            WS_VISIBLE|WS_CHILD|BS_DEFPUSHBUTTON,
            25,//x
            200,//y
            150,//width
            24,//height
            mainSysOpt,
            **ID GOES HERE**,
            GetModuleHandle(NULL),
            NULL);
    or do I have to do this in the header file where I declare the other modules?

    Also, I'm still having a hard time seeing the link between the button and the processor/core. Could you please clarify?

  4. #4
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,859
    Yes, you may need to cast the ID to a HMENU (ie (HMENU)IDC_CORE1,)

    You will need to add a method to check if the ID of in the WM_COMMAND message is from one of your 'core' buttons.

    Please note this is coded from memory, so not in any way tested....
    Code:
    CASE WM_COMMAND:
    int ControlID = LOWORD(wParam);
    int MessageType = HIWORD(wParam);
    int Processed = -1;
    int Error = -1;
    if(MessageType == BN_CLICKED)
    {
         //check for CORE BUTTON ID
         Error = CheckCoreID(ControlID, &Processed);
         //check to see if we processed the button click (ie was one of the created CORE buttons)
         if(Processed == 1 && Error == 0)
         {
                return 0;//show we processed this button click
         }
         //else let the message fall into the normal COMMAND switch 
         SWITCH (ControlID)
         {
               CASE IDC_ANOTHER_CTRL:
    
    //etc
    
    
    //CORE BUTTON HANDLER
    //returns 0 for success, non zero on error
    //sets ProcResult = 1 if we find CntrlId is one of the created CORE buttons
     
    int CheckCoreID(int CntrlId, int *ProcResult)
    {
         //run through the IDs in the CORE vector looking for the button ID
         //if we find the ID
              //process 
              //set the 'ProcResult' variable to show we have processed the button click
    
        return 0;//else return non zero on error
    }

    Another way is to assume a maximum number of cores per CPU (ie 8) and design a 'standard' or default dialog / window in the resource editor with this max number of buttons (ie create a dialog with 8 buttons).

    Remove the WS_VISIBLE style from these buttons (so they do not appear) and add the visible style to the required buttons once you know how many cores a given CPU has (using ShowWindow() ).

    Your callback would have a WM_COMMAND handler for each of these 'core' buttons (as with 'normal' buttons / controls), but only the visible ones will be able to generate BN_CLICKED messages.

    You would then match the ID in the CORE vector to determine the required processing.
    "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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Child Window Text/Buttons Missing [WIN32 API/C++/VS 2010]
    By William Putnam in forum Windows Programming
    Replies: 3
    Last Post: 07-22-2013, 10:53 PM
  2. Child Window Text/Buttons Missing [WIN32 API/C++/VS 2010]
    By William Putnam in forum C++ Programming
    Replies: 0
    Last Post: 07-22-2013, 12:47 PM
  3. WIN32 API Controls used with Resource
    By parad0x13 in forum C++ Programming
    Replies: 0
    Last Post: 07-19-2008, 03:05 PM
  4. c Win32 Programming Controls
    By konvoy_dev_team in forum Windows Programming
    Replies: 1
    Last Post: 03-08-2003, 09:08 PM
  5. spin controls in Win32
    By loobian in forum Windows Programming
    Replies: 4
    Last Post: 10-21-2001, 08:49 PM

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