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. How to add a message handler function to a control at run-time?

How to add a message handler function to a control at run-time?

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++helptutorial
2 Posts 2 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.
  • M Offline
    M Offline
    misterbear
    wrote on last edited by
    #1

    hello, I use MFC and new I need to create controls (buttons) on-the-fly and add message handlers to them for the BN_CLICKED event at the time they are created. I see how the messagemap functionality can add handlers for the controls that I already have on my form, but what about dynamically created. I do not wish to create an excessive amount of controls and just hide the ones that are inappropriate at the time.. (cause: where di I set the limit as to what is a safe amount of controls then..) thankful for any help I can get.

    I 1 Reply Last reply
    0
    • M misterbear

      hello, I use MFC and new I need to create controls (buttons) on-the-fly and add message handlers to them for the BN_CLICKED event at the time they are created. I see how the messagemap functionality can add handlers for the controls that I already have on my form, but what about dynamically created. I do not wish to create an excessive amount of controls and just hide the ones that are inappropriate at the time.. (cause: where di I set the limit as to what is a safe amount of controls then..) thankful for any help I can get.

      I Offline
      I Offline
      Ivan Cachicatari
      wrote on last edited by
      #2

      Hi, You can intercept the button down message (WM_LBUTTONDOWN) or any button in the PreTranslateMessage(MSG* pMsg):

      BOOL CYourDialog::PreTranslateMessage(MSG* pMsg)
      {
      if(pMsg->message == WM_LBUTTONDOWN)
      {
      CWnd *wnd = CWnd::FromHandle(pMsg->hwnd);
      int ID = wnd->GetDlgCtrlID();
      if(ID == IDC_OWNBUTTON1)
      {
      MessageBox("IDC_OWNBUTTON1");
      return TRUE;
      }
      // ....
      if(ID == IDC_OWNBUTTON_N)
      {
      MessageBox("IDC_OWNBUTTON_N");
      return TRUE;
      }
      }
      return CDialog::PreTranslateMessage(pMsg);
      }

      The IDC_OWNBUTTON1 ... IDC_OWNBUTTON_N is an custom control id's Ivan Cachicatari www.latindevelopers.com

      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