Tired of copy/pasting code.......
-
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 -
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 PierreWhat 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."
-
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 PierreAs 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!!!
-
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