How can I display current time in dialog static control
-
void CDigitalclockDlg::OnPaint() { if (IsIconic()) { CPaintDC dc(this); // device context for painting . ........ ............ ............ } else { CDialog::OnPaint(); GetLocalTime(&time); char str[256]; sprintf_s(str,"02d%:02d%:02d%",time.wHour,time.wMinute,time.wSecond); int len; int slength = (int)str.length() + 1; len = MultiByteToWideChar(CP_ACP, 0, str.c_str(), slength, 0, 0); wchar_t* buf = new wchar_t[len]; MultiByteToWideChar(CP_ACP, 0, str.c_str(), slength, buf, len); SetDlgItemText(IDC_STATIC,(LPCTSTR)str); } }
Why do you do this in the OnPaint ?
SetDlgItemText(IDC_STATIC,(LPCTSTR)str);
you want to printstr
orbuf
?This signature was proudly tested on animals.
-
Why do you do this in the OnPaint ?
SetDlgItemText(IDC_STATIC,(LPCTSTR)str);
you want to printstr
orbuf
?This signature was proudly tested on animals.
ya buf i have chnaged , the code is working but its not display the hh:mm:yy its only shows current hours in the static control
GetLocalTime(&time); char str[256]; sprintf(str,"%d:%d:%d",time.wHour,time.wMinute,time.wSecond); std::string str1(str); int len; int slength = (int)str1.length() + 1; len = MultiByteToWideChar(CP_ACP, 0, str1.c_str(), slength, 0, 0); wchar_t* buf = new wchar_t[len]; MultiByteToWideChar(CP_ACP, 0, str1.c_str(), slength, buf, len); SetDlgItemText(IDC_STATIC1,(LPCTSTR)buf);
-
the code working but display hours only i have increased the size of control but still not able to see the entire string
GetLocalTime(&time); char str[256]; sprintf(str,"%d:%d:%d",time.wHour,time.wMinute,time.wSecond); std::string str1(str); int len; int slength = (int)str1.length() + 1; len = MultiByteToWideChar(CP_ACP, 0, str1.c_str(), slength, 0, 0); wchar_t* buf = new wchar_t[len]; MultiByteToWideChar(CP_ACP, 0, str1.c_str(), slength, buf, len); SetDlgItemText(IDC_STATIC1,(LPCTSTR)buf);
-
now its working when i used sprintf but in the dialog it should be print hh:mm:yy I have debug the application i got the perfect string in all varibals but when display in the static control its only shows the hour nothing more. So here in US 1:40:40 so must be displayed but it show me 1 only ?
amistry_petlad wrote:
...now its working when i used sprintf but...
It makes no difference whether you use
sprintf()
orsprintf_s()
. If you have a malformed format string, you can't expect correct results. What is the value ofbuf
prior to callingSetDlgItemText()
?"Love people and use things, not love things and use people." - Unknown
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
When I had simply used sprintf complier generated warning for sprintf AND give suggestion fo sprintf_s i am using MSVS2008 ON VISTA I have change the code following way it compile succesful but rund time break the application
GetLocalTime(&time); char str[256]; sprintf_s(str,"02d%:02d%:02d%",time.wHour,time.wMinute,time.wSecond); std::string str1(str); int len; int slength = (int)str1.length() + 1; len = MultiByteToWideChar(CP_ACP, 0, str1.c_str(), slength, 0, 0); wchar_t* buf = new wchar_t[len]; MultiByteToWideChar(CP_ACP, 0, str1.c_str(), slength, buf, len); SetDlgItemText(IDC_STATIC1,buf);
amistry_petlad wrote:
sprintf_s(str,"02d%:02d%:02d%",time.wHour,time.wMinute,time.wSecond);
The format string is
"02d%:02d%:02d%"
or"%02d:%02d:%02d"
? ;) Need to brushup those old C lessons? :-D BTW, Why can't you use CString?SYSTEMTIME time;
GetLocalTime(&time);CString csTime;
csTime.Format( _T("%02d:%02d:%02d"), time.wHour,time.wMinute,time.wSecond );SetDlgItemText(IDC_STATIC1,csTime);
I apoligize that since i don't have VS2008 installed, the above code snippet is compiled in VS6.0. So try it. Regards, Jijo.
_____________________________________________________ http://weseetips.com[^] Visual C++ tips and tricks. Updated daily.
-
amistry_petlad wrote:
...now its working when i used sprintf but...
It makes no difference whether you use
sprintf()
orsprintf_s()
. If you have a malformed format string, you can't expect correct results. What is the value ofbuf
prior to callingSetDlgItemText()
?"Love people and use things, not love things and use people." - Unknown
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
buf has garbage value now In my debugging i have seen that if 15:22:34 Then in buf shows L(1) only show what is the reason? so then this character display in the static control
-
amistry_petlad wrote:
sprintf_s(str,"02d%:02d%:02d%",time.wHour,time.wMinute,time.wSecond);
The format string is
"02d%:02d%:02d%"
or"%02d:%02d:%02d"
? ;) Need to brushup those old C lessons? :-D BTW, Why can't you use CString?SYSTEMTIME time;
GetLocalTime(&time);CString csTime;
csTime.Format( _T("%02d:%02d:%02d"), time.wHour,time.wMinute,time.wSecond );SetDlgItemText(IDC_STATIC1,csTime);
I apoligize that since i don't have VS2008 installed, the above code snippet is compiled in VS6.0. So try it. Regards, Jijo.
_____________________________________________________ http://weseetips.com[^] Visual C++ tips and tricks. Updated daily.
sorry ,in hurry i had type that things but in my code it is ok but sorry for that. and you are genius becoz you find that mistake anyways thanks for this code
-
sorry ,in hurry i had type that things but in my code it is ok but sorry for that. and you are genius becoz you find that mistake anyways thanks for this code
-
amistry_petlad wrote:
and you are genius
:rolleyes: Regards, Jijo.
_____________________________________________________ http://weseetips.com[^] Visual C++ tips and tricks. Updated daily.
-
the code working but display hours only i have increased the size of control but still not able to see the entire string
GetLocalTime(&time); char str[256]; sprintf(str,"%d:%d:%d",time.wHour,time.wMinute,time.wSecond); std::string str1(str); int len; int slength = (int)str1.length() + 1; len = MultiByteToWideChar(CP_ACP, 0, str1.c_str(), slength, 0, 0); wchar_t* buf = new wchar_t[len]; MultiByteToWideChar(CP_ACP, 0, str1.c_str(), slength, buf, len); SetDlgItemText(IDC_STATIC1,(LPCTSTR)buf);
You can set size of font for this control.