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. Tired of copy/pasting code.......

Tired of copy/pasting code.......

Scheduled Pinned Locked Moved C / C++ / MFC
c++helpquestionannouncement
4 Posts 4 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.
  • P Offline
    P Offline
    pblais
    wrote on last edited by
    #1

    A long time ago, I needed a way to put information into edit boxes etc. in my MFC dialog applications "on the fly". That is, I didn't want to wait till a function terminated and the particular window was updated/repainted. I wanted to see the data change real time as it was running. I didn't want to use a timer so I came up with the following function. void DisplayNow(int nID, LPCTSTR lpszString) { CWnd* pDISPLAY_Wnd = GetDlgItem(nID); pDISPLAY_Wnd->SetWindowText(lpszString); pDISPLAY_Wnd->UpdateWindow(); } where nID is the ID of the edit box etc. that I want to display the data in I overloaded more versions of the same functions for integers, floats etc. I am only showing the version that does strings here. Anyways these functions have allowed me to display data on the fly and I have been quite happy with them for quite some time now. But, anytime I want to use them, I have to copy/paste them into the application's dialog file. At one point, I tried to wrap them in a class and use them that way but the compiler burped hard at that because the handle to the window (pDISPLAY_Wnd) is not known at compile/link time. So I continued copy/pasting. Recently I tried to put all the functions into a file called DisplayNow.cpp and DisplayNow.h hoping that, even though I couldn't make them into a class, this would prevent me from the copy/paste thing. So DisplayNow.cpp looks like this: #include "stdafx.h" #include "DisplayNow.h" void DisplayNow(int nID, LPCTSTR lpszString) { CWnd* pDISPLAY_Wnd = GetDlgItem(nID); pDISPLAY_Wnd->SetWindowText(lpszString); pDISPLAY_Wnd->UpdateWindow(); } and DisplayNow.h looks like this: void DisplayNow(int nID, LPCTSTR lpszString); Now, when I try to compile, I get this error: DisplayNow.cpp(6) : error C2660: 'GetDlgItem' : function does not take 1 parameters Am I destined to being forced to copy/paste all these functions into very new application that I create????:( Thank you in advance Pierre

    M P S 3 Replies Last reply
    0
    • P pblais

      A long time ago, I needed a way to put information into edit boxes etc. in my MFC dialog applications "on the fly". That is, I didn't want to wait till a function terminated and the particular window was updated/repainted. I wanted to see the data change real time as it was running. I didn't want to use a timer so I came up with the following function. void DisplayNow(int nID, LPCTSTR lpszString) { CWnd* pDISPLAY_Wnd = GetDlgItem(nID); pDISPLAY_Wnd->SetWindowText(lpszString); pDISPLAY_Wnd->UpdateWindow(); } where nID is the ID of the edit box etc. that I want to display the data in I overloaded more versions of the same functions for integers, floats etc. I am only showing the version that does strings here. Anyways these functions have allowed me to display data on the fly and I have been quite happy with them for quite some time now. But, anytime I want to use them, I have to copy/paste them into the application's dialog file. At one point, I tried to wrap them in a class and use them that way but the compiler burped hard at that because the handle to the window (pDISPLAY_Wnd) is not known at compile/link time. So I continued copy/pasting. Recently I tried to put all the functions into a file called DisplayNow.cpp and DisplayNow.h hoping that, even though I couldn't make them into a class, this would prevent me from the copy/paste thing. So DisplayNow.cpp looks like this: #include "stdafx.h" #include "DisplayNow.h" void DisplayNow(int nID, LPCTSTR lpszString) { CWnd* pDISPLAY_Wnd = GetDlgItem(nID); pDISPLAY_Wnd->SetWindowText(lpszString); pDISPLAY_Wnd->UpdateWindow(); } and DisplayNow.h looks like this: void DisplayNow(int nID, LPCTSTR lpszString); Now, when I try to compile, I get this error: DisplayNow.cpp(6) : error C2660: 'GetDlgItem' : function does not take 1 parameters Am I destined to being forced to copy/paste all these functions into very new application that I create????:( Thank you in advance Pierre

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      What if you make a common base class and derive your dialog classes from it - something like:

      class CMyDialog : public CDialog
      {
      public:
      CMyDialog() : CDialog() {}
      explicit CMyDialog(LPCTSTR lpszTemplateName, CWnd* pParentWnd = NULL) :
      CDialog(lpszTemplateName, pParentWnd) {}
      explicit CMyDialog(UINT nIDTemplate, CWnd* pParentWnd = NULL) :
      CDialog(nIDTemplate, pParentWnd) {}

      void DisplayNow(int, LPCTSTR);
      };
      ...
      void CMyDialog::DisplayNow(int nID, LPCTSTR lpszString)
      {
      CWnd* pDISPLAY_Wnd = GetDlgItem(nID);

      //if (pDISPLAY_Wnd && ::IsWindow(*pDISPLAY_Wnd)) *edit* ::IsWindow() call is redundant here
      if (pDISPLAY_Wnd)
      {
      pDISPLAY_Wnd->SetWindowText(lpszString);
      pDISPLAY_Wnd->UpdateWindow();
      }
      }

      -- modified at 18:39 Friday 23rd February, 2007

      "Do you know what it's like to fall in the mud and get kicked... in the head... with an iron boot? Of course you don't, no one does. It never happens. It's a dumb question... skip it."

      1 Reply Last reply
      0
      • P pblais

        A long time ago, I needed a way to put information into edit boxes etc. in my MFC dialog applications "on the fly". That is, I didn't want to wait till a function terminated and the particular window was updated/repainted. I wanted to see the data change real time as it was running. I didn't want to use a timer so I came up with the following function. void DisplayNow(int nID, LPCTSTR lpszString) { CWnd* pDISPLAY_Wnd = GetDlgItem(nID); pDISPLAY_Wnd->SetWindowText(lpszString); pDISPLAY_Wnd->UpdateWindow(); } where nID is the ID of the edit box etc. that I want to display the data in I overloaded more versions of the same functions for integers, floats etc. I am only showing the version that does strings here. Anyways these functions have allowed me to display data on the fly and I have been quite happy with them for quite some time now. But, anytime I want to use them, I have to copy/paste them into the application's dialog file. At one point, I tried to wrap them in a class and use them that way but the compiler burped hard at that because the handle to the window (pDISPLAY_Wnd) is not known at compile/link time. So I continued copy/pasting. Recently I tried to put all the functions into a file called DisplayNow.cpp and DisplayNow.h hoping that, even though I couldn't make them into a class, this would prevent me from the copy/paste thing. So DisplayNow.cpp looks like this: #include "stdafx.h" #include "DisplayNow.h" void DisplayNow(int nID, LPCTSTR lpszString) { CWnd* pDISPLAY_Wnd = GetDlgItem(nID); pDISPLAY_Wnd->SetWindowText(lpszString); pDISPLAY_Wnd->UpdateWindow(); } and DisplayNow.h looks like this: void DisplayNow(int nID, LPCTSTR lpszString); Now, when I try to compile, I get this error: DisplayNow.cpp(6) : error C2660: 'GetDlgItem' : function does not take 1 parameters Am I destined to being forced to copy/paste all these functions into very new application that I create????:( Thank you in advance Pierre

        P Offline
        P Offline
        PJ Arends
        wrote on last edited by
        #3

        As Mark said, or you could just add a third parameter to the DisplayNow function

        void DisplayNow(CWnd *pParent, int nID, LPCTSTR lpszString)
        {
        CWnd *pDISPLAY_Wnd = pParent->GetDlgItem(nID);
        ...
        }

        And in dialog you call it like this:

        DisplayNow(this, ID, _T("Text"));


        You may be right
        I may be crazy
        -- Billy Joel --

        Within you lies the power for good, use it!!!

        1 Reply Last reply
        0
        • P pblais

          A long time ago, I needed a way to put information into edit boxes etc. in my MFC dialog applications "on the fly". That is, I didn't want to wait till a function terminated and the particular window was updated/repainted. I wanted to see the data change real time as it was running. I didn't want to use a timer so I came up with the following function. void DisplayNow(int nID, LPCTSTR lpszString) { CWnd* pDISPLAY_Wnd = GetDlgItem(nID); pDISPLAY_Wnd->SetWindowText(lpszString); pDISPLAY_Wnd->UpdateWindow(); } where nID is the ID of the edit box etc. that I want to display the data in I overloaded more versions of the same functions for integers, floats etc. I am only showing the version that does strings here. Anyways these functions have allowed me to display data on the fly and I have been quite happy with them for quite some time now. But, anytime I want to use them, I have to copy/paste them into the application's dialog file. At one point, I tried to wrap them in a class and use them that way but the compiler burped hard at that because the handle to the window (pDISPLAY_Wnd) is not known at compile/link time. So I continued copy/pasting. Recently I tried to put all the functions into a file called DisplayNow.cpp and DisplayNow.h hoping that, even though I couldn't make them into a class, this would prevent me from the copy/paste thing. So DisplayNow.cpp looks like this: #include "stdafx.h" #include "DisplayNow.h" void DisplayNow(int nID, LPCTSTR lpszString) { CWnd* pDISPLAY_Wnd = GetDlgItem(nID); pDISPLAY_Wnd->SetWindowText(lpszString); pDISPLAY_Wnd->UpdateWindow(); } and DisplayNow.h looks like this: void DisplayNow(int nID, LPCTSTR lpszString); Now, when I try to compile, I get this error: DisplayNow.cpp(6) : error C2660: 'GetDlgItem' : function does not take 1 parameters Am I destined to being forced to copy/paste all these functions into very new application that I create????:( Thank you in advance Pierre

          S Offline
          S Offline
          S Douglas
          wrote on last edited by
          #4

          pblais wrote:

          Tired of copy/pasting code

          Here is some food for thought Avoiding GetDlgItem By Joseph M. Newcomer. [^]


          I'd love to help, but unfortunatley I have prior commitments monitoring the length of my grass. :Andrew Bleakley:

          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