各位大侠,我通过VC6.0建了一个多文档的程序,现在想通过菜单项中的一个选项,能够关联一个新的视图类,通过这个视图类显示图像,请问如何操作呢?请详细一些,谢谢各位

解决方案 »

  1.   

    参考App类InitInstance函数中的CMultiDocTemplate,然后AddDocTemplate添加到文档模板中去。
      

  2.   

    pMyTemplate = new CMultiDocTemplate(IDR_MyTYPE,
    RUNTIME_CLASS(CMyDoc),
    RUNTIME_CLASS(CChildFrame), // 自定义 MDI 子框架
    RUNTIME_CLASS(CMyView));
    if (!pMyTemplate)
    return FALSE;
    AddDocTemplate(pMyTemplate);
      

  3.   

    感谢 楼上两位,我现在将你们说的模板添加进去了,现在也定义了一个新的类 MyView,在主菜单中添加了一个菜单项 ID_SHOW ,现在有了一个函数 void MyView::OnShow() 我想在MyView 中的OnDraw()中读入图像并且显示,怎么和 ID_SHOW有响应呢? 
      

  4.   

    读入图像最好在APP类中实现,可以通过读取扩展名的方法,判断系统打开哪一个View,然后将图像的路径传递给Doc类,让他来读取图像的基本信息,最后在View类的OnDraw中调用Doc中的参数来显示图像。
      

  5.   

    在APP类的OnFileOpen()中添加如下代码:CString ImageStyle=_T("Bitmap(*.bmp)|*.bmp||");
    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;
      

  6.   

    在对应的Doc类里面添加
    virtual BOOL OnOpenDocument(LPCTSTR lpszPathName);
    其中lpszPathName就是你刚才传递过来的路径str[i]
    在OnOpenDocument里面添加图像信息读取代码最后在View的OnDraw中添加
    CMyDoc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);
    if (!pDoc)
       return;其中pDoc即为文档类的指针,剩下的就是自己去调用显示了。
      

  7.   

    楼上大侠,请问 我就是想在VIEW中读取数据呢,有办法么?
      

  8.   

    我现在在BOOL CLoadBmpApp::InitInstance()下添加了模板,
    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)
    请问该如何处理?
      

  9.   

    CMultiDocTemplate* pHeightTemplate;
    在.h文件里面定义,你现在定义成局部的了,外界无法调用。
      

  10.   


    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类里面根据文件路径读取数据了。
      

  11.   

    我现在是这么想的,我的一个窗口只需要读取一个图片,所以就不想用OnOpenDocument函数了,所以在新增模板的时候
    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
      

  12.   

    新建的HeightView中的OnDraw()
    默认的是
    oid HeightView::OnDraw(CDC* pDC)
    {
    CDocument* pDoc = GetDocument();
      

  13.   

    我用了强制转换
    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有关呢??
      

  14.   

    我现在在
    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();
    写一下类似的窗口显示语句呢?我再琢磨琢磨