Using DC in added class (outside CView class)
-
Hello. I can use CClientDC and its TextOut function in C~~View class. It's quite simple like this:
void C~~View::OnLButtonDown(UINT nFlags, CPoint point)
{
CClientDC dc(this);
dc.TextOut(x, y, "..");
}Yet when I use this function in my 'added' class, which is not derived class of CView, an error occurs. like this:
void CMyclass::memfunc()
{
CClientDC dc(this);
dc.TextOut(x, y, "..");
}So I changed the code like this:
\[C~~View.cpp\]
void C~~View::OnLButtonDown(UINT nFlags, CPoint point)
{
CClientDC dc(this);
CMyclass myclass;
myclass.memfunc(dc);
}
[CMyclass.cpp]
void CMyclass::memfunc(CClientDC dc)
{
dc.TextOut(x, y, "..");
}And errors still occur. Of course, I linked the header file of my class to C~~View.cpp. I want to display Text or Images on window with code in 'my class'. Please give me advice or ideas. Thank you.
May the sky bring you a full measure of health and prosperity.
-
Hello. I can use CClientDC and its TextOut function in C~~View class. It's quite simple like this:
void C~~View::OnLButtonDown(UINT nFlags, CPoint point)
{
CClientDC dc(this);
dc.TextOut(x, y, "..");
}Yet when I use this function in my 'added' class, which is not derived class of CView, an error occurs. like this:
void CMyclass::memfunc()
{
CClientDC dc(this);
dc.TextOut(x, y, "..");
}So I changed the code like this:
\[C~~View.cpp\]
void C~~View::OnLButtonDown(UINT nFlags, CPoint point)
{
CClientDC dc(this);
CMyclass myclass;
myclass.memfunc(dc);
}
[CMyclass.cpp]
void CMyclass::memfunc(CClientDC dc)
{
dc.TextOut(x, y, "..");
}And errors still occur. Of course, I linked the header file of my class to C~~View.cpp. I want to display Text or Images on window with code in 'my class'. Please give me advice or ideas. Thank you.
May the sky bring you a full measure of health and prosperity.
You should pass the device context by reference, i.e. change your function definition (remember to change the declaration too):
void CMyclass::memfunc(CClientDC & dc)
{
dc.TextOut(x, y, "..");
}:)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
Hello. I can use CClientDC and its TextOut function in C~~View class. It's quite simple like this:
void C~~View::OnLButtonDown(UINT nFlags, CPoint point)
{
CClientDC dc(this);
dc.TextOut(x, y, "..");
}Yet when I use this function in my 'added' class, which is not derived class of CView, an error occurs. like this:
void CMyclass::memfunc()
{
CClientDC dc(this);
dc.TextOut(x, y, "..");
}So I changed the code like this:
\[C~~View.cpp\]
void C~~View::OnLButtonDown(UINT nFlags, CPoint point)
{
CClientDC dc(this);
CMyclass myclass;
myclass.memfunc(dc);
}
[CMyclass.cpp]
void CMyclass::memfunc(CClientDC dc)
{
dc.TextOut(x, y, "..");
}And errors still occur. Of course, I linked the header file of my class to C~~View.cpp. I want to display Text or Images on window with code in 'my class'. Please give me advice or ideas. Thank you.
May the sky bring you a full measure of health and prosperity.
If you check the documentation you see that CClientDC's constructor[^] gets a CWnd (or derivate) pointer, my guess would be that your own class has nothing to do with CWnd. Explain a bit more what you want to do.
> The problem with computers is that they do what you tell them to do and not what you want them to do. < > Life: great graphics, but the gameplay sux. <
modified on Monday, February 23, 2009 8:59 AM
-
You should pass the device context by reference, i.e. change your function definition (remember to change the declaration too):
void CMyclass::memfunc(CClientDC & dc)
{
dc.TextOut(x, y, "..");
}:)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]Thank you so much! You saved me! :)
May the sky bring you a full measure of health and prosperity.
-
Thank you so much! You saved me! :)
May the sky bring you a full measure of health and prosperity.
You are welcome. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
If you check the documentation you see that CClientDC's constructor[^] gets a CWnd (or derivate) pointer, my guess would be that your own class has nothing to do with CWnd. Explain a bit more what you want to do.
> The problem with computers is that they do what you tell them to do and not what you want them to do. < > Life: great graphics, but the gameplay sux. <
modified on Monday, February 23, 2009 8:59 AM
What I meant was : In MFC single document, I want to show Text on main window. And I also want this function to be operated in my 'own' class(which is a derived class of CWnd), not in CView class. Pallini's answer is the best solution to my question (how nice of him/her :)). It works well. And now I've found another solution.
void CMyclass::memfunc()
{
AfxGetApp()->m_pMainWnd->GetDC()->TextOut(x, y, "..");
}Thank you for your kind answer.
May the sky bring you a full measure of health and prosperity.
-
What I meant was : In MFC single document, I want to show Text on main window. And I also want this function to be operated in my 'own' class(which is a derived class of CWnd), not in CView class. Pallini's answer is the best solution to my question (how nice of him/her :)). It works well. And now I've found another solution.
void CMyclass::memfunc()
{
AfxGetApp()->m_pMainWnd->GetDC()->TextOut(x, y, "..");
}Thank you for your kind answer.
May the sky bring you a full measure of health and prosperity.
I see. Sorry if i misunderstood anything, good luck with your project. :)
> The problem with computers is that they do what you tell them to do and not what you want them to do. < > Life: great graphics, but the gameplay sux. <