Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Button Creation, Handling the Click event

Button Creation, Handling the Click event

Scheduled Pinned Locked Moved C / C++ / MFC
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jkirkerx
    wrote on last edited by
    #1

    I searched for hours, but everything was C# or C++ mfc. I created a button, using CreateWindow, I found it on a tutorial site on how to make window controls, but there no example on how to handle the click. So I wrote this, am I missing something like a SendMessage to register the button?

    HWND bt_IIS_Install;
    bt_IIS_Exit = CreateWindow(L"button", L"Exit", WS_CHILD,
    winWidth / 2 - 200,
    winHeight - 90,
    180, 32, h_IIS_Install_Client, NULL, GetModuleHandle(NULL), 0
    );
    ShowWindow( bt_IIS_Exit, SW_SHOW);

    I found this, that handles a dialog response, so I'm guessing that this is how I capture the button clicks, but I need to assign the defintion to the button.

    case WM_COMMAND:
    if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
    {
    //EndDialog(hDlg, LOWORD(wParam));
    //return (INT_PTR)TRUE;
    }

            break;
    

    Am I on the right track here?

    N _ 2 Replies Last reply
    0
    • J jkirkerx

      I searched for hours, but everything was C# or C++ mfc. I created a button, using CreateWindow, I found it on a tutorial site on how to make window controls, but there no example on how to handle the click. So I wrote this, am I missing something like a SendMessage to register the button?

      HWND bt_IIS_Install;
      bt_IIS_Exit = CreateWindow(L"button", L"Exit", WS_CHILD,
      winWidth / 2 - 200,
      winHeight - 90,
      180, 32, h_IIS_Install_Client, NULL, GetModuleHandle(NULL), 0
      );
      ShowWindow( bt_IIS_Exit, SW_SHOW);

      I found this, that handles a dialog response, so I'm guessing that this is how I capture the button clicks, but I need to assign the defintion to the button.

      case WM_COMMAND:
      if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
      {
      //EndDialog(hDlg, LOWORD(wParam));
      //return (INT_PTR)TRUE;
      }

              break;
      

      Am I on the right track here?

      N Offline
      N Offline
      Niklas L
      wrote on last edited by
      #2

      You should use the hMenu parameter to specify the control id of your button (IDOK or IDCANCEL in your case) From MSDN: hMenu [in, optional] A handle to a menu, or specifies a child-window identifier depending on the window style. For an overlapped or pop-up window, hMenu identifies the menu to be used with the window; it can be NULL if the class menu is to be used. For a child window, hMenu specifies the child-window identifier, an integer value used by a dialog box control to notify its parent about events. The application determines the child-window identifier; it must be unique for all child windows with the same parent window.

      J 2 Replies Last reply
      0
      • N Niklas L

        You should use the hMenu parameter to specify the control id of your button (IDOK or IDCANCEL in your case) From MSDN: hMenu [in, optional] A handle to a menu, or specifies a child-window identifier depending on the window style. For an overlapped or pop-up window, hMenu identifies the menu to be used with the window; it can be NULL if the class menu is to be used. For a child window, hMenu specifies the child-window identifier, an integer value used by a dialog box control to notify its parent about events. The application determines the child-window identifier; it must be unique for all child windows with the same parent window.

        J Offline
        J Offline
        jkirkerx
        wrote on last edited by
        #3

        Thanks for the quick response, who works on Sunday. I understand, I just need to turn the gears awhile to produce something that works.

        1 Reply Last reply
        0
        • N Niklas L

          You should use the hMenu parameter to specify the control id of your button (IDOK or IDCANCEL in your case) From MSDN: hMenu [in, optional] A handle to a menu, or specifies a child-window identifier depending on the window style. For an overlapped or pop-up window, hMenu identifies the menu to be used with the window; it can be NULL if the class menu is to be used. For a child window, hMenu specifies the child-window identifier, an integer value used by a dialog box control to notify its parent about events. The application determines the child-window identifier; it must be unique for all child windows with the same parent window.

          J Offline
          J Offline
          jkirkerx
          wrote on last edited by
          #4

          Well that took awhile to figure out. I created a menu in my resource file, and assigned the number to it in the other resource file. Then added the single hMenu item to the create window so WM_COMMAND can pick it up as the wParam. Thanks for the pointer to the huge lesson that I really learned from.

          HWND bt_IIS_Install;
          bt_IIS_Exit = CreateWindow(L"BUTTON", L"Exit", WS_CHILD,
          winWidth / 2 - 200,
          winHeight - 90,
          180, 32, hIIS_Install, (HMENU)IDC_IIS_WS_EXIT, GetModuleHandle(NULL), 0
          );
          ShowWindow( bt_IIS_Exit, SW_SHOW);

          1 Reply Last reply
          0
          • J jkirkerx

            I searched for hours, but everything was C# or C++ mfc. I created a button, using CreateWindow, I found it on a tutorial site on how to make window controls, but there no example on how to handle the click. So I wrote this, am I missing something like a SendMessage to register the button?

            HWND bt_IIS_Install;
            bt_IIS_Exit = CreateWindow(L"button", L"Exit", WS_CHILD,
            winWidth / 2 - 200,
            winHeight - 90,
            180, 32, h_IIS_Install_Client, NULL, GetModuleHandle(NULL), 0
            );
            ShowWindow( bt_IIS_Exit, SW_SHOW);

            I found this, that handles a dialog response, so I'm guessing that this is how I capture the button clicks, but I need to assign the defintion to the button.

            case WM_COMMAND:
            if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
            {
            //EndDialog(hDlg, LOWORD(wParam));
            //return (INT_PTR)TRUE;
            }

                    break;
            

            Am I on the right track here?

            _ Offline
            _ Offline
            _Superman_
            wrote on last edited by
            #5

            I did this in one of my articles - Mousey! Roll Over and Park[^] The settings dialog and all its controls are created using CreateWindow.

            «_Superman_»  _I love work. It gives me something to do between weekends.

            _Microsoft MVP (Visual C++)

            Polymorphism in C

            J 1 Reply Last reply
            0
            • _ _Superman_

              I did this in one of my articles - Mousey! Roll Over and Park[^] The settings dialog and all its controls are created using CreateWindow.

              «_Superman_»  _I love work. It gives me something to do between weekends.

              _Microsoft MVP (Visual C++)

              Polymorphism in C

              J Offline
              J Offline
              jkirkerx
              wrote on last edited by
              #6

              Great article. So does that mean that I created my buttons correctly? Writing an app in c++ has been my goal for a long time. Finally had a reason to pursue the next step. I don't want to put anymore visual basic programs out there any more, too much trouble to package and deploy them. I've got this nice eCommerce program, but I can't anybody to use it because it's not super easy to setup and deploy. So this program will make it easier to start using it. The goal is to use a windows program install the web server, create the default web site, import database, create email templates and so forth. Thanks for looking at my post! jkirkerx

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups