大家好,我在基于对话框MFC应用程序中自定义一个Key类(并没有父类,不知这样说可不可以),把主对话框中的一段程序拷过来编译会出很多错误,请问怎么解决。是要包含一些头文件过去,还是要先获得主对话框指针之类的。初学者,请勿见笑。#include "stdafx.h"
#include "Key.h"#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////Key::Key()
{
caret_x=0;
caret_y=0;
}Key::~Key()
{
}
void Key::ProcessKey()
{
CString str="3.txt";
m_output.HideCaret();
CFile file(str,CFile::modeRead);
CArchive ar1(&file,CArchive::load);
int i2;
char ch2;
float f2;
CString str2;
CString Result;
ar1.ReadString(Result);
CWnd* pWnd0=AfxGetMainWnd();
CWnd* pWnd=pWnd0->GetDlgItem(IDC_STATICPUTOUT);
pWnd->SetWindowText(Result);
POINT point;
caret_x=0;
caret_y=0;
point.x= caret_x;
point.y= caret_y;
int cxChr; //字符宽度
TEXTMETRIC tm;
HDC hdc;
hdc=::GetDC(m_output.m_hWnd);
SelectObject(hdc,GetStockObject(SYSTEM_FIXED_FONT));
GetTextMetrics(hdc,&tm);
m_output.CreateSolidCaret(tm.tmAveCharWidth/8,tm.tmAscent);
m_output.SetCaretPos(point);
m_output.ShowCaret();
}E:\work\project\k1000\Key.cpp(35) : error C2065: 'm_output' : undeclared identifier
E:\work\project\k1000\Key.cpp(35) : error C2228: left of '.HideCaret' must have class/struct/union type
E:\work\project\k1000\Key.cpp(55) : error C2228: left of '.m_hWnd' must have class/struct/union type
E:\work\project\k1000\Key.cpp(58) : error C2228: left of '.CreateSolidCaret' must have class/struct/union type
E:\work\project\k1000\Key.cpp(59) : error C2228: left of '.SetCaretPos' must have class/struct/union type
E:\work\project\k1000\Key.cpp(60) : error C2228: left of '.ShowCaret' must have class/struct/union type
MyButton.cpp
Generating Code...
Skipping... (no relevant changes detected)
k1000.cpp
k1000Dlg.cpp
MyStatic.cpp
Error executing cl.exe.
Creating browse info file...k1000.exe - 6 error(s), 0 warning(s)
#include "Key.h"#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////Key::Key()
{
caret_x=0;
caret_y=0;
}Key::~Key()
{
}
void Key::ProcessKey()
{
CString str="3.txt";
m_output.HideCaret();
CFile file(str,CFile::modeRead);
CArchive ar1(&file,CArchive::load);
int i2;
char ch2;
float f2;
CString str2;
CString Result;
ar1.ReadString(Result);
CWnd* pWnd0=AfxGetMainWnd();
CWnd* pWnd=pWnd0->GetDlgItem(IDC_STATICPUTOUT);
pWnd->SetWindowText(Result);
POINT point;
caret_x=0;
caret_y=0;
point.x= caret_x;
point.y= caret_y;
int cxChr; //字符宽度
TEXTMETRIC tm;
HDC hdc;
hdc=::GetDC(m_output.m_hWnd);
SelectObject(hdc,GetStockObject(SYSTEM_FIXED_FONT));
GetTextMetrics(hdc,&tm);
m_output.CreateSolidCaret(tm.tmAveCharWidth/8,tm.tmAscent);
m_output.SetCaretPos(point);
m_output.ShowCaret();
}E:\work\project\k1000\Key.cpp(35) : error C2065: 'm_output' : undeclared identifier
E:\work\project\k1000\Key.cpp(35) : error C2228: left of '.HideCaret' must have class/struct/union type
E:\work\project\k1000\Key.cpp(55) : error C2228: left of '.m_hWnd' must have class/struct/union type
E:\work\project\k1000\Key.cpp(58) : error C2228: left of '.CreateSolidCaret' must have class/struct/union type
E:\work\project\k1000\Key.cpp(59) : error C2228: left of '.SetCaretPos' must have class/struct/union type
E:\work\project\k1000\Key.cpp(60) : error C2228: left of '.ShowCaret' must have class/struct/union type
MyButton.cpp
Generating Code...
Skipping... (no relevant changes detected)
k1000.cpp
k1000Dlg.cpp
MyStatic.cpp
Error executing cl.exe.
Creating browse info file...k1000.exe - 6 error(s), 0 warning(s)
把它当作参数传进ProcessKey()里
自己写也可以啊不需要这些东西的#include "stdafx.h"
你要什么就包什么,不要的就不要包
我这个类就是想封装一些数据,还有就是对主对话框上一些控件进行控制。能实现吗,也就是说我能得到主对话框上的那些控件的指针,并对他们进行控制吗。m_output这个变量的确是主对话框上一个控件的关联量。它的类型是CMybutton类(派生于CButton)
当作参数传进ProcessKey()里我试试,呵呵。但是如果部传递参数,还有别的解决办法吗?
再次谢谢大家
CWnd* pWnd=::AfxGetMainWnd();
CMyStatic *m_output;
m_output=(CMyStatic *)pWnd->GetDlgItem(IDC_STATICPUTOUT);
这样就可以了
再次感谢大家