关闭子对话框

zdblzwj 2008-04-17 10:09:13

PrintFrame *pFrame = new PrintFrame;
pFrame->m_pCallerDlg = this;

pFrame->Create(NULL,"Curve Print",WS_OVERLAPPEDWINDOW,CRect(0,0,0,0));

这里PringFrame类用于创建一个窗口,m_pCallerDlg 是用CDialog *m_pCallerDlg初始化的,
pFrame->m_pCallerDlg = this;这句的作用好像是把当前创建的窗口也用父窗口初始吧?
问题:怎么样才能在关闭pFrame的情况下不要关闭父窗口,我在关闭pFrame的时候,总是会把父窗口一起给关了。如何在目前初始的情况下关闭这个子窗口?
我新学VC,名词各们前辈给予帮助!分不多,对不住各位!
...全文
317 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
zdblzwj 2008-04-25
  • 打赏
  • 举报
回复
未解决,但不想在这里拖着,先把贴子结了
zdblzwj 2008-04-22
  • 打赏
  • 举报
回复
多谢楼上两位,我去试下
zlcs8921 2008-04-21
  • 打赏
  • 举报
回复
别响应 OnOK(), 因为子窗口的OnOK()消息会让父窗口也相应这个消息,你可以把 父窗口的OnOK() 改写。
chenyu2202863 2008-04-21
  • 打赏
  • 举报
回复
在 OnNcDestroy里delete this
zdblzwj 2008-04-21
  • 打赏
  • 举报
回复
沉了!


wltg2001 2008-04-18
  • 打赏
  • 举报
回复
将构造函数中的这一句m_pWnd= AfxGetApp()->m_pMainWnd去掉试试
zdblzwj 2008-04-18
  • 打赏
  • 举报
回复
还是不行。。。郁闷。。。郁闷。。。郁闷呐。!!
我再看看
wltg2001 2008-04-18
  • 打赏
  • 举报
回复
用DestroyWindow的主要作用是产生WM_QUIT消息,主循环得到这个消息会退出,但是这个消息不应该会发给这主循环才对啊,我觉得问题应该是这两句:
m_pWnd = AfxGetApp()->m_pMainWnd;
AfxGetApp()->m_pMainWnd = this;

你将它们改为:
m_pWnd=null;
AfxGetApp()->m_pMainWnd = this;这一句去掉试试.
milaoshu2 2008-04-18
  • 打赏
  • 举报
回复
pFrame->m_pCallerDlg = this;有问题,你去掉试试
zdblzwj 2008-04-18
  • 打赏
  • 举报
回复
没有用。。还是一样的。。。英雄帮帮我!!!!!
我都不清楚这个逻辑是怎么一回事,看了N遍了

那个原作者他也是这样写的,但是他的不会被关闭。就连那个子对话框都看不到
scq2099yt 2008-04-17
  • 打赏
  • 举报
回复
up
zdblzwj 2008-04-17
  • 打赏
  • 举报
回复
我使用pFrame->DestroyWindow()的时候它是一起把主窗口给关了,我不知道什么原因
zdblzwj 2008-04-17
  • 打赏
  • 举报
回复
谢谢红猪指点,那关闭这个子窗口该怎么关呢?
用pFrame->DestroyWindow()吗?
wltg2001 2008-04-17
  • 打赏
  • 举报
回复
pFrame->m_pCallerDlg = this;
====================
m_pCallerDlg应该是自己定义的变量,可能是用来记录父窗口的,这句也就是指定对话框的父窗口.应该不会出现你所说的情况吧.
zdblzwj 2008-04-17
  • 打赏
  • 举报
回复
路过的高人指点一下!谢谢
zdblzwj 2008-04-17
  • 打赏
  • 举报
回复
// TestProDlg.cpp : implementation file
//

#include "stdafx.h"
#include "TestPro.h"
#include "TestProDlg.h"
#include "math.h"
#include "TestDialog.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
CAboutDlg();




// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA

// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL

// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTestProDlg dialog

CTestProDlg::CTestProDlg(CWnd* pParent /*=NULL*/)
: CDialog(CTestProDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CTestProDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CTestProDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CTestProDlg)
DDX_Control(pDX, IDC_OCX1, m_wmpplayer);
//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CTestProDlg, CDialog)
//{{AFX_MSG_MAP(CTestProDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(ID_PRINT, OnPrint)
//}}AFX_MSG_MAP
ON_BN_CLICKED(ID_PRINT, OnPrint)
ON_MESSAGE(WM_MY_PRINT,OnMyPrint)
ON_MESSAGE(WM_BEGIN_PRINTING,OnBeginPrinting)
ON_MESSAGE(WM_END_PRINTING,OnEndPrinting)

END_MESSAGE_MAP()


/////////////////////////////////////////////////////////////////////////////
// CTestProDlg message handlers

BOOL CTestProDlg::OnInitDialog()
{
CDialog::OnInitDialog();

// Add "About..." menu item to system menu.

// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);

CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}

// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon

// TODO: Add extra initialization here
CString strMediaPath = "F:\\vc\\vc++\\Insurance_test\\Debug\\ad.wmv";
CWMPMedia media = m_wmpplayer.newMedia(strMediaPath);
// m_wmpplayer.SetUrl("E:\\lonely.wmv");
m_wmpplayer.SetCurrentMedia(media);
m_wmpcontrols = m_wmpplayer.GetControls();
m_wmpsetings = m_wmpplayer.GetSettings();

m_wmpsetings.SetPlayCount(10);
m_wmpcontrols.play();

return TRUE; // return TRUE unless you set the focus to a control
}

void CTestProDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}

// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.

void CTestProDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting

SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;

// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}

// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CTestProDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}

void CTestProDlg::OnPrint()
{
// TODO: Add your control notification handler code here
PrintFrame *pFrame = new PrintFrame;
pFrame->m_pCallerDlg = this;

pFrame->Create(NULL,"Curve Print",WS_OVERLAPPEDWINDOW,CRect(0,0,0,0));//就是这个窗口,如何在关闭他的时候不要把父窗口也给关闭了
pFrame->m_pView->OnMyPrint();

}
LRESULT CTestProDlg::OnMyPrint(WPARAM wParam,LPARAM lParam)
{


UINT gl_uNumOfPoints = 5;//只打印五行
CDC* pDC = (CDC*)wParam;
CPrintInfo* pInfo = (CPrintInfo *)lParam;


int nPageNumber = pInfo->m_nCurPage;
int i,j;
CFont *pOldFont;
CFont DataFont;
DataFont.CreatePointFont(120,"宋体",pDC);
if(nPageNumber==1)
{

TCHAR *pszTitle[7]={"Time","数值1","数值2","数值3","数值4","数值5","数值6"};
pOldFont=pDC->SelectObject(&DataFont);
//Rectangle用于画矩形
pDC->Rectangle(m_cxOffset,(int)((2.5 + PRINTMARGIN)*m_cyPrinter),m_cxOffset+m_cxWidth,(m_nLinesPerPage+3 + PRINTMARGIN)*m_cyPrinter+20);
for(i=1;i<5;i++)//加竖线
{
// pDC->MoveTo(m_cxOffset+i*m_cxWidth/4,(int)((2.5 + PRINTMARGIN)*m_cyPrinter));
// pDC->LineTo(m_cxOffset+i*m_cxWidth/4,(3 +m_nLinesPerPage+PRINTMARGIN)*m_cyPrinter+20);
}
pDC->MoveTo(m_cxOffset,(4 + PRINTMARGIN)*m_cyPrinter);
pDC->LineTo(m_cxOffset+m_cxWidth,(4 + PRINTMARGIN)*m_cyPrinter);
for(i=0;i<4;i++)//确认要显示的字段个数:pszTitle
{
//控制间距,与下相等
pDC->TextOut(m_cxOffset+80+i*m_cxWidth/5,(int)((2.8 + PRINTMARGIN)*m_cyPrinter),CString(pszTitle[i]));
}
for(i=0;i<(int)m_nLinesPerPage-1;i++)
{
CString str[7];
int nIndex = (nPageNumber-1)*(m_nLinesPerPage-1)+i;
if(nIndex<(int)gl_uNumOfPoints)
{

str[0].Format("%.3f",(double)(i+1));
str[1].Format("%.3f",1+sin(i));
str[2].Format("%.3f",1+cos(i));
str[3].Format("%.3f",1+sin(0.5*i));
for(j=0;j<7;j++)
{
//控制间距,与上相等
pDC->TextOut(m_cxOffset+80+j*m_cxWidth/5,(4 + PRINTMARGIN+i)*m_cyPrinter+5,str[j]);
}
}
}
pDC->SelectObject (pOldFont);

}

return TRUE;
}
LRESULT CTestProDlg::OnBeginPrinting(WPARAM wParam,LPARAM lParam)
{

CDC* pDC = (CDC*)wParam;
CPrintInfo* pInfo = (CPrintInfo *)lParam;
if(m_fontPrinter.m_hObject==NULL)
m_fontPrinter.CreatePointFont(120,"FixedSys",pDC);
TEXTMETRIC tm;
CFont* pOldFont = pDC->SelectObject (&m_fontPrinter);
pDC->GetTextMetrics (&tm);
m_cyPrinter = tm.tmHeight + tm.tmExternalLeading;

CSize size = pDC->GetTextExtent (_T ("1----2-----3-----4"), 35);
pDC->SelectObject (pOldFont);
m_cxWidth = size.cx;
pInfo->SetMaxPage (1);//设置最大页码数

m_cxOffset = (pDC->GetDeviceCaps (HORZRES) - size.cx) / 1;
return TRUE;
}

LRESULT CTestProDlg::OnEndPrinting(WPARAM wParam,LPARAM lParam)
{

if(m_fontPrinter.m_hObject!=NULL)
m_fontPrinter.DeleteObject ();

return FALSE;
}

这个程序不是我写的,是我在网上找的,我才学VC,很多都不懂,请指点,谢谢!
我没有把头文件贴出来,应该没关系吧?
zdblzwj 2008-04-17
  • 打赏
  • 举报
回复
谢谢你的关注

// PrintFrame.cpp : implementation file
//

#include "stdafx.h"
#include "TestPro.h"
#include "PrintFrame.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// PrintFrame

IMPLEMENT_DYNCREATE(PrintFrame, CFrameWnd)

PrintFrame::PrintFrame()
{
m_pCallerDlg = NULL;
m_pWnd = AfxGetApp()->m_pMainWnd;
AfxGetApp()->m_pMainWnd = this;
}

PrintFrame::~PrintFrame()
{
}


BEGIN_MESSAGE_MAP(PrintFrame, CFrameWnd)
//{{AFX_MSG_MAP(PrintFrame)
// NOTE - the ClassWizard will add and remove mapping macros here.
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// PrintFrame message handlers
BOOL PrintFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext)
{
// TODO: Add your specialized code here and/or call the base class
m_pView = new CPrintView();
m_pView->Create(NULL,NULL,WS_CHILD|WS_VISIBLE,CRect(0,0,0,0),this,AFX_IDW_PANE_FIRST,pContext);

return TRUE;
}

void PrintFrame::OnDestroy()
{
if(m_pView!=NULL)
{
m_pView->DestroyWindow();
}
CFrameWnd::OnDestroy();
AfxGetApp()->m_pMainWnd = m_pWnd;
}



// PrintView.cpp : implementation file
//

#include "stdafx.h"
#include "PrintView.h"
#include "PrintFrame.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CPrintView

IMPLEMENT_DYNCREATE(CPrintView, CView)

CPrintView::CPrintView()
{
m_bPrint = TRUE;
}

CPrintView::~CPrintView()
{
}


BEGIN_MESSAGE_MAP(CPrintView, CView)
//{{AFX_MSG_MAP(CPrintView)
// NOTE - the ClassWizard will add and remove mapping macros here.
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPrintView drawing

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

/////////////////////////////////////////////////////////////////////////////
// CPrintView diagnostics

#ifdef _DEBUG
void CPrintView::AssertValid() const
{
CView::AssertValid();
}

void CPrintView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CPrintView message handlers
void CPrintView::OnMyPrint()
{
GetParent()->ShowWindow(SW_SHOWMINIMIZED);
m_bPrint = TRUE;
CView::OnFilePrint();

}
void CPrintView::OnMyPrintPreview()
{
GetParent()->ShowWindow(SW_SHOWMAXIMIZED);
m_bPrint=FALSE;
CView::OnFilePrintPreview();
}
BOOL CPrintView::OnPreparePrinting(CPrintInfo* pInfo)
{
pInfo->m_bDirect = TRUE;//不弹出打印对话框
if(DoPreparePrinting(pInfo))
return TRUE;
else
{
GetParent()->DestroyWindow();
return FALSE;
}
}
void CPrintView::OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo)
{
CView::OnBeginPrinting(pDC, pInfo);
PrintFrame *pFrame =(PrintFrame *)GetParent();
pFrame->m_pCallerDlg->SendMessage(WM_BEGIN_PRINTING,(WPARAM) pDC, (LPARAM) pInfo);
}
void CPrintView::OnEndPrinting(CDC* pDC, CPrintInfo* pInfo)
{
CView::OnEndPrinting(pDC, pInfo);
PrintFrame *pFrame =(PrintFrame *)GetParent();
pInfo->m_bDirect = TRUE;
pFrame->m_pCallerDlg->SendMessage(WM_END_PRINTING,(WPARAM) pDC, (LPARAM) pInfo);
if(m_bPrint) //直接打印,不是预览
//GetParent()->DestroyWindow();
pFrame->DestroyWindow();
pFrame->GetParent()->ShowWindow(SW_SHOW);

}

void CPrintView::OnPrint(CDC* pDC, CPrintInfo* pInfo)
{
PrintFrame *pFrame =(PrintFrame *)GetParent();
pFrame->m_pCallerDlg->SendMessage(WM_MY_PRINT,(WPARAM) pDC, (LPARAM) pInfo) ;
}
void CPrintView::OnEndPrintPreview(CDC* pDC, CPrintInfo* pInfo, POINT point, CPreviewView* pView)
{
// TODO: Add your specialized code here and/or call the base class
CView::OnEndPrintPreview(pDC, pInfo, point, pView);
GetParent()->DestroyWindow();
}

wltg2001 2008-04-17
  • 打赏
  • 举报
回复
上面说错了,EndDialog是关闭模态对话框的,非模态对话框确实应该用DestoryWindow来关闭,你将代码发一下看看吧.
wltg2001 2008-04-17
  • 打赏
  • 举报
回复
用pFrame->EndDialog试试
zdblzwj 2008-04-17
  • 打赏
  • 举报
回复
快沉了!
MFC的视图打印/预览功能基于CView::OnFilePrint和CView::OnFilePrintPreview这两个函数 如果要对用户自定义对话框(模态)内的视图(以下简称A视图)进行打印,直接调用A视图的CView::OnFilePrint即可,弹出的“选择打印机”对话框同样也是模态对话框,覆盖在用户对话框上。 如果要对用户对话框内的A视图进行预览,直接调用A视图的CView::OnFilePrintPreview后,预览视图(MFC CPreviewView类实例)内虽然预览正常,但是挡住了用户对话框。由于用户对话框是模态的,导致预览视图上的“打印...”、“上一页”、“下一页”、“放大”、“缩小”、“单页/双页”和“关闭”等按钮均无法响应鼠标点击,处于“假死”状态。 解决方法: 在调用A视图的CView::OnFilePrintPreview之前,先关闭用户对话框(CDialog::OnCancel),由于这时已不存在用于预览的A视图,再重新创建一个相同的视图(以下简称B视图),以B视图来启动OnFilePrintPreview进行预览。 MFC视图预览的原理是:以主框架窗口(CMainFrame)作为预览框架,隐藏原先的视图(用CMainFrame::GetActiveView得到的那个,以下简称C视图),用CPreviewView类替换,同时创建包含“打印...”、“放大”、“关闭”等按钮的工具栏(CDialogBar类),最后将CPreviewView::m_pPreviewDC作为C视图OnDraw的CDC*参数传入,这样,虽然调用的是C视图的OnDraw函数,实际上却显示在了CPreviewView视图中,起到了“预览”的效果。在点击预览框架工具栏上的“打印...”按钮后, MFC体系将向原先的主框架(本例中和预览框架是同一个,都是CMainFrame)发送ID_FILE_PRINT的WM_COMMAND消息。在缺省的配置中,这将启动C视图的OnFilePrint,从而在打印机上打出C视图的内容。 在本例中,为了打印B视图,需要在CMainFrame中处理WM_COMMAND/ID_FILE_PRINT,加入额外的条件,来判断对按钮“打印...”的点击是来自B还是C视图的预览框架。在本例中用了一个变量CMainFrame::m_nPrintFrom。根据其取值(0或者1),分别启动B或者C视图的OnFilePrint。 使用方法: 程序示例在VC++2010上通过。主菜单项“打印预览测试”下,有两个菜单项,“对话框用户视图的打印预览”用于B视图(画出一个圆);“单文档视图的打印预览”用于C视图(画出一个正方形),等同于“文件”主菜单项下的“打印预览(V)”。

15,979

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 界面
社区管理员
  • 界面
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧