void CRepeatButton::OnTimer(UINT nIDEvent)
{
if (nIDEvent == 100)
{
KillTimer(100);
//start the button pressing timer
SetTimer(200,200,NULL); // 200=ID 1000=msdelay
}
else if (nIDEvent = 200)
{
CWnd *Parent = GetParent();
if (Parent)
{
//send the BN_CLICK Message to the parent
Parent->SendMessage(WM_COMMAND,MAKEWPARAM(GetDlgCtrlID(),BN_CLICKED),(LPARAM)m_hWnd);
}
}
CButton::OnTimer(nIDEvent);
}
BN_CLICKED is not a message.
That is not what happens. What happens in the code snippet is as follows:
After 100 ms delay timer is set to 200 ms delay and from a WM_TIMER message
handler, button repeatedly sends WM_COMMAND message with button’s ID and
NOTYFICATION CODE BN_CLICKED.
I do not see any function connected to a CRepeatButton. MFC way of handling
messages is mapping a message handler in a class’ object.
I cannot say if Parent object has a handler for this particular command.
To answer your question:
In general, of course, you can. You would have to cast Parent pointer to a
dialog class type.
Another way of handling that would be to send a custom message.
The question is why would you want to do that? Why would you use button to
repeatedly invoke a function from a button, while you can do the same in the
Parent class, calling member function from own WM_TIMER message handler?
--
RainMan
This should be '=='
-- David
"David Ching" <d...@remove-this.dcsoft.com> wrote in message
news:utcLYK9M...@TK2MSFTNGP02.phx.gbl...
>In the code below in the RepeatButton.cpp of the test app I see it
>sends the BN_CLICKED message to the button function which is connected
>to CRepeatButton.
>Is it possible to call another function within the main dialog from here?
>
>void CRepeatButton::OnTimer(UINT nIDEvent)
>{
****
Have you ever heard of #define or const int? Who in their right mind would use an
absolute integer in this context?
****
> if (nIDEvent == 100)
> {
> KillTimer(100);
> //start the button pressing timer
****
You do not need to kill the timer to change its time, and you don't need a second
timer; you can use the first timer.
Again, have you ever heard of #define? There would be no need to have a comment that
disagrees with the code if you used symbolic names such as
SetTimer(IDT_REPEAT_TIMER, REPEAT_INTERVAL, NULL);
****
> SetTimer(200,200,NULL); // 200=ID 1000=msdelay
> }
> else if (nIDEvent = 200)
****
This code shows real beginner qualities. Also, I would think you would want to use ==
here, instead of an embedded assignment. And you would want to use a #define or const
int to define the value. Turn on /W4 for your builds. In fact, you should never build
with less than /W4. Any warnings you get will require repair, and that is how it should
be.
****
> {
> CWnd *Parent = GetParent();
> if (Parent)
****
It is clearer if you write
if(Parent != NULL)
but why are you wasting time with this test? This is a child control; it cannot exist
without having a parent! Therefore, GetParent cannot return NULL. And since returning
NULL would indicate a very deep and serious error, where is the
ASSERT(Parent != NULL);
that should be here?
****
> {
> //send the BN_CLICK Message to the parent
> Parent->SendMessage(WM_COMMAND,MAKEWPARAM(GetDlgCtrlID(),BN_CLICKED),(LPARAM)m_hWnd);
> }
> }
> CButton::OnTimer(nIDEvent);
>}
>
Joseph M. Newcomer [MVP]
email: newc...@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
'nIDEvent = 200' is an expression that is evaluated to 200.
In fact, '=' is an operator, and its return value is the RHS (i.e. 200)
in your case.
So the expression in the else-if:
else if (nIDEvent = 200)
...
is evaluated as:
else if (200)
...
200 is different from 0, so it is evaluated as boolean value "true" (0
-> "false", non-0 -> "true").
So, your code is equivalent to:
...
else if (true) {
nIDEvent = 200;
...
}
or just (considering that 'else if (true)' is simply an 'else'):
...
else {
nIDEvent = 200;
...
}
So, the global 'if' becomes:
<code>
if (nIDEvent == 100)
{
KillTimer(100);
//start the button pressing timer
SetTimer(200,200,NULL); // 200=ID 1000=msdelay
}
else // was: else if (nIDEvent = 200)
{
nIDEvent = 200;
CWnd *Parent = GetParent();
if (Parent)
{
//send the BN_CLICK Message to the parent
Parent->SendMessage(WM_COMMAND,MAKEWPARAM(GetDlgCtrlID(),BN_CLICKED),(LPARAM)m_hWnd);
}
}
</code>
Probably you only have two timers (the '100' and the '200'), so if the
event ID is not 100 it is 200, so your code works.
But of course it is not good programming style, and the proper thing to
do is to use the clear form of operator== (equality operator) and use
const int to identify timer IDs, as David and Joe suggested.
Giovanni