Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How can I display current time in dialog static control

How can I display current time in dialog static control

Scheduled Pinned Locked Moved C / C++ / MFC
question
18 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A amistry_petlad

    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);

    D Offline
    D Offline
    David Crow
    wrote on last edited by
    #7

    amistry_petlad wrote:

    ...but rund time break the application

    And rightfully so since you did not change sprintf_s().

    "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

    A 1 Reply Last reply
    0
    • D David Crow

      amistry_petlad wrote:

      ...but rund time break the application

      And rightfully so since you did not change sprintf_s().

      "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

      A Offline
      A Offline
      amistry_petlad
      wrote on last edited by
      #8

      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 ?

      D 1 Reply Last reply
      0
      • A amistry_petlad

        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); } }

        M Offline
        M Offline
        Maximilien
        wrote on last edited by
        #9

        Why do you do this in the OnPaint ? SetDlgItemText(IDC_STATIC,(LPCTSTR)str); you want to print str or buf ?

        This signature was proudly tested on animals.

        A 1 Reply Last reply
        0
        • M Maximilien

          Why do you do this in the OnPaint ? SetDlgItemText(IDC_STATIC,(LPCTSTR)str); you want to print str or buf ?

          This signature was proudly tested on animals.

          A Offline
          A Offline
          amistry_petlad
          wrote on last edited by
          #10

          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);

          1 Reply Last reply
          0
          • M messages

            Can you show your code,please?

            A Offline
            A Offline
            amistry_petlad
            wrote on last edited by
            #11

            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);

            H 1 Reply Last reply
            0
            • A amistry_petlad

              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 ?

              D Offline
              D Offline
              David Crow
              wrote on last edited by
              #12

              amistry_petlad wrote:

              ...now its working when i used sprintf but...

              It makes no difference whether you use sprintf() or sprintf_s(). If you have a malformed format string, you can't expect correct results. What is the value of buf prior to calling SetDlgItemText()?

              "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

              A 1 Reply Last reply
              0
              • A amistry_petlad

                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);

                J Offline
                J Offline
                Jijo Raj
                wrote on last edited by
                #13

                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.

                A 1 Reply Last reply
                0
                • D David Crow

                  amistry_petlad wrote:

                  ...now its working when i used sprintf but...

                  It makes no difference whether you use sprintf() or sprintf_s(). If you have a malformed format string, you can't expect correct results. What is the value of buf prior to calling SetDlgItemText()?

                  "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

                  A Offline
                  A Offline
                  amistry_petlad
                  wrote on last edited by
                  #14

                  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

                  1 Reply Last reply
                  0
                  • J Jijo Raj

                    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.

                    A Offline
                    A Offline
                    amistry_petlad
                    wrote on last edited by
                    #15

                    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

                    J 1 Reply Last reply
                    0
                    • A amistry_petlad

                      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

                      J Offline
                      J Offline
                      Jijo Raj
                      wrote on last edited by
                      #16

                      amistry_petlad wrote:

                      and you are genius

                      :rolleyes: Regards, Jijo.

                      _____________________________________________________ http://weseetips.com[^] Visual C++ tips and tricks. Updated daily.

                      S 1 Reply Last reply
                      0
                      • J Jijo Raj

                        amistry_petlad wrote:

                        and you are genius

                        :rolleyes: Regards, Jijo.

                        _____________________________________________________ http://weseetips.com[^] Visual C++ tips and tricks. Updated daily.

                        S Offline
                        S Offline
                        SandipG
                        wrote on last edited by
                        #17

                        You guys dont like _strdate and _strtime functions, do you?? :)

                        Regards, Sandip.

                        1 Reply Last reply
                        0
                        • A amistry_petlad

                          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);

                          H Offline
                          H Offline
                          Hamid Taebi
                          wrote on last edited by
                          #18

                          You can set size of font for this control.

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups