-
June 14th, 2006, 05:41 AM
#1
[RESOLVED] How to use timer as an ocx?
Hi All,
I'm trying to create a digital clock as an ocx (related to my previous question). And I just realize that I can't use timer in class CxxxCtrl (xxx is the name of the project. CxxxCtrl is the child of class COleCtrl). Well, I have already created the OnTimer message handler from WM_TIMER, but it just doesn't work (Windows does not call the OnTimer message handler at all). The SetTimer has been set in the constructor of the CxxxCtrl.
Can anybody give me any information about how to use timer in an ocx?
thanks.
-
June 14th, 2006, 08:18 AM
#2
Re: How to use timer as an ocx?
In order for timer to be handled in WM_TIMER message handler you have to have valid window that will receive it.
You can set up timer using timer procedure that is called by the system directly. See SetTimer description in MSDN.
There are only 10 types of people in the world:
Those who understand binary and those who do not.
-
June 14th, 2006, 09:51 AM
#3
Re: How to use timer as an ocx?
Originally Posted by budihartanto
The SetTimer has been set in the constructor of the CxxxCtrl.
I think it is too early to create the timer in the constructor, since the window was not created yet. You should create the timer later, for instance in the handler for WM_CREATE notification (OnCreate handler), or other appropriate. At the point where SetTimer is executed, the m_hWnd member of your control should not be NULL. Otherwise your OnTimer handler will not be called.
-
June 15th, 2006, 05:00 AM
#4
Re: How to use timer as an ocx?
Thanks for the reply.
For detail information, I used MFC ActiveX control wizard as the framework of my ocx.
For example, I want to display a number that will increase by one, for each second.
-> I have a data member that I called "m_number"
-> give it an initial value 0 in the CxxxCtrl constructor
-> draw the value in the OnDraw
-> Create a message Handler from WM_TIMER (Called OnTimer)
-> add the number m_number by 1 inside the OnTimer
-> Create a message handler from WM_CREATE (Called OnCreate)
-> Set the timer inside OnCreate. Since I thought CxxxCtrl is the descendant of CWnd, then I don't have to give a window handle inside the SetTimer command (I used the SetTimer, that was a member function of class CWnd) - well Actually I tried both SetTimer functions, whether it is a member function of CWnd or it is a Win32 function. And both of them didn't work as I expected.
Below is the source code
/////////////////////////////////////////////////////////////////////////////
// CTimerOcxCtrl::OnDraw - Drawing function
void CTimerOcxCtrl::OnDraw(
CDC* pdc, const CRect& rcBounds, const CRect& rcInvalid)
{
// Draw the number in the center of the ocx rectangle
// =====================================
CRect rectOcx = rcBounds;
CString strNumber;
strNumber.Format("%ld", m_number);
pdc->DrawText(strNumber, rectOcx, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
}
/////////////////////////////////////////////////////////////////////////////
// CTimerOcxCtrl message handlers
void CTimerOcxCtrl::OnTimer(UINT nIDEvent)
{
// Increase the number by 1, for each second
//================================
m_number ++;
if (m_number > 100)
m_number = 0;
COleControl::OnTimer(nIDEvent);
}
int CTimerOcxCtrl::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (COleControl::OnCreate(lpCreateStruct) == -1)
return -1;
// Set Up the Timer
// version I. Using the Win32 SetTimer function.
// Doesn't work
/*
CWnd *pMainWnd = AfxGetMainWnd();
HWND hWnd = pMainWnd->GetSafeHwnd();
SetTimer(hWnd, ID_TIMER1, 1000, NULL);
*/
// Version II. Using the CWnd::SetTimer function
// doesn't work as well
SetTimer(ID_TIMER1, 1000,NULL);
// Note: ID_TIMER1 has been set up in the String Table
return 0;
}
-
June 15th, 2006, 06:09 AM
#5
Re: How to use timer as an ocx?
Most possible, it "didn't work" as you expected, because you didn't invalidate the control each time it's necessary to draw.
Something like
Code:
void CTimerCtrl::OnTimer(UINT nIDEvent)
{
m_number++;
// ...
InvalidateControl();
}
and yes, it's OK to call CWnd::SetTimer in WM_CREATE message handler, like:
Code:
int CTimerCtrl::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (COleControl::OnCreate(lpCreateStruct) == -1)
return -1;
SetTimer(0, 1000, NULL);
return 0;
}
NOTE: The first argument of CWnd::SetTimer is of type UINT and is the event identifier (which is passed to OnTimer handler function).
Nosense to pass an ID of a string from resource string table.
-
June 16th, 2006, 09:35 PM
#6
Re: How to use timer as an ocx?
Thanks a lot for all replies.
Yes, I missed something in my program, that make it didn't work.
It works now.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|