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
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.
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
Re: Single Event Handle for Multiple Controls
Quote:
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?
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.
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.
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.
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. :thumbd: 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).
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?
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.
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.
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 :blush:
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? :)
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?
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.
Re: Single Event Handle for Multiple Controls
Ok, what types of messages, events, etc. are you looking to handle?
Re: Single Event Handle for Multiple Controls
Quote:
Originally Posted by greekgoddj
Well its safe to say now that ON_CONTROL_RANGE is not suitable to be used with ActiveX controls...
Are you sure it is? ;)
Quote:
so the new question is...What is? :)
I'm not sure about what exact thing you're trying to do, but I suppose you're trying to fire some event from AX depending on the fact some of the buttons were clicked, right? Well, for this purpose you have to have message map in your AX along with the ON_CONTROL_RANGE inside, and some event which must be fired on button clicking. Apparently AX event must pass some value related to ctrlID which was clicked. Having these all you just fire AX event from inside control range handler. Easy, ain't it?
Re: Single Event Handle for Multiple Controls
Did you ponder about the point that Igor made?
ON_CONTROL_RANGE macro works for pre-defined notifications from MFC controls. I doubt that it could be extended to handle events fired by your ActiveX controls (even if your ActiveX control is generated by MFC).
So your first approach seems quite the way i.e. using ON_EVENT in the EVENT SINK MAP
What you need is something like ON_EVENT_RANGE and it is precisely what is available.
Please see ON_EVENT_RANGE macro
Re: Single Event Handle for Multiple Controls
Quote:
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.
Are you aware that through versioning, you can add new functionality to a control without breaking other applications that use the control? Modifying the controls seems like the best option here. Otherwise, you'll need to look into using Windows hooks to trap any messages. And, I'm not sure that will handle everything you want.
Re: Single Event Handle for Multiple Controls
Hi again,
The AX controls take care of events from mouse and keyboard and everything is fine with controls themselves. When the AX controls respond to those user events(or are they messages), the AX controls fire off custom events/messages. Now my application is using many of these AX controls and wish to capture the events fired from the AX controls.
The good news...It now works!!! What I needed was ON_EVENT_RANGE http://msdn.microsoft.com/library/de...vent_range.asp
:)
Thanks Igor and Mike!
Aristotel
Re: Single Event Handle for Multiple Controls
Ohh..I just posted that last post and THEN saw the recommendation of ON_EVENT_RANGE!! So yes guys..that was what I needed! Thanks Mitesh :)
Re: Single Event Handle for Multiple Controls
Hi greekgoddj,
if you have no issue then can u plz give me your code sample to handle all the db events of Axapta in my .net application.
It will really very much helpful to me.
Thanx in adavance.
Regards,
Ashlesh