我在程序里加了一个“打开样图”的菜单,希望从这里打开图形文件,没有实现,如果在classwizard自己生成的OpenDocument里就没问题,这是我的代码,是不是还要加点什么????
BOOL CMyWorkDoc::OnOpenDocument(LPCTSTR lpszPathName) ///这个函数可以打开图形文件
{
if (!CDocument::OnOpenDocument(lpszPathName))
return FALSE;
OFSTRUCT of;
HANDLE hFile=NULL;
hFile=(HANDLE)OpenFile(lpszPathName,&of,OF_READWRITE|OF_SHARE_COMPAT);
TRACE(lpszPathName);
if(hFile!=(HANDLE)HFILE_ERROR)
{
TRACE("SHAN CHU\n");
DWORD dwHighWord=NULL;
DWORD dwSizeLow=GetFileSize(hFile,&dwHighWord);
DWORD dwFileSize=dwSizeLow;
TRACE("dwFileSize=%d\n",dwFileSize);
if(dwFileSize==0)
{
TRACE("DA KAI\n");
POSITION viewPos=this->GetFirstViewPosition();
CMyWorkView* MyView=(CMyWorkView*)this->GetNextView(viewPos);
MyView->m_ObjectList.RemoveAll();
}
}
::CloseHandle(hFile);
return TRUE;
}
void CMyWorkDoc::On_MenuPicture_Read()///一样的代码在这里就不可以了
{
CFileDialog dlg(TRUE,"pic","*.pic");
if(dlg.DoModal()==IDOK)
{
OFSTRUCT of;
HANDLE hFile=NULL;
hFile=(HANDLE)OpenFile(dlg.GetPathName(),&of,OF_READWRITE|OF_SHARE_COMPAT);
if(hFile!=(HANDLE)HFILE_ERROR)
{
DWORD dwHighWord=NULL;
DWORD dwSizeLow=GetFileSize(hFile,&dwHighWord);
DWORD dwFileSize=dwSizeLow;
TRACE("dwFileSize=%d\n",dwFileSize);
if(dwFileSize==0)
{
POSITION viewPos=this->GetFirstViewPosition();
CMyWorkView* MyView=(CMyWorkView*)this->GetNextView(viewPos);
MyView->m_ObjectList.RemoveAll();
}
}
::CloseHandle(hFile);
CFile file;
// VERIFY(file.Open(dlg.GetPathName(),CFile::modeRead));
file.Open(dlg.GetPathName(),CFile::modeRead);
file.Close();
}
你可以看看MFC程序设计,关于文档和视图这一章
[此贴子已经被作者于2006-10-14 19:38:27编辑过]
虽然不是很明白怎么回事,但是实现了我要的效果,可以读入文件了
void CMyWorkDoc::On_MenuPicture_Read()
{
static char szFilter[]="PIC Files(*.pic)|*.pic||";
CFileDialog FileDlg(TRUE,NULL,NULL,OFN_HIDEREADONLY,szFilter);
CString filename;
if(FileDlg.DoModal()==IDOK)
{
filename=FileDlg.GetPathName();
}
OFSTRUCT of;
HANDLE hFile=NULL;
hFile=(HANDLE)OpenFile(filename,&of,OF_READWRITE|OF_SHARE_COMPAT);
if(hFile!=(HANDLE)HFILE_ERROR)
{
DWORD dwHighWord=NULL;
DWORD dwSizeLow=GetFileSize(hFile,&dwHighWord);
DWORD dwFileSize=dwSizeLow;
TRACE("dwFileSize=%d\n",dwFileSize);
if(dwFileSize==0)
{
POSITION viewPos=this->GetFirstViewPosition();
CMyWorkView* MyView=(CMyWorkView*)this->GetNextView(viewPos);
MyView->m_ObjectList.RemoveAll();
}
}
::CloseHandle(hFile);
CFile file;
CFileException fe;
//以读方式打开文件
if(!file.Open(filename,CFile::modeRead,&fe))
{
fe.ReportError();
return;
}
//构建CArchive 对象
CArchive ar(&file,CArchive::load);
Serialize(ar);
ar.Close();
file.Close();
CMainFrame *pMain=(CMainFrame *)AfxGetApp()->m_pMainWnd;
CMyWorkView *pView=(CMyWorkView *)pMain->GetActiveView();
pView->Invalidate();
}
这样更简单,直接调用CDocument::OnOpenDocument
void CMyDrawDoc::OnMENUITEMReadPic()
{
static char szFilter[]="PIC Files(*.liu)|*.liu||";
CFileDialog FileDlg(TRUE,NULL,NULL,OFN_HIDEREADONLY,szFilter);
CString filename;
if(FileDlg.DoModal()==IDOK)
{
filename=FileDlg.GetPathName();
}
//定义文件对象和文件异常对象
CDocument::OnOpenDocument(filename) ;
CMainFrame *pMain=(CMainFrame *)AfxGetApp()->m_pMainWnd;
CMyDrawView *pView=(CMyDrawView *)pMain->GetActiveView();
pView->Invalidate();
}