c++ - 如何在MFC 的不同 View 中相互调用函数?

标签 c++ visual-studio-2010 visual-c++ mfc imagemagick

我在 MFC 的对话框中创建了一个 View 。 我想从对话框中调用一个函数来查看。 如何在不同的 View 中调用彼此的函数?

这是代码我也附上了link

void Cmfc_test5Dlg::OnDropFiles(HDROP hDropInfo)
{
int nFiles;
char szPathName[MAX_PATH]; 
CString strFileName;
nFiles = ::DragQueryFile( hDropInfo, 0xFFFFFFFF, szPathName, MAX_PATH );

{
::DragQueryFile(hDropInfo, 0, szPathName, MAX_PATH);
}
::DragFinish(hDropInfo);
CDialog::OnDropFiles(hDropInfo);

DoDisplayImage(); <---Here is My call function.

CDialogEx::OnDropFiles(hDropInfo);

}

这是另一个函数

void CTestview::DoDisplayImage()
{
  CDC *pDC = GetDC();
  if (pDC != NULL && m_Image.isValid() ) 
    {
      CRect rectClient;
      GetClientRect(rectClient);
      pDC->FillSolidRect(rectClient,pDC->GetBkColor());
      // Set up the Windows bitmap header
      BITMAPINFOHEADER bmi;
      bmi.biSize = sizeof(BITMAPINFOHEADER);    // Size of structure
      bmi.biWidth = m_Image.columns();          // Bitmaps width in pixels
      bmi.biHeight = (-1)*m_Image.rows();       // Bitmaps height n pixels
      bmi.biPlanes = 1;                         // Number of planes in the image
       bmi.biBitCount = 32;                      // The number of bits per pixel
       bmi.biCompression = BI_RGB;               // The type of 
       ...

以及从这里调用的 DoDisplayImage 函数

void CTestview::OnDraw(CDC* pDC)
{
    CDocument* pDoc = GetDocument();
    // TODO: add draw code here
    DoDisplayImage();
}

但是,如您所知,我的问题是我无法在 void Cmfc_test5Dlg::OnDropFiles(HDROP hDropInfo) 函数中调用 DoDisplayImage(),我还想在 DoDisplayImage 中获取 OnDropFiles 函数的 szPathName。

我应该怎么做才能解决这个问题?

更新1

我做的时候出现如下错误

1>d:\work\mfc_test5\mfc_test5\Testview.h(29): error C2143: 语法错误:缺少 ';'前 '*' 1>d:\work\mfc_test5\mfc_test5\Testview.h(29): error C4430: 缺少类型说明符 - 假定为 int。注意:C++ 不支持 default-int 1>d:\work\mfc_test5\mfc_test5\Testview.h(29): error C4430: 缺少类型说明符 - 假定为 int。注意:C++ 不支持 default-int 1> 1>构建失败。

在 TestView.h 中,

#pragma once



// CTestview view

class CTestview : public CScrollView
{
    DECLARE_DYNCREATE(CTestview)

protected:
    CTestview();           // protected constructor used by dynamic creation
    virtual ~CTestview();

public:
#ifdef _DEBUG
    virtual void AssertValid() const;
#ifndef _WIN32_WCE
    virtual void Dump(CDumpContext& dc) const;
#endif
#endif

protected:
    virtual void OnDraw(CDC* pDC);      // overridden to draw this view
    virtual void OnInitialUpdate();     // first time after construct

    DECLARE_MESSAGE_MAP()
public:
    CTestView* pTestView; <---- is this right?
};

这是代码我也附上了link

更新2

我做了如下。

// mfc_test5Dlg.h : header file
//

#pragma once
#include "afxcmn.h"


// Cmfc_test5Dlg dialog
class CTestview;//adding
class Cmfc_test5Dlg : public CDialogEx
{
// Construction
public:
    Cmfc_test5Dlg(CWnd* pParent = NULL);    // standard constructor
    CTestview* pTestView;//adding

// Dialog Data
    enum { IDD = IDD_MFC_TEST5_DIALOG };

    protected:
    virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support


Cmfc_test5Dlg::Cmfc_test5Dlg(CWnd* pParent /*=NULL*/)
    : CDialogEx(Cmfc_test5Dlg::IDD, pParent)
    , m_CString(_T(""))
{
    m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
    pTestView = NULL; //adding
}

但在这部分我无法理解如何在我的案例中实现它。

You have to set pTestView each time the dialog is created. For example:

void CTestview::foo()
{
    Cmfc_test5Dlg dlg(...);
    dlg.pTestView = this;
    dlg.DoModal();
}

最佳答案

在 MFC 的文档/ View 架构中,您不能从外部调用 View 的方法。

让 View 绘制内容的方法是通过其文档更新内容并调用 UpdateAllViews(),可能带有提示(用于优化)。

关于c++ - 如何在MFC 的不同 View 中相互调用函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33447514/

相关文章:

c++ - 错误 MSB6006 : "midl.exe" exited with code 2026

visual-studio-2008 - 检查所有库和 dll 是否来自同一个版本?

c++ - 数组中的 SIGABRT C++ 如何转向 <vector>

visual-studio-2010 - 如何使用条件文件复制设置安装程序项目

visual-studio - 警告错误 - 如何摆脱这些

visual-studio-2010 - 从构建输出过滤错误消息

c++ - LNK2019, boost::asio

c# - 以编程方式卸载掌上电脑程序

C++11 将多个共享指针存储为原始指针

C++ 无法使用 fstream 对象读取和写入文本文件