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. Event handling for dynamically created controls

Event handling for dynamically created controls

Scheduled Pinned Locked Moved C / C++ / MFC
c++
10 Posts 4 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.
  • S Offline
    S Offline
    sanskypotov
    wrote on last edited by
    #1

    Hi, I am having the following scenario a class CMy( and is not MFC class ), and I create 3 objects a,b,c ( actually I need to create any number of objects) CMy a , b ,c now CMy has an event WM_LBUTTONDOWN, WM_LBUTTONUP I want only 1 function(event handler) OnMouseLButtonDown(), so this is called whenever I press any one of the object i.e a , b or c I am trying to avoid Subclassing. Can some tell me what should I do. Thanks, Sansky :-D God is Good, all the Time. All the Time, God is Good.

    M T 2 Replies Last reply
    0
    • S sanskypotov

      Hi, I am having the following scenario a class CMy( and is not MFC class ), and I create 3 objects a,b,c ( actually I need to create any number of objects) CMy a , b ,c now CMy has an event WM_LBUTTONDOWN, WM_LBUTTONUP I want only 1 function(event handler) OnMouseLButtonDown(), so this is called whenever I press any one of the object i.e a , b or c I am trying to avoid Subclassing. Can some tell me what should I do. Thanks, Sansky :-D God is Good, all the Time. All the Time, God is Good.

      M Offline
      M Offline
      Maxwell Chen
      wrote on last edited by
      #2

      Maybe you may try to modify my code below into what you want to...

      #include <iostream>
      using namespace std;

      class CCore
      {
      public:
      void Job(int iId) {
      // Do the job
      cout << "Hey this is button " << iId << " clicked.\n";
      // Passing info from that ID to WM_LBUTTONDOWN....
      }
      };

      class CMy
      {
      int _iId;
      static CCore _EventMachine;

      public:
      CMy(int iId) { _iId = iId; }
      void ButtonLClicked() {
      _EventMachine.Job(_iId);
      }
      };

      CCore CMy::_EventMachine;

      void main()
      {
      CMy btn[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

      btn\[2\].ButtonLClicked();
      btn\[6\].ButtonLClicked();
      

      }

      Maxwell Chen People say "No news is good news". Then, no code is good code!?

      1 Reply Last reply
      0
      • S sanskypotov

        Hi, I am having the following scenario a class CMy( and is not MFC class ), and I create 3 objects a,b,c ( actually I need to create any number of objects) CMy a , b ,c now CMy has an event WM_LBUTTONDOWN, WM_LBUTTONUP I want only 1 function(event handler) OnMouseLButtonDown(), so this is called whenever I press any one of the object i.e a , b or c I am trying to avoid Subclassing. Can some tell me what should I do. Thanks, Sansky :-D God is Good, all the Time. All the Time, God is Good.

        T Offline
        T Offline
        Tom Archer
        wrote on last edited by
        #3

        Subclassing is precisely what you need. In fact, it's what the MFC control classes do for you internally. Therefore, you don't want to use MFC, you'll need to either manually do the subclassing yourself or use another library that does it (such as MFC). Cheers, Tom Archer Author, Inside C# Author, Visual C++.NET Bible A total abstainer is one who abstains from everything but abstention, and especially from inactivity in the af

        M 1 Reply Last reply
        0
        • T Tom Archer

          Subclassing is precisely what you need. In fact, it's what the MFC control classes do for you internally. Therefore, you don't want to use MFC, you'll need to either manually do the subclassing yourself or use another library that does it (such as MFC). Cheers, Tom Archer Author, Inside C# Author, Visual C++.NET Bible A total abstainer is one who abstains from everything but abstention, and especially from inactivity in the af

          M Offline
          M Offline
          Maxwell Chen
          wrote on last edited by
          #4

          Tom Archer wrote: Subclassing is precisely what you need. Hi Tom, was my way wrong ? :confused: :confused: :confused: Maxwell Chen People say "No news is good news". Then, no code is good code!?

          N T 3 Replies Last reply
          0
          • M Maxwell Chen

            Tom Archer wrote: Subclassing is precisely what you need. Hi Tom, was my way wrong ? :confused: :confused: :confused: Maxwell Chen People say "No news is good news". Then, no code is good code!?

            N Offline
            N Offline
            Neville Franks
            wrote on last edited by
            #5

            Hi Maxwell, I would have thought you would have a base class as you've done and then have virtual OnxxxButton() function in the base class to handle the event. This could then be overriden in the derived classes as required. Neville Franks, Author of ED for Windows. www.getsoft.com

            1 Reply Last reply
            0
            • M Maxwell Chen

              Tom Archer wrote: Subclassing is precisely what you need. Hi Tom, was my way wrong ? :confused: :confused: :confused: Maxwell Chen People say "No news is good news". Then, no code is good code!?

              T Offline
              T Offline
              Tom Archer
              wrote on last edited by
              #6

              Hi Maxwell, I guess I'm just not understanding how your code is going to attach handlers to control windows messages without hooking the control's wndproc. Can you explain that a bit more? Maybe I'm just missing something obvious here. Cheers, Tom Archer Author, Inside C# Author, Visual C++.NET Bible A total abstainer is one who abstains from everything but abstention, and especially from inactivity in the af

              1 Reply Last reply
              0
              • M Maxwell Chen

                Tom Archer wrote: Subclassing is precisely what you need. Hi Tom, was my way wrong ? :confused: :confused: :confused: Maxwell Chen People say "No news is good news". Then, no code is good code!?

                T Offline
                T Offline
                Tom Archer
                wrote on last edited by
                #7

                Ok. I finally took more than a second to look at your code :) The issue is that I'm looking at it from a different level than you. You're assuming you already have the routed messages (via subclassing the control) and are providing an event machine to better handle the messaging of an array of controls. I, on the other hand, was thinking that the poster wasn't even at the point of getting the messages for the controls yet. He hasn't responded to any of the posts yet so it'll be interesting to see what he really is trying to get. By the way, with regards to your "event engine", is this something you threw together for this question or is it something more fleshed out that you could share with us (maybe in an article with examples of its use)? Cheers, Tom Archer Author, Inside C# Author, Visual C++.NET Bible A total abstainer is one who abstains from everything but abstention, and especially from inactivity in the af

                M 1 Reply Last reply
                0
                • T Tom Archer

                  Ok. I finally took more than a second to look at your code :) The issue is that I'm looking at it from a different level than you. You're assuming you already have the routed messages (via subclassing the control) and are providing an event machine to better handle the messaging of an array of controls. I, on the other hand, was thinking that the poster wasn't even at the point of getting the messages for the controls yet. He hasn't responded to any of the posts yet so it'll be interesting to see what he really is trying to get. By the way, with regards to your "event engine", is this something you threw together for this question or is it something more fleshed out that you could share with us (maybe in an article with examples of its use)? Cheers, Tom Archer Author, Inside C# Author, Visual C++.NET Bible A total abstainer is one who abstains from everything but abstention, and especially from inactivity in the af

                  M Offline
                  M Offline
                  Maxwell Chen
                  wrote on last edited by
                  #8

                  CMy is non-MFC ? Ok., forget it, I might be wrong...... Maxwell Chen People say "No news is good news". Then, no code is good code!?

                  T 1 Reply Last reply
                  0
                  • M Maxwell Chen

                    CMy is non-MFC ? Ok., forget it, I might be wrong...... Maxwell Chen People say "No news is good news". Then, no code is good code!?

                    T Offline
                    T Offline
                    Tom Archer
                    wrote on last edited by
                    #9

                    Exactly. He wants a non-MFC solution, which was my first question as to how you were getting the messages to begin with. Your solution looks quite intriguing, but assumes that the messages are already being received in some fashion. Cheers, Tom Archer Author, Inside C# Author, Visual C++.NET Bible A total abstainer is one who abstains from everything but abstention, and especially from inactivity in the af

                    S 1 Reply Last reply
                    0
                    • T Tom Archer

                      Exactly. He wants a non-MFC solution, which was my first question as to how you were getting the messages to begin with. Your solution looks quite intriguing, but assumes that the messages are already being received in some fashion. Cheers, Tom Archer Author, Inside C# Author, Visual C++.NET Bible A total abstainer is one who abstains from everything but abstention, and especially from inactivity in the af

                      S Offline
                      S Offline
                      sanskypotov
                      wrote on last edited by
                      #10

                      Hi , Thanks for the reply. I certainly have limitations here. I am trying to create instances of 3rd party Ax( ActiveX ), and hence I am not able to SubClass it. Secondly I tried using ON_EVENT_RANGE, but here I don't know how to detect the Control ID . Can someone shed more light on this. Thanks, Sansky :-D God is Good, all the Time. All the Time, God is Good.

                      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