Click here to Skip to main content
15,891,513 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi;
I used the time picker from MFC ToolBox.
Would you suggest me how Can I update the time by current time? (like a real clock)?

Thank you;
Sb
Posted
Comments
Christian Graus 17-Aug-10 16:00pm    
Do you mean set the system time using the time selected, or set the control to show the current time ?

// I just need to have a clock on the Dialog window.

It will be suggested (by me),
to use a static text box (for example with ID: IDC_YOURCLOCK),

whose content will be set (GetDlgItem(IDC_YOURCLOCK)->SetWindowText(..))
by the formatted strings from the timer reaction of your dialog.

You could also set any styles and fonts at that box,
and it will stay as a "read only" clock for the users... :)
 
Share this answer
 
Comments
[no name] 18-Aug-10 10:17am    
YOu are right, I also can do it by Text Control.
So I did like:
CString Time;
SYSTEMTIME LocalTime;
GetLocalTime(&LocalTime);
Time.Format(_T("%02d%s%02d%s%02d"),LocalTime.wHour,_T(":"),LocalTime.wMinute,_T(":"),LocalTime.wSecond);
GetDlgItem(IDC_STATICTIME)->SetWindowText(Time);

and I have a clock on the dialog.
How can make it real time? do I need timer?right now it looks like a timepicker, the first time that I run it it shows the correct value.
Thanks
Eugen Podsypalnikov 18-Aug-10 10:25am    
Yes, you can use a timer (with the rate about a half of the smallest unit, so in this case ~500ms) and update your clock box from the CYourDialog::OnTimer(..) directly, by your code above :) (the actual time will be set to a DateTimePicker at its creation, you could initialize your box in CYourDialog::OnInitDialog() for the first time only, then start the timer and let it work)
[no name] 18-Aug-10 10:47am    
oooo; I understand.
Is the OnTimer function, calls automatically and recursively?
I am sorry that I keep asking questions, I read this link
ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.WIN32COM.v10.en/WMFORM95/htm/iwmreadercallbackadvancedontime.htm
but it is not clear for me.
So in side of this function; I wrote something like:
CDialog::OnTimer(nIDEvent);
int i=0;
if (i<100){
UpdateWindow();
i++;
}
that 100 is just a sample value.
Does it make sense? Am i in a good track?
Thanks again
Eugen Podsypalnikov 18-Aug-10 10:58am    
Your track is correct :)

void CYourDialog::UpdateClock()
{
CString cszTime;
// Format the string with the date/time information here...
// ...

GetDlgItem(IDC_STATIC_CLOCK)->SetWindowText(cszTime);
}

void CYourDialog::OnTimer(UINT_PTR uiEvent)
{
UpdateClock(); // nothing more

CDialog::OnTimer(uiEvent);
}

int CYourDialog::OnInitDialog()
{
int iResult = CDialog::OnInitDialog();

UpdateClock();

/*UINT_PTR dialog member*/ m_uiTimer = SetTimer(1, 500, 0);

return iResult;
}

void CYourDialog::OnDestroy()
{
KillTimer(m_uiTimer);
}

Please control also your message map implementation:

BEGIN_MESSAGE_MAP(CYourDialog, CDialog)
ON_WM_TIMER()
ON_WM_DESTROY()
...
END_MESSAGE_MAP()
[no name] 18-Aug-10 11:25am    
Thanks for your help Eugen.
Your TimePicker should have a property called Time (or maybe DateTime). When you want to update the control (maybe on a timer), just set that property to the current time/datetime. (There is probably a SetTime or some such method.) Make sure the control or some container of it is refreshed.
 
Share this answer
 
I just neen to have a clock on the Dialog window.
Right mow I add TimePicker from ToolBox. What I have now is sit at whatever time when I start the application. I want to update it automatically.
Thanks for help.

Sb
 
Share this answer
 
Comments
raju melveetilpurayil 17-Aug-10 16:32pm    
Reason for my vote of 1
don't hit answer tab for add comment.
raju melveetilpurayil 17-Aug-10 16:34pm    
you can add comment like me and Christian Graus did.
[no name] 17-Aug-10 17:28pm    
I just neen to have a clock on the Dialog window.
Right mow I add TimePicker from ToolBox. What I have now is sit at whatever time when I start the application. I want to update it automatically.
Thanks for help.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900