I have .H file and .Cpp file for a dialog Print that contains two buttons Printer 1 and Printer 2 and I want to call a method in another class function when Printer 1 is pressed.
.H File
#pragma once
#ifndef PRINTCHOOSEDLG_H
#define PRINTCHOOSEDLG_H
class CPrintChooseDlg : public CTungstenDlg
{
public:
CPrintChooseDlg(CWnd* pParent = NULL);
enum { IDD = IDD_PRINTBOX };
protected:
virtual void DoDataExchange(CDataExchange* pDX);
protected:
afx_msg void OnPrinter1();
afx_msg void OnPrinter2();
//virtual void OnPrinter1();
//virtual void OnPrinter2();
DECLARE_MESSAGE_MAP()
};
#endif
.Cpp
#include "stdafx.h"
#include "Tungsten.h"
#include "PrintChooseDlg.h"
CPrintChooseDlg::CPrintChooseDlg(CWnd* pParent /*=NULL*/)
: CDialog(CPrintChooseDlg::IDD, pParent)
{
}
void CPrintChooseDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CPrintChooseDlg, CDialog)
ON_BN_CLICKED(IDC_PRINTER1,OnPrinter1)
ON_BN_CLICKED(IDC_PRINTER2,OnPrinter2)
END_MESSAGE_MAP()
void CPrintChooseDlg::OnPrinter1()
{
CTungstenDlg aux;
aux.OnOK();
}
void CPrintChooseDlg::OnPrinter2()
{
CTungstenDlg aux;
aux.OnOK();
}
How can i call the method CTungsten::OnOK inside the function CPrintChooseDlg::OnPrinter1()
It gives me an error of course because it is not defined in the PrintChooseDlg.cpp
What i tried: is to include guards in header and use suggested solutions in comments
Thanks in Advance
Related
I have structure: PrintChooseDlg.h
#ifndef PRINTCHOOSEDLG_H
#define PRINTCHOOSEDLG_H
#include <string>
#pragma once
#endif
class CPrintChooseDlg : public CDialog
{
public:
int choosing;
/*afx_msg void OnPrinter1();
afx_msg void OnPrinter2();*/
CPrintChooseDlg(CWnd* pParent = NULL);
enum { IDD = IDD_PRINTBOX };
protected:
virtual void DoDataExchange(CDataExchange* pDX);
protected:
//afx_msg void OnPrinter1();
//afx_msg void OnPrinter2();
virtual void OnPrinter1();
virtual void OnPrinter2();
DECLARE_MESSAGE_MAP()
};
and PrintChooseDlg.cpp
// PrintChoose.cpp : implementation file
//
#include "stdafx.h"
#include "Tungsten.h"
#include "PrintChooseDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
// PrintChoose
//IMPLEMENT_DYNAMIC(PrintChoose, CWnd)
CPrintChooseDlg::CPrintChooseDlg(CWnd* pParent /*=NULL*/)
: CDialog(CPrintChooseDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CChooseLabelDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
}
void CPrintChooseDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CChooseLabelDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CPrintChooseDlg, CDialog)
ON_BN_CLICKED(IDC_PRINTER1,OnPrinter1)
ON_BN_CLICKED(IDC_PRINTER2,OnPrinter2)
END_MESSAGE_MAP()
// PrintChoose message handlers
void CPrintChooseDlg::OnPrinter1()
{
choosing=0;
CDialog::OnPrinter1();
}
void CPrintChooseDlg::OnPrinter2()
{
choosing=1;
CDialog::OnPrinter2();
}
and in the main file where i am running from, i define the following headers:
#include "stdafx.h"
#include "Tungsten.h"
#include "TungstenDlg.h"
using namespace std;
#include<sstream>
#include <string>
The problem is i am always getting the following errors: error C2039: 'OnPrinter1': is not a member of 'CDialog'
error C2039: 'OnPrinter2': is not a member of 'CDialog'
What i tried is to add #include <string> at my header and make sure that the headers are not repeated, and defining Printer1 and Pronter 2 in the main file where i am running from, but i still get the same error. I appreciate your help. Thanks in Advance
To get the address of a member function, you need to use &CPrintChooseDlg::OnPrinter1.
Really old VC++, like VC6, didn't care about the correct syntax and generated incorrect message maps.
I want to use #pragma pointers_to_members() to just one class.
I have other class definitions in my header file but I want to apply the required pragma to only one class and other classes should not be affected. How can this be achieved? There are not many code examples for that.
I have resolved this issue. I used the # pragma directive just before the BEGIN_MESSAGE_MAP() in my .cpp file.
#pragma pointers_to_members(full_generality,virtual_inheritance)
BEGIN_MESSAGE_MAP(CMy4407Dlg, CDialogEx)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, &CMy4407Dlg::OnBnClickedButton1)
END_MESSAGE_MAP()
This is my derived class CMy4407Dlg:
class CMy4407Dlg : public CDialogEx, public virtual ABCD
{
// Construction
public:
CMy4407Dlg(CWnd* pParent = NULL); // standard constructor
// Dialog Data
enum { IDD = IDD_MY4407_DIALOG };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
// Implementation
protected:
HICON m_hIcon;
// Generated message map functions
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
DECLARE_MESSAGE_MAP()
public:
afx_msg void OnBnClickedButton1();
};
I am trying to create CWnd derived class at runtime but CWnd::Create fails. I have no idea why. Here is minimal code that show the problem:
MFCTestApplicationDlg.h
#pragma once
class c_CustomButton : public CButton
{
protected:
DECLARE_MESSAGE_MAP()
virtual void PreSubclassWindow();
public:
c_CustomButton();
virtual ~c_CustomButton();
};
class TestWindow : public CWnd
{
public:
TestWindow();
virtual ~TestWindow();
protected:
DECLARE_MESSAGE_MAP()
DECLARE_DYNCREATE(TestWindow)
};
// CMFCTestApplicationDlg dialog
class CMFCTestApplicationDlg : public CDialogEx
{
...
}
MFCTestApplicationDlg.cpp
//
#include "stdafx.h"
#include "MFCTestApplication.h"
#include "MFCTestApplicationDlg.h"
#include "afxdialogex.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
/*==========================================================================*/
c_CustomButton::c_CustomButton()
{
}
/*==========================================================================*/
c_CustomButton::~c_CustomButton()
{
}
BEGIN_MESSAGE_MAP(c_CustomButton, CButton)
END_MESSAGE_MAP()
/*==========================================================================*/
void c_CustomButton::PreSubclassWindow()
{
CButton::PreSubclassWindow();
ModifyStyle(0, BS_OWNERDRAW);
}
IMPLEMENT_DYNAMIC(TestWindow, CWnd)
TestWindow::TestWindow()
{
}
TestWindow::~TestWindow()
{
}
BEGIN_MESSAGE_MAP(TestWindow, CWnd)
END_MESSAGE_MAP()
void CMFCTestApplicationDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
c_CustomButton* pColoredButton = new c_CustomButton;
pColoredButton->Create((LPCTSTR)"", 0, CRect(), this, 0);// successeded
pColoredButton->SetWindowTextW((LPCTSTR)"test");
TestWindow* pTestWindow = new TestWindow;
pTestWindow->Create((LPCTSTR)"TestWindow", (LPCTSTR)"TestWindowName", 0, CRect(), this, 0);// failed
pTestWindow->SetWindowText((LPCTSTR)"test");
}
In void CMFCTestApplicationDlg::DoDataExchange(CDataExchange* pDX) I tried to create a CButton derived class object and CWnd derived class object. The first one created successfully but CWnd derived class object fails to create. Whats wrong with this code?
I need to place a CView derived class into a CDockablePane. Is there any code example somewhere, or can someone provide such code?
What I tried:
Apparently should be simple, online I found advice like "just create the view and set its parent to be the dialog or the dockable pane or what kind of window you want". But for some reason it doesn't work, maybe is because it needs a CFrameWnd, I don't know.
Anyway, I need to be able to do this without creating another document template class. Just to work with preexisting document and view classes.
Here's an example:
a class derived from CDockablePane:
//CRichEditPane .h
class CRichEditPane : public CDockablePane
{
DECLARE_DYNAMIC(CRichEditPane)
public:
CRichEditPane();
virtual ~CRichEditPane();
protected:
void AdjustLayout();
protected:
DECLARE_MESSAGE_MAP()
public:
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
afx_msg void OnSize(UINT nType, int cx, int cy);
};
//CRichEditPane .cpp
IMPLEMENT_DYNAMIC(CRichEditPane, CDockablePane)
CRichEditPane::CRichEditPane()
{
}
CRichEditPane::~CRichEditPane()
{
}
BEGIN_MESSAGE_MAP(CRichEditPane, CDockablePane)
ON_WM_CREATE()
ON_WM_SIZE()
END_MESSAGE_MAP()
// CRichEditPane message handlers
int CRichEditPane::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CDockablePane::OnCreate(lpCreateStruct) == -1)
return -1;
CRuntimeClass *pClass = RUNTIME_CLASS(CRichEditViewInPane);
// calling constructor using IMPLEMENT_DYNCREATE macro
CRichEditViewInPane *pView = (CRichEditViewInPane*)pClass->CreateObject();
if (!pView->Create(NULL, NULL, AFX_WS_DEFAULT_VIEW, CRect(0,0,0,0), this, AFX_IDW_PANE_FIRST, NULL))
{
return -1;
}
CRichEditCtrl ctrl;
ctrl.Create(WS_CHILD, CRect(0, 0, 0, 0), this, 10991);
return 0;
}
void CRichEditPane::OnSize(UINT nType, int cx, int cy)
{
CDockablePane::OnSize(nType, cx, cy);
AdjustLayout();
}
a view class derived from CView:
//CRichEditViewInPane .h
class CRichEditViewInPane : public CRichEditView
{
DECLARE_DYNCREATE(CRichEditViewInPane)
protected:
CRichEditViewInPane(); // protected constructor used by dynamic creation
virtual ~CRichEditViewInPane();
public:
#ifdef _DEBUG
virtual void AssertValid() const;
#ifndef _WIN32_WCE
virtual void Dump(CDumpContext& dc) const;
#endif
#endif
protected:
DECLARE_MESSAGE_MAP()
};
//CRichEditViewInPane. cpp
IMPLEMENT_DYNCREATE(CRichEditViewInPane, CRichEditView)
CRichEditViewInPane::CRichEditViewInPane()
{
}
CRichEditViewInPane::~CRichEditViewInPane()
{
}
BEGIN_MESSAGE_MAP(CRichEditViewInPane, CRichEditView)
END_MESSAGE_MAP()
I have a CStdDlg thats inherits from a CDialog class. In the CStdDlg dialog, I have a list box (m_lcList1), edit box (m_ceEdit1), a radio button(m_rbButton2) and buttons OK, Cancel and Button1.
I am trying to create another class named CDerivedDlg that inherits from CStdDlg. I want to use everything in CStdDlg but from the CDerivedDlg.
This is a silly test application here, but I need something exactly like this is a real application.
I'll show all the code below. PROBLEM: The problem keeps bombing whenever I try to add a column header to the list box. m_hWnd = NULL
Can anyone tell me what's wrong? I would truly appreciate it. Thank you.
// CStdDlg.cpp file
#include "stdafx.h"
#include "testdlg.h"
#include "StdDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CStdDlg dialog
CStdDlg::CStdDlg(UINT nIDTemplate, CWnd* pParent /*=NULL*/)
: CDialog(CStdDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CStdDlg)
//}}AFX_DATA_INIT
}
void CStdDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CStdDlg)
DDX_Control(pDX, IDC_EDIT1, m_ceEdit1);
DDX_Control(pDX, IDC_RADIO2, m_rbButton2);
DDX_Control(pDX, IDC_LIST1, m_lcList1);
//}}AFX_DATA_MAP
}
IMPLEMENT_DYNAMIC(CStdDlg, CDialog)
BEGIN_MESSAGE_MAP(CStdDlg, CDialog)
//{{AFX_MSG_MAP(CStdDlg)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CStdDlg message handlers
BOOL CStdDlg::OnInitDialog()
{
CDialog::OnInitDialog();
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
===================================================================================
//CStdDlg.h file
#if !defined(AFX_STDDLG_H__CDAFF61F_91AB_4B47_9970_6B8BB5DE0003__INCLUDED_)
#define AFX_STDDLG_H__CDAFF61F_91AB_4B47_9970_6B8BB5DE0003__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// StdDlg.h : header file
//
#include <afxwin.h>
/////////////////////////////////////////////////////////////////////////////
// CStdDlg dialog
class CStdDlg : public CDialog
{
DECLARE_DYNAMIC(CStdDlg)
// Construction
public:
CStdDlg(UINT nIDTemplate,CWnd* pParent = NULL); // standard constructor
CString GetTitle() {
CString csTTL;
GetWindowText(csTTL);
return csTTL;
}
// Dialog Data
//{{AFX_DATA(CStdDlg)
enum { IDD = IDD_STDDLG };
CEdit m_ceEdit1;
CButton m_rbButton2;
CListCtrl m_lcList1;
//}}AFX_DATA
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CStdDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
void ShowMsg() { AfxMessageBox("ShowMsg from StdDlg"); }
// Generated message map functions
//{{AFX_MSG(CStdDlg)
virtual BOOL OnInitDialog();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_STDDLG_H__CDAFF61F_91AB_4B47_9970_6B8BB5DE0003__INCLUDED_)
===================================================================================
//CDerivedDlg.cpp file
// StdDlg.cpp : implementation file
//
#include "stdafx.h"
#include "testdlg.h"
#include "CDerivedDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CDerivedDlg dialog
CDerivedDlg::CDerivedDlg(CWnd* pParent /*=NULL*/)
: CStdDlg(CDerivedDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CDerivedDlg)
//}}AFX_DATA_INIT
}
void CDerivedDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CDerivedDlg)
//}}AFX_DATA_MAP
}
IMPLEMENT_DYNAMIC(CDerivedDlg, CStdDlg)
BEGIN_MESSAGE_MAP(CDerivedDlg, CStdDlg)
//{{AFX_MSG_MAP(CDerivedDlg)
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CDerivedDlg message handlers
//void CDerivedDlg::OnOK()
//{
// // TODO: Add extra validation here
// AfxMessageBox("CDerived Class OK button pressed");
// CDialog::OnOK();
//}
BOOL CDerivedDlg::OnInitDialog()
{
CStdDlg::OnInitDialog();
SetWindowText("Derived Test Window");
m_lcList1.InsertColumn(0,"This is a test"); *******THIS IS WHERE IT CRASHES*****
m_rbButton2.ShowWindow(SW_HIDE);
m_ceEdit1.ShowWindow(SW_HIDE);
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void CDerivedDlg::OnButton1()
{
// // TODO: Add extra validation here
AfxMessageBox("CDerived Button1 pressed");
AfxMessageBox(GetTitle());
}
void CDerivedDlg::OnOK()
{
AfxMessageBox("CDerived Class OK button pressed");
}
===================================================================================
// CDerivedDlg.h file
#if !defined(AFX_CDerivedDlg_H__CDAFF61F_91AB_4B47_9970_6B8BB5DE0003__INCLUDED_)
#define AFX_CDerivedDlg_H__CDAFF61F_91AB_4B47_9970_6B8BB5DE0003__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// CDerivedDlg.h : header file
//
#include "StdDlg.h"
/////////////////////////////////////////////////////////////////////////////
// CDerivedDlg dialog
class CDerivedDlg : public CStdDlg
{
DECLARE_DYNAMIC(CDerivedDlg)
// Construction
public:
CDerivedDlg(CWnd* pParent = NULL); // standard constructor
virtual void SetTTL(CString csTitle) { this->SetWindowText(csTitle); }
// Dialog Data
//{{AFX_DATA(CDerivedDlg)
enum { IDD = IDD_STDDLG };
//}}AFX_DATA
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CDerivedDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
// Generated message map functions
//{{AFX_MSG(CDerivedDlg)
virtual BOOL OnInitDialog();
afx_msg void OnButton1();
virtual void OnOK();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_CDerivedDlg_H__CDAFF61F_91AB_4B47_9970_6B8BB5DE0003__INCLUDED_)
===================================================================================
I'm guessing that since the DDX_Control() are on the base class, the derived class will not link the resource controls to their respective classes. You might want to try to change this:
void CDerivedDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
into this:
void CDerivedDlg::DoDataExchange(CDataExchange* pDX)
{
CStdDlg::DoDataExchange(pDX);
m_lcList1.InsertColumn(0,"This is a test"); *******THIS IS WHERE IT CRASHES*****
You have to create the control before you'll be able to use it.
See CListCtrl::Create and/or CListCtrl::CreateEx methods.
You must choose the LVS_REPORT style since you want to use the list as a report view control.