Using timers
I was given some code, and need to make some changes. A much simplified summary
is given below of the relevant member function of class CMain which I have
numbered (1) through (8), where the computer is programmed to read in some
data such as sound or video, then play it back. I'm fairly new to Visual
C++ (though I know C and some C++) and don't understand how to use timers.
In the present implimentation of the program you click on the "Record" button
and clearly function (4) Record() is invoked, it starts the timer which then
calls the last function OnTimer here. The recording is not stopped automatically,
unless a buffer overflow occures, so to stop it you click on the "Stop" button
which clearly invokes function (5) Stop(), but this doesn't do very much,
and somehow the program also invokes function (7) EndRecord(...) which also
kills the timer. The same process happens when you click on the "Play" button
and call function (3) Play() and playback the recorded data, except that
optionally you don't have to stop the playback, as it will stop anyway when
the end of the recorded data is reached.
My main question is how do I simulate mouse clicks, so that on starting the
program recording will automatically take place for a specified amount of
time, say 1 minute, then somehow recording is terminated and playback takes
place. By calling Record() at the end of initialization in (1) CMain() will
invoke recording, but I can't see how then to call EndRecord(...) then Play().
Putting these after calling Record() inside CMain() clearly won't work.
One possibility is that it looks as if I have to use additional calls to
SetTimer and KillTimer, then somehow intercept the WM_TIMER messages and
make them look like mouse clicks. I also don't really know what CWnd::OnTimer(timerId)
does in the last function.
I would be most grateful for somebody's advice on this.
Christopher Sharp
-------------------
#define ID_TIMER 1
UINT idTimer;
CMain::CMain() // (1)
{
// Initializations
}
CMain::~CMain() // (2)
{
// Cleanup
}
void CMain::Play() // (3)
{
// ... code
idTimer=SetTimer(ID_TIMER,1,NULL);
// ... code
}
void CMain::Record() // (4)
{
// ... code
idTimer=SetTimer(ID_TIMER,1,1NULL);
// ... code
}
void CMain::STOP() // (5)
{
// Just resets a variable and does nothing else
}
LRESULT CMain::EndPlay(WPARAM wParam, LPARAM, lParam) // (6)
{
KillTimer(idTimer);
// ... code
}
LRESULT CMain::EndRecord(WPARAM wParam, LPARAM, lParam) // (7)
{
KillTimer(idTimer);
// ... code
}
void CMain::OnTimer(UINT timerId) // (8)
{
CWnd::OnTimer(timerId);
// ... code
}
# 1 Re: Using timers
Hello,
I simulate mouse clicks by using the SendMessage function. If you have access
to the CButton class (and if your buttons are some derived class, if you
have access to them) you can use the member function. If not, you can use
the non-member function with the Button handle as a parameter.
The CButton SendMessage member function is actually a member of its parent
class, CWnd. The function is:
CWnd::SendMessage(UINT MESSAGE, WPARAM, LPARAM)
- MESSAGE is the message you send to CWnd. I use WM_LBUTTONDOWN when I simulate
mouse clicks. This corresponds to a left mouse button down.
- WPARAM is message specific information. In this case it is NULL
- LPARAM is also message specific, and in this case is NULL
The non-member function is:
SendMessage(HWND hHANDLE, UINT MESSAGE, WPARAM, LPARAM)
- hHANDLE is the handle of the window to send the message.
- The rest of the parameters are the same as above.
When you call this function, the message is sent to the window as though
it came from the user (like a mouse click). The button then processes the
message (in your case, stopping, recording, etc...)
The functions look like this (for example)
CButton MyButton;
MyButton.SendMessage(WM_LBUTTONDOWN, NULL, NULL);
- or -
HWND hButton;
SendMessage(hButton, WM_LBUTTONDOWN, NULL, NULL);
You can use these functions when your timer reaches a certain point (ie 100
seconds). However, you have to know the handle of the Button, or have access
to the CButton class you want to send the message to.
I hope this helps you out. If you have any other questions, let me know.
-Johnny
"Christopher Sharp" <cmsharp01@aol.com> wrote:
>
>I was given some code, and need to make some changes. A much simplified
summary
>is given below of the relevant member function of class CMain which I have
>numbered (1) through (8), where the computer is programmed to read in some
>data such as sound or video, then play it back. I'm fairly new to Visual
>C++ (though I know C and some C++) and don't understand how to use timers.
>
>In the present implimentation of the program you click on the "Record" button
>and clearly function (4) Record() is invoked, it starts the timer which
then
>calls the last function OnTimer here. The recording is not stopped automatically,
>unless a buffer overflow occures, so to stop it you click on the "Stop"
button
>which clearly invokes function (5) Stop(), but this doesn't do very much,
>and somehow the program also invokes function (7) EndRecord(...) which also
>kills the timer. The same process happens when you click on the "Play"
button
>and call function (3) Play() and playback the recorded data, except that
>optionally you don't have to stop the playback, as it will stop anyway when
>the end of the recorded data is reached.
>
>My main question is how do I simulate mouse clicks, so that on starting
the
>program recording will automatically take place for a specified amount of
>time, say 1 minute, then somehow recording is terminated and playback takes
>place. By calling Record() at the end of initialization in (1) CMain()
will
>invoke recording, but I can't see how then to call EndRecord(...) then Play().
> Putting these after calling Record() inside CMain() clearly won't work.
> One possibility is that it looks as if I have to use additional calls to
>SetTimer and KillTimer, then somehow intercept the WM_TIMER messages and
>make them look like mouse clicks. I also don't really know what CWnd::OnTimer(timerId)
>does in the last function.
>
>I would be most grateful for somebody's advice on this.
>
>Christopher Sharp
>
>-------------------
>#define ID_TIMER 1
>
>UINT idTimer;
>
>CMain::CMain() // (1)
>{
> // Initializations
>}
>
>CMain::~CMain() // (2)
>{
> // Cleanup
>}
>
>void CMain::Play() // (3)
>{
> // ... code
>
> idTimer=SetTimer(ID_TIMER,1,NULL);
>
> // ... code
>}
>
>void CMain::Record() // (4)
>{
> // ... code
>
> idTimer=SetTimer(ID_TIMER,1,1NULL);
>
> // ... code
>}
>
>void CMain::STOP() // (5)
>{
> // Just resets a variable and does nothing else
>}
>
>LRESULT CMain::EndPlay(WPARAM wParam, LPARAM, lParam) // (6)
>{
> KillTimer(idTimer);
>
> // ... code
>}
>
>LRESULT CMain::EndRecord(WPARAM wParam, LPARAM, lParam) // (7)
>{
> KillTimer(idTimer);
>
> // ... code
>}
>
>void CMain::OnTimer(UINT timerId) // (8)
>{
> CWnd::OnTimer(timerId);
>
> // ... code
>}
Johnny at 2007-11-11 20:41:21 >

# 2 Re: Using timers
Hi,
Thanks very much for your reply, which I'm sure will be most helpful. I
will try out your suggestions over the weekend and let you know how I get
on.
Christopher
"Johnny" <aeroslacker@hotmail.com> wrote:
>
>Hello,
>
>I simulate mouse clicks by using the SendMessage function. If you have
access
>to the CButton class (and if your buttons are some derived class, if you
>have access to them) you can use the member function. If not, you can use
>the non-member function with the Button handle as a parameter.
>
>The CButton SendMessage member function is actually a member of its parent
>class, CWnd. The function is:
>
>CWnd::SendMessage(UINT MESSAGE, WPARAM, LPARAM)
>
>- MESSAGE is the message you send to CWnd. I use WM_LBUTTONDOWN when I
simulate
>mouse clicks. This corresponds to a left mouse button down.
>- WPARAM is message specific information. In this case it is NULL
>- LPARAM is also message specific, and in this case is NULL
>
>The non-member function is:
>
>SendMessage(HWND hHANDLE, UINT MESSAGE, WPARAM, LPARAM)
>
>- hHANDLE is the handle of the window to send the message.
>- The rest of the parameters are the same as above.
>
>When you call this function, the message is sent to the window as though
>it came from the user (like a mouse click). The button then processes the
>message (in your case, stopping, recording, etc...)
>
>The functions look like this (for example)
>
>CButton MyButton;
>MyButton.SendMessage(WM_LBUTTONDOWN, NULL, NULL);
>
>- or -
>
>HWND hButton;
>SendMessage(hButton, WM_LBUTTONDOWN, NULL, NULL);
>
>You can use these functions when your timer reaches a certain point (ie
100
>seconds). However, you have to know the handle of the Button, or have access
>to the CButton class you want to send the message to.
>
>I hope this helps you out. If you have any other questions, let me know.
>
>-Johnny
>