CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 21
  1. #1
    Join Date
    Dec 2004
    Location
    Leamington Spa, UK
    Posts
    202

    Single Event Handle for Multiple Controls

    Greetings,

    I have a program that creates a number of activeX controls. That number is not known until run time. I achive this by having a dynamic array of control pointers allocated once the number of controls needed is know. When creating the controls I must give them a unique ID which I do by starting the ID count from a specific value and incrimenting it for every control. This works fine.

    However I want to capture messages fired by these controls. The ugly way to do this would be to write an event handler for evry control and every message, but as I do not know the number of controls until run time, I won't know how many controls event handlers to code.

    A nicer solution would be to use ON_CONTROL_RANGE http://msdn.microsoft.com/library/de...trol_range.asp which forwards events from controls with IDs of the specified consecutive range to a single function.

    However once in that function I have no way of knowing controls was the one that fired the message in the first place. Does anybody have any other ideas of how this can be achieved? I want one function to handle all messages fired by a number of controls, and I want to still be able to know (preferably inside the handling function) the ID of the control that fired that message.


    Thanks,

    Aristotel

  2. #2
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: Single Event Handle for Multiple Controls

    Quote Originally Posted by greekgoddj
    A nicer solution would be to use ON_CONTROL_RANGE http://msdn.microsoft.com/library/de...trol_range.asp which forwards events from controls with IDs of the specified consecutive range to a single function.

    However once in that function I have no way of knowing controls was the one that fired the message in the first place.
    Sorry, you're wrong.
    The prototype for handler for ON_CONTROL_RANGE must be:

    Code:
    void CMyContainerWindow::OnSomeNotify(UINT ctrlID);
    Here in ctrlID you get the id of the control which fired the notification.
    Best regards,
    Igor

  3. #3
    Join Date
    Dec 2004
    Location
    Leamington Spa, UK
    Posts
    202

    Re: Single Event Handle for Multiple Controls

    Hi, thanks for that...you are right ofcourse...

    However I have a problem:


    error C2440: 'initializing' : cannot convert from 'int' to 'LPCTSTR'
    Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast

    error C2440: 'initializing' : cannot convert from 'WORD' to 'LPCSTR'
    Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast

    error C2440: 'initializing' : cannot convert from 'AfxSig' to 'AFX_PMSG'
    There is no context in which this conversion is possible



    with the following code:

    Code:
    BEGIN_EVENTSINK_MAP(CVSTPage, CWnd)
    	ON_CONTROL_RANGE(1, IDR_PARAMETERSTART, IDR_PARAMETERSTART + 50, OnParameterChanged)
    END_EVENTSINK_MAP()
    There was a similar problem in this thread http://www.codeguru.com/forum/showth..._CONTROL_RANGE
    but it seems that guys mistake was that the function's argument list did not contain the UINT. I have is like this
    Code:
    afx_msg void OnParameterChanged(UINT ctrlID);
    though and cannot figure what the problem is.

    Any ideas?


    Thanks

    Aristotel

  4. #4
    Join Date
    Apr 1999
    Posts
    3,585

    Re: Single Event Handle for Multiple Controls

    However I want to capture messages fired by these controls.
    What kinds of messages? All messages, or, just certain ones? Or, are you speaking in terms of control events? What about the other controls?
    Gort...Klaatu, Barada Nikto!

  5. #5
    Join Date
    Dec 2004
    Location
    Leamington Spa, UK
    Posts
    202

    Re: Single Event Handle for Multiple Controls

    Quote Originally Posted by Mike Harnad
    What kinds of messages? All messages, or, just certain ones? Or, are you speaking in terms of control events? What about the other controls?
    Specific Messages. Like the controls are all custom knobs made as ActiveX controls. When their value changes (when by mouse or keyboard) then they fire a message saying that the position has changed as well as firing the value indicating the position of the knob. With Igor's suggestion to use ON_CONTROL_RANGE to capture that message and in the handling function where the ID of the firing control is passed, I would know which of the controls it was and I would ask the control directly for what its value is.

  6. #6
    Join Date
    Dec 2004
    Location
    Leamington Spa, UK
    Posts
    202

    Re: Single Event Handle for Multiple Controls

    More information in case its of help in figuring this out.

    At the moment I have:
    Code:
    BEGIN_EVENTSINK_MAP(CVSTPage, CWnd)
    	ON_CONTROL_RANGE(1, IDR_PARAMETERSTART, IDR_PARAMETERSTART + 50, OnParameterChanged)
    END_EVENTSINK_MAP()
    and I get the above mentioned compilation error list. I have it in the event sink section as I am expecting messages from OLE controls. I had it before in the Message map and it had no compilation problems but the messages never were forwarded to the handling function. Perhaps I am wrong in thinking that ON_CONTROL_RANGE should be under the event sink map.

  7. #7
    Join Date
    Apr 1999
    Posts
    3,585

    Re: Single Event Handle for Multiple Controls

    Have you thought about defining a generic event that can be used by all of the controls? The event can pass a structure pointer, or, whatever you need to expose. The event handler will then have all of the information it needs.
    Gort...Klaatu, Barada Nikto!

  8. #8
    Join Date
    Dec 2004
    Location
    Leamington Spa, UK
    Posts
    202

    Re: Single Event Handle for Multiple Controls

    Quote Originally Posted by Mike Harnad
    Have you thought about defining a generic event that can be used by all of the controls? The event can pass a structure pointer, or, whatever you need to expose. The event handler will then have all of the information it needs.
    That would involve changing the code for the controls themeselves which I am not supposed to go into. The problem is not in the controls. The problem would be the same if I used ActiveX controls from another party (where i cannot go inot the code and create generic events for particular projects).

  9. #9
    Join Date
    Apr 1999
    Posts
    3,585

    Re: Single Event Handle for Multiple Controls

    Sorry, from your initial description it appeared like you owned the source for the controls. Can you provide a specific example of what message you'd like to handle? You are talking about handling the message in the container (ie. dialog), correct?
    Gort...Klaatu, Barada Nikto!

  10. #10
    Join Date
    Dec 2004
    Location
    Leamington Spa, UK
    Posts
    202

    Re: Single Event Handle for Multiple Controls

    I do have the source code to the ActiveX controls, I was the one who wrote them a year ago or so. What I meant to say is that I cannot/should not change the behaviour of the controls which are already being used in many places and different applications, just for them being used in one specific application. Especially since there seems to be a specific macros (ON_CONTROL_RANGE) to deal with my requirements...the trick is to get that macros to work

    The ActiveX controls are Knobs...when the knob changes in some way (through the user changing it by the mouse or keys) and everytime that happens, the ActiveX control fires a message "KnobChanged(short)" where short is is a short indicating the new value of the knob.

    The following code:

    Code:
    //in somepage.h
    void OnFirstKnobChange(short nPosition); 
    //in somepage.cpp
    BEGIN_EVENTSINK_MAP(CSomePage, CWnd)
    	ON_EVENT(CSomePage, IDR_PARAMETERSTART, 1 , OnFirstKnobChange, VTS_I2)
    END_EVENTSINK_MAP()
    
    void CSomePage::OnFirstKnobChange(short nPosition)
    {
    	int a = 0;  //Set breakpoint here
    }
    Works fine, the breakpoint stops when I fiddle with the first knob in the window. So the aim is to use ON_CONTROL_RANGE to achieve the same thing for a number of such ActiveX controls.

  11. #11
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: Single Event Handle for Multiple Controls

    Quote Originally Posted by greekgoddj
    with the following code:

    Code:
    BEGIN_EVENTSINK_MAP(CVSTPage, CWnd)
    	ON_CONTROL_RANGE(1, IDR_PARAMETERSTART, IDR_PARAMETERSTART + 50, OnParameterChanged)
    END_EVENTSINK_MAP()
    There was a similar problem in this thread http://www.codeguru.com/forum/showth..._CONTROL_RANGE
    but it seems that guys mistake was that the function's argument list did not contain the UINT. I have is like this
    Code:
    afx_msg void OnParameterChanged(UINT ctrlID);
    though and cannot figure what the problem is.

    Any ideas?
    The idea is the only: why on earth did you placed ON_CONTROL_RANGE into event sink map?!!! It belongs in message map and nowhere else.
    Best regards,
    Igor

  12. #12
    Join Date
    Dec 2004
    Location
    Leamington Spa, UK
    Posts
    202

    Re: Single Event Handle for Multiple Controls

    Quote Originally Posted by Igor Vartanov
    The idea is the only: why on earth did you placed ON_CONTROL_RANGE into event sink map?!!! It belongs in message map and nowhere else.
    Hmmmm....emmm... good question

    Well its safe to say now that ON_CONTROL_RANGE is not suitable to be used with ActiveX controls...so the new question is...What is?

  13. #13
    Join Date
    Apr 1999
    Posts
    3,585

    Re: Single Event Handle for Multiple Controls

    From your problem description, it sounds like you want to handle the events in the container, not the control. Is this correct? Are you looking for a mechanishm that will handle events from all controls within a dialog?
    Gort...Klaatu, Barada Nikto!

  14. #14
    Join Date
    Dec 2004
    Location
    Leamington Spa, UK
    Posts
    202

    Re: Single Event Handle for Multiple Controls

    Quote Originally Posted by Mike Harnad
    From your problem description, it sounds like you want to handle the events in the container, not the control. Is this correct? Are you looking for a mechanishm that will handle events from all controls within a dialog?

    Yes, that is correct. Sorry if it was not clear enough.

  15. #15
    Join Date
    Apr 1999
    Posts
    3,585

    Re: Single Event Handle for Multiple Controls

    Ok, what types of messages, events, etc. are you looking to handle?
    Gort...Klaatu, Barada Nikto!

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured