[英]How do I can call each other function in the different view in MFC?
我在MFC中的对话框中进行了查看。 我想从对话框中调用一个函数以进行查看。 如何在不同的视图中调用彼此的函数?
这是代码另外我也附上了链接
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> d:\\ work \\ mfc_test5 \\ mfc_test5 \\ Testview.h(29):错误C2143:语法错误:缺少';' 在'*'之前1> d:\\ work \\ mfc_test5 \\ mfc_test5 \\ Testview.h(29):错误C4430:缺少类型说明符-假定为int。 注意:C ++不支持default-int 1> d:\\ work \\ mfc_test5 \\ mfc_test5 \\ Testview.h(29):错误C4430:缺少类型说明符-假定为int。 注意:C ++不支持default-int 1> 1> Build FAILED。
在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?
};
这是代码另外我也附上了链接
我做了如下。
// 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的Document / View体系结构中,您不会从外部调用View的方法。
获取视图绘制内容的方法是通过更新其内容并调用UpdateAllViews()(可能带有提示(用于优化))通过其Document。
这是基本的c ++。 您有不相关的A类和B类。 您如何从B访问A? 试试这个控制台程序:
class TA {
public:
void foo() {
cout << "TA\n";
}
};
class TB {
public:
TA *A;
TB() {
A = nullptr;
}
void foo() {
if (A)
A->foo();
}
};
int main()
{
TA *a = new TA;
TB *b = new TB;
b->A = a;
b->foo(); //prints "TA"
delete a;
delete b;
return 0;
}
在MFC中相同。 您可以声明一个指向CTestView
的指针,并在对话框类中使用该指针:
class CTestview;
class Cmfc_test5Dlg : public CDialogEx
{
public:
CTestView* pTestView;
...
};
include "TestView.h"
Cmfc_test5Dlg::Cmfc_test5Dlg()
{
pTestView = NULL;
}
每次创建对话框时,都必须设置pTestView
。 例如:
void CTestview::foo()
{
Cmfc_test5Dlg dlg(...);
dlg.pTestView = this;
dlg.DoModal();
}
然后在Cmfc_test5Dlg
任何地方使用pTestView->DoDisplayImage()
。 例如:
void Cmfc_test5Dlg::OnDropFiles(HDROP hDropInfo)
{
...
if (pTestView != NULL)
pTestView->DoDisplayImage();
}
另一种方法是使用全局变量:
CTestview *global_TestView;
CTestview::CTestview()
{
global_TestView = this;
...
}
//---- another cpp file:
extern CTestview *global_TestView;
...
void Cmfc_test5Dlg::OnDropFiles(HDROP hDropInfo)
{
...
if (global_TestView != NULL)
global_TestView->DoDisplayImage();
}
但是由于多种原因,该方法可能会失败。 例如,如果可以有多个CTestview
。
ps,请勿调用CDialogEx::OnDropFiles(hDropInfo);
和CDialogEx::OnDropFiles(hDropInfo);
连续地,这没有任何意义。
因此,您希望在子对话框上放置文件时在视图中显示图像吗?
在对话框的构造函数中提供指向视图的指针作为附加参数,并将其存储在对话框成员变量m_view
。 然后调用它:
m_view->DoDisplayImage();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.