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 to display CStatic

How to display CStatic

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialc++visual-studiohelp
9 Posts 3 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.
  • G Offline
    G Offline
    gomez_a
    wrote on last edited by
    #1

    I have an MFC application (VS 2003 Std). There is a dialog window with CStaticText. For example: CStatic lblRecno; I want to change the colour of the CStaticText. I am using the OnCtlColor function: HBRUSH DlgBackup::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) { HBRUSH hbr = NULL; /* if(pWnd->GetDlgCtrlID() == IDC_STATIC_LP) or */ if( nCtlColor == CTLCOLOR_STATIC ) { pDC->SetTextColor(clBlack); pDC->SetBkMode(TRANSPARENT); hbr = (HBRUSH) GetStockObject( NULL_BRUSH ); } else { hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor); } return hbr; } To this place is OK. Now I am changeing the lblRecno value in the while loop: int lp = 0; CString newTxt = ""; while(...) { ++lp; bewTxt.Format("%d", lp); lblRecno.SetWindowText(newTxt); } But the new value lblRecno is displaying in the place of the previous value, an so on. I think I should to use the WM_ERASEBKGND, but I don't know how to do it. Can you help me. Regards mwgomez Poland

    R D 2 Replies Last reply
    0
    • G gomez_a

      I have an MFC application (VS 2003 Std). There is a dialog window with CStaticText. For example: CStatic lblRecno; I want to change the colour of the CStaticText. I am using the OnCtlColor function: HBRUSH DlgBackup::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) { HBRUSH hbr = NULL; /* if(pWnd->GetDlgCtrlID() == IDC_STATIC_LP) or */ if( nCtlColor == CTLCOLOR_STATIC ) { pDC->SetTextColor(clBlack); pDC->SetBkMode(TRANSPARENT); hbr = (HBRUSH) GetStockObject( NULL_BRUSH ); } else { hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor); } return hbr; } To this place is OK. Now I am changeing the lblRecno value in the while loop: int lp = 0; CString newTxt = ""; while(...) { ++lp; bewTxt.Format("%d", lp); lblRecno.SetWindowText(newTxt); } But the new value lblRecno is displaying in the place of the previous value, an so on. I think I should to use the WM_ERASEBKGND, but I don't know how to do it. Can you help me. Regards mwgomez Poland

      R Offline
      R Offline
      Rinu_Raj
      wrote on last edited by
      #2

      What you want actually to show the static text one after another. If you just place a while loop you can see that last value only. Try with a timer updating the window text in regular intervals say 1 sec Rinu Raj

      G 1 Reply Last reply
      0
      • R Rinu_Raj

        What you want actually to show the static text one after another. If you just place a while loop you can see that last value only. Try with a timer updating the window text in regular intervals say 1 sec Rinu Raj

        G Offline
        G Offline
        gomez_a
        wrote on last edited by
        #3

        No it doesn't work. Please try it. I don't create CStatic it my sorce code, it is on the dialog window. I am setting the new Value by lblRecno.SetWindowText(...) I am setting the CStatic colour by OnCtlColor(...) and the TIMER void CDemoDlg::OnTimer(UINT nIDEvent) { lblLP.Invalidate(); CDialog::OnTimer(nIDEvent); } Regards mwgomez

        R 1 Reply Last reply
        0
        • G gomez_a

          No it doesn't work. Please try it. I don't create CStatic it my sorce code, it is on the dialog window. I am setting the new Value by lblRecno.SetWindowText(...) I am setting the CStatic colour by OnCtlColor(...) and the TIMER void CDemoDlg::OnTimer(UINT nIDEvent) { lblLP.Invalidate(); CDialog::OnTimer(nIDEvent); } Regards mwgomez

          R Offline
          R Offline
          Rinu_Raj
          wrote on last edited by
          #4

          inside the timer do void CDemoDlg::OnTimer(UINT nIDEvent) { static int lp = 0; CString newTxt = ""; ++lp; newTxt.Format("%d", lp); lblRecno.SetWindowText(newTxt); } then Start the timer in the initdialog Rinu Raj

          G 1 Reply Last reply
          0
          • R Rinu_Raj

            inside the timer do void CDemoDlg::OnTimer(UINT nIDEvent) { static int lp = 0; CString newTxt = ""; ++lp; newTxt.Format("%d", lp); lblRecno.SetWindowText(newTxt); } then Start the timer in the initdialog Rinu Raj

            G Offline
            G Offline
            gomez_a
            wrote on last edited by
            #5

            I think there is steel something wrong. Please look at my source code. My dialog window is MOkno.h public: CStatic lblRecno; // for displaying recno CButton btnStart; // button Start int iLicznik; // increment variable CString newTxt; // new value of the text = iLicznik as a Text MOkno.cpp: BOOL MOkno::OnInitDialog() { CDialog::OnInitDialog(); // TODO: SetTimer(ID_TIMER1, 1000, NULL); // my Timer is running every 1 s return TRUE; } HBRUSH MOkno::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) { HBRUSH hbr; if(nCtlColor == CTLCOLOR_STATIC ) { pDC->SetBkMode(TRANSPARENT); pDC->SetTextColor(clBlue); hbr = (HBRUSH) GetStockObject( NULL_BRUSH ); } else { hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor); } return hbr; } void MOkno::OnClose() { KillTimer(ID_TIMER1); CDialog::OnClose(); } On the dialog window tehre is a Start button. When user click on the button: void MOkno::OnBnClickedButtonStart() { iLicznik = 0; newTxt = ""; // suppose: It is a database, and I am reading records // and display the current recno number for(iLicznik = 1; iLicznik <= 1000; iLicznik++) { newTxt.Format("%d", iLicznik); } } void MOkno::OnTimer(UINT nIDEvent) { CDialog::OnTimer(nIDEvent); lblRecno.SetWindowText(newTxt); } Regards mwgomez

            R D 2 Replies Last reply
            0
            • G gomez_a

              I think there is steel something wrong. Please look at my source code. My dialog window is MOkno.h public: CStatic lblRecno; // for displaying recno CButton btnStart; // button Start int iLicznik; // increment variable CString newTxt; // new value of the text = iLicznik as a Text MOkno.cpp: BOOL MOkno::OnInitDialog() { CDialog::OnInitDialog(); // TODO: SetTimer(ID_TIMER1, 1000, NULL); // my Timer is running every 1 s return TRUE; } HBRUSH MOkno::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) { HBRUSH hbr; if(nCtlColor == CTLCOLOR_STATIC ) { pDC->SetBkMode(TRANSPARENT); pDC->SetTextColor(clBlue); hbr = (HBRUSH) GetStockObject( NULL_BRUSH ); } else { hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor); } return hbr; } void MOkno::OnClose() { KillTimer(ID_TIMER1); CDialog::OnClose(); } On the dialog window tehre is a Start button. When user click on the button: void MOkno::OnBnClickedButtonStart() { iLicznik = 0; newTxt = ""; // suppose: It is a database, and I am reading records // and display the current recno number for(iLicznik = 1; iLicznik <= 1000; iLicznik++) { newTxt.Format("%d", iLicznik); } } void MOkno::OnTimer(UINT nIDEvent) { CDialog::OnTimer(nIDEvent); lblRecno.SetWindowText(newTxt); } Regards mwgomez

              R Offline
              R Offline
              Rinu_Raj
              wrote on last edited by
              #6

              There are some problems in your code You may modify code like this void MOkno::OnBnClickedButtonStart() { SetTimer( ...) // Set the timer here not in the oninitdialog } void MOkno::OnTimer(UINT nIDEvent) { static int i =0; newTxt.Format( "%d", ++i ); lblRecno.SetWindowText(csTex); CDialog::OnTimer(nIDEvent); } Rinu Raj

              G 1 Reply Last reply
              0
              • R Rinu_Raj

                There are some problems in your code You may modify code like this void MOkno::OnBnClickedButtonStart() { SetTimer( ...) // Set the timer here not in the oninitdialog } void MOkno::OnTimer(UINT nIDEvent) { static int i =0; newTxt.Format( "%d", ++i ); lblRecno.SetWindowText(csTex); CDialog::OnTimer(nIDEvent); } Rinu Raj

                G Offline
                G Offline
                gomez_a
                wrote on last edited by
                #7

                Thank you for yuour help, I must find the reason... Regards mwgomez

                1 Reply Last reply
                0
                • G gomez_a

                  I think there is steel something wrong. Please look at my source code. My dialog window is MOkno.h public: CStatic lblRecno; // for displaying recno CButton btnStart; // button Start int iLicznik; // increment variable CString newTxt; // new value of the text = iLicznik as a Text MOkno.cpp: BOOL MOkno::OnInitDialog() { CDialog::OnInitDialog(); // TODO: SetTimer(ID_TIMER1, 1000, NULL); // my Timer is running every 1 s return TRUE; } HBRUSH MOkno::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) { HBRUSH hbr; if(nCtlColor == CTLCOLOR_STATIC ) { pDC->SetBkMode(TRANSPARENT); pDC->SetTextColor(clBlue); hbr = (HBRUSH) GetStockObject( NULL_BRUSH ); } else { hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor); } return hbr; } void MOkno::OnClose() { KillTimer(ID_TIMER1); CDialog::OnClose(); } On the dialog window tehre is a Start button. When user click on the button: void MOkno::OnBnClickedButtonStart() { iLicznik = 0; newTxt = ""; // suppose: It is a database, and I am reading records // and display the current recno number for(iLicznik = 1; iLicznik <= 1000; iLicznik++) { newTxt.Format("%d", iLicznik); } } void MOkno::OnTimer(UINT nIDEvent) { CDialog::OnTimer(nIDEvent); lblRecno.SetWindowText(newTxt); } Regards mwgomez

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

                  Your code is doing exactly what it is supposed to do. Paint-related messages are low priority so they do not get handled if other, high-priority messages are in the queue. This is why you do not see the lblRecno control being updated until the end. Lose the timer stuff and try:

                  void MOkno::OnBnClickedButtonStart()
                  {
                  for(int iLicznik = 1; iLicznik <= 1000; iLicznik++)
                  {
                  CString newTxt;
                  newTxt.Format("%d", iLicznik);
                  lblRecno.SetWindowText(newTxt);
                  }
                  }

                  I just tried this (using 10000) and it worked fine.


                  "Talent without discipline is like an octopus on roller skates. There's plenty of movement, but you never know if it's going to be forward, backwards, or sideways." - H. Jackson Brown, Jr.

                  "Judge not by the eye but by the heart." - Native American Proverb

                  1 Reply Last reply
                  0
                  • G gomez_a

                    I have an MFC application (VS 2003 Std). There is a dialog window with CStaticText. For example: CStatic lblRecno; I want to change the colour of the CStaticText. I am using the OnCtlColor function: HBRUSH DlgBackup::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) { HBRUSH hbr = NULL; /* if(pWnd->GetDlgCtrlID() == IDC_STATIC_LP) or */ if( nCtlColor == CTLCOLOR_STATIC ) { pDC->SetTextColor(clBlack); pDC->SetBkMode(TRANSPARENT); hbr = (HBRUSH) GetStockObject( NULL_BRUSH ); } else { hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor); } return hbr; } To this place is OK. Now I am changeing the lblRecno value in the while loop: int lp = 0; CString newTxt = ""; while(...) { ++lp; bewTxt.Format("%d", lp); lblRecno.SetWindowText(newTxt); } But the new value lblRecno is displaying in the place of the previous value, an so on. I think I should to use the WM_ERASEBKGND, but I don't know how to do it. Can you help me. Regards mwgomez Poland

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

                    gomez_a wrote:

                    But the new value lblRecno is displaying in the place of the previous value, an so on.

                    Of course. What are you expecting it to do? :confused:


                    "Talent without discipline is like an octopus on roller skates. There's plenty of movement, but you never know if it's going to be forward, backwards, or sideways." - H. Jackson Brown, Jr.

                    "Judge not by the eye but by the heart." - Native American Proverb

                    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