我现在在 BOOL CLoadBmpApp::InitInstance()函数中,只增加了文档模板,没有创建子窗口,是这个原因吧,源代码如下 BOOL CLoadBmpApp::InitInstance() { AfxEnableControlContainer(); // Standard initialization // If you are not using these features and wish to reduce the size // of your final executable, you should remove from the following // the specific initialization routines you do not need.#ifdef _AFXDLL Enable3dControls(); // Call this when using MFC in a shared DLL #else Enable3dControlsStatic(); // Call this when linking to MFC statically #endif // Change the registry key under which our settings are stored. // TODO: You should modify this string to be something appropriate // such as the name of your company or organization. SetRegistryKey(_T("Local AppWizard-Generated Applications")); LoadStdProfileSettings(6); // Load standard INI file options (including MRU) // Register the application's document templates. Document templates // serve as the connection between documents, frame windows and views. CMultiDocTemplate* pDocTemplate; pDocTemplate = new CMultiDocTemplate( IDR_LOADBMTYPE, RUNTIME_CLASS(CLoadBmpDoc), RUNTIME_CLASS(CChildFrame), // custom MDI child frame RUNTIME_CLASS(CLoadBmpView)); AddDocTemplate(pDocTemplate); // create main MDI Frame window CMainFrame* pMainFrame = new CMainFrame; if (!pMainFrame->LoadFrame(IDR_MAINFRAME)) return FALSE; m_pMainWnd = pMainFrame; pDocTemplate = new CMultiDocTemplate( IDR_HEIGHT, RUNTIME_CLASS(CLoadBmpDoc), RUNTIME_CLASS(CChildFrame), // custom MDI child frame RUNTIME_CLASS(HeightView)); if(!pDocTemplate) return FALSE; AddDocTemplate(pDocTemplate);
// Parse command line for standard shell commands, DDE, file open CCommandLineInfo cmdInfo; ParseCommandLine(cmdInfo); // Dispatch commands specified on the command line if (!ProcessShellCommand(cmdInfo)) return FALSE; // The main window has been initialized, so show and update it. pMainFrame->ShowWindow(m_nCmdShow); pMainFrame->UpdateWindow(); return TRUE; }我是否应该新建子窗口,模仿上面主框架的语句 CChildFrame* pChildFrame = new CChildFrame; if(!pChildFrame->LoadFrame(IDR_HEIGHT)) return FALSE; pChildFrame->ShowWindow(); pChildFrame->UpdateWindow(); 写一下类似的窗口显示语句呢?我再琢磨琢磨
RUNTIME_CLASS(CMyDoc),
RUNTIME_CLASS(CChildFrame), // 自定义 MDI 子框架
RUNTIME_CLASS(CMyView));
if (!pMyTemplate)
return FALSE;
AddDocTemplate(pMyTemplate);
CFileDialog dlg(TRUE,NULL,NULL,NULL,ImageStyle,NULL);
//dlg.m_ofn.Flags |=OFN_ALLOWMULTISELECT|OFN_EXPLORER;//同时打开多幅图像 CString sBuffer;
dlg.GetOFN().lpstrFile = sBuffer.GetBuffer(10000); // Strictly, the buffer size should be something like this: (c_cMaxFiles * (MAX_PATH + 1)) + 1
dlg.GetOFN().nMaxFile = 10000; POSITION pos; if(dlg.DoModal()==IDOK)
{
pos=dlg.GetStartPosition(); CString str[100];
CString FileExt[100]; int n=-1,i;
while(pos!=NULL)
{
n++;
str[n]=dlg.GetNextPathName(pos);
} for(i=0;i<=n;i++)
{
for(int j=i;j<=n;j++)
{
if( (str[i].GetLength()==str[j].GetLength() && strcmp((char *)(LPCTSTR)str[i],(char *)(LPCTSTR)str[j])<0) || str[i].GetLength()<str[j].GetLength() )
{
str[n+1]=str[j];
str[j]=str[i];
str[i]=str[n+1];
}
}
//由文件路径转换为文件扩展名
FileExt[i]=str[i].Right(str[i].GetLength()-str[i].ReverseFind('.')-1);
} CMultiDocTemplate* pDoc;
for(i=0;i<=n;i++)
{
if ((FileExt[i]==_T("bmp"))||(FileExt[i]==_T("BMP")))
{
pDoc=pImageTemplate;//自己创建文档模板指针
pImageTemplate->OpenDocumentFile(str[i]);
}
else
{
AfxMessageBox(_T("不支持的文件类型!"));
return;
}
}
//Order-open files END sBuffer.ReleaseBuffer();
}
else
{
sBuffer.ReleaseBuffer();
return;
} return;
virtual BOOL OnOpenDocument(LPCTSTR lpszPathName);
其中lpszPathName就是你刚才传递过来的路径str[i]
在OnOpenDocument里面添加图像信息读取代码最后在View的OnDraw中添加
CMyDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc)
return;其中pDoc即为文档类的指针,剩下的就是自己去调用显示了。
CMultiDocTemplate* pHeightTemplate;
pHeightTemplate = new CMultiDocTemplate(
IDR_HEIGHT,
RUNTIME_CLASS(CLoadBmpDoc),
RUNTIME_CLASS(CChildFrame), // custom MDI child frame
RUNTIME_CLASS(HeightView));
AddDocTemplate(pHeightTemplate);编译通过了,我想要非模态的对话框,并且要打开的图像只有一副,可以在HeightView中打开就行,现在增加了一个菜单项“Height”
void CLoadBmpApp::OnHeight()
{
//CLoadBmpDlg* dlg = new CLoadBmpDlg;
// CAboutDlg* dlg = new CAboutDlg;
// dlg->Create(IDD_HEIGHT,this);
// dlg->ShowWindow(SW_SHOW);
AfxMessageBox("chulai");
CMultiDocTemplate* pDoc;
pDoc = pHeightTemplate;
}
按照您的做法应该是这么添加吧?但是报错了
Compiling...
LoadBmp.cpp
D:\track\Multi-document20110820\LoadBmp\LoadBmp.cpp(182) : error C2065: 'pHeightTemplate' : undeclared identifier
D:\track\Multi-document20110820\LoadBmp\LoadBmp.cpp(182) : error C2440: '=' : cannot convert from 'int' to 'class CMultiDocTemplate *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
Error executing cl.exe.LoadBmp.exe - 2 error(s), 0 warning(s)
请问该如何处理?
在.h文件里面定义,你现在定义成局部的了,外界无法调用。
doc类里面的虚函数:
OnOpenDocument(LPCTSTR lpszPathName);
其中lpszPathName就是你从APP中传递过来的路径。
你可以在doc类的.h文件中定义一个CString类型的变量m_strPath;
然后再在OnOpenDocument(LPCTSTR lpszPathName)的实现函数里面添加
m_strPath=lpszPathName;最后:
CMyDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc)
return;pDoc->m_strPath就是路径信息了,你就可以在view类里面根据文件路径读取数据了。
pDocTemplate = new CMultiDocTemplate(
IDR_HEIGHT,
RUNTIME_CLASS(CLoadBmpDoc),
RUNTIME_CLASS(CChildFrame), // custom MDI child frame
RUNTIME_CLASS(HeightView));
if(!pDocTemplate)
return FALSE;
AddDocTemplate(pDocTemplate);
上面的IDR_HEIGHT是我新建的菜单,CLoadBmpDoc是唯一的DOC,我不想增加,已经存在了,CChildFrame就是照抄下来的,我就是新建了一个HeightView类,但是现在在
void HeightView::OnDraw(CDC* pDC)
{
CLoadBmpDoc* pDoc = GetDocument();
// TODO: add draw code here
ASSERT_VALID(pDoc);
if(!pDoc)
return;
CPen pen(PS_SOLID,3,RGB(255,0,0));
CPen *pOldPen = pDC->SelectObject(&pen);
pDC->MoveTo(0,0);
pDC->LineTo(4000,4000);
// CPen
}
会报错
error C2440: 'initializing' : cannot convert from 'class CDocument *' to 'class CLoadBmpDoc *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
默认的是
oid HeightView::OnDraw(CDC* pDC)
{
CDocument* pDoc = GetDocument();
void HeightView::OnDraw(CDC* pDC)
{
CLoadBmpDoc* pDoc = (CLoadBmpDoc*)GetDocument();//强制转换
ASSERT_VALID(pDoc);
if(!pDoc)
return;
CPen pen(PS_SOLID,3,RGB(255,0,0));
CPen *pOldPen = pDC->SelectObject(&pen);
pDC->MoveTo(0,0);
pDC->LineTo(4000,4000);
// CPen
}
现在不报错了,但是也没有出现新的窗口啊,画线也没有,是不是和我没有创建新文档,而是用了原来的主文档LoadBmpDoc有关呢??
BOOL CLoadBmpApp::InitInstance()函数中,只增加了文档模板,没有创建子窗口,是这个原因吧,源代码如下
BOOL CLoadBmpApp::InitInstance()
{
AfxEnableControlContainer(); // Standard initialization
// If you are not using these features and wish to reduce the size
// of your final executable, you should remove from the following
// the specific initialization routines you do not need.#ifdef _AFXDLL
Enable3dControls(); // Call this when using MFC in a shared DLL
#else
Enable3dControlsStatic(); // Call this when linking to MFC statically
#endif // Change the registry key under which our settings are stored.
// TODO: You should modify this string to be something appropriate
// such as the name of your company or organization.
SetRegistryKey(_T("Local AppWizard-Generated Applications")); LoadStdProfileSettings(6); // Load standard INI file options (including MRU) // Register the application's document templates. Document templates
// serve as the connection between documents, frame windows and views. CMultiDocTemplate* pDocTemplate;
pDocTemplate = new CMultiDocTemplate(
IDR_LOADBMTYPE,
RUNTIME_CLASS(CLoadBmpDoc),
RUNTIME_CLASS(CChildFrame), // custom MDI child frame
RUNTIME_CLASS(CLoadBmpView));
AddDocTemplate(pDocTemplate); // create main MDI Frame window
CMainFrame* pMainFrame = new CMainFrame;
if (!pMainFrame->LoadFrame(IDR_MAINFRAME))
return FALSE;
m_pMainWnd = pMainFrame; pDocTemplate = new CMultiDocTemplate(
IDR_HEIGHT,
RUNTIME_CLASS(CLoadBmpDoc),
RUNTIME_CLASS(CChildFrame), // custom MDI child frame
RUNTIME_CLASS(HeightView));
if(!pDocTemplate)
return FALSE;
AddDocTemplate(pDocTemplate);
// Parse command line for standard shell commands, DDE, file open
CCommandLineInfo cmdInfo;
ParseCommandLine(cmdInfo); // Dispatch commands specified on the command line
if (!ProcessShellCommand(cmdInfo))
return FALSE; // The main window has been initialized, so show and update it.
pMainFrame->ShowWindow(m_nCmdShow);
pMainFrame->UpdateWindow();
return TRUE;
}我是否应该新建子窗口,模仿上面主框架的语句
CChildFrame* pChildFrame = new CChildFrame;
if(!pChildFrame->LoadFrame(IDR_HEIGHT))
return FALSE;
pChildFrame->ShowWindow();
pChildFrame->UpdateWindow();
写一下类似的窗口显示语句呢?我再琢磨琢磨