如何自定义常见打印对话框的 MFC 应用程序中

 我来答
xiangjuan314
2016-04-01 · TA获得超过3.3万个赞
知道大有可为答主
回答量:2.9万
采纳率:0%
帮助的人:3114万
展开全部
要向应用程序.rc 文件,请从 Commdlg.rc 复制 PRINTDLGORD 对话框框模板 (ID 是 1538),请按这些步骤操作。

请注意 在 Visual C++ 4 x 和 5.0 中, 此对话框框模板位于文件 Include\Prnsetup.dlg。 在 Visual C++6.0 中此文件位于 MSDN 光盘上 \Samples\Vc98\Mfc\General\Clipart。 在 Microsoft Visual C++.NET 中此文件位于 MSDN 光盘上的 \Samples\Vc\Mfc\General\Clipart 文件夹。
打开 Commdlg.rc。
启用剪切和粘贴的项目资源文件中的资源:
如果您使用 Visual C++.NET 请在资源编辑器模式 (而不是默认资源视图模式中) 中打开项目.rc 文件。 为此,关闭资源视图窗口项目的。 解决方案资源管理器窗口中找到您项目.rc 文件。 右键单击解决方案资源管理器窗口中的.rc 文件,然后单击打开方式。 在打开方式对话框中,单击以选中该程序用来打开该.rc 文件的资源编辑器,然后单击打开。
如果您使用的 Visual C++ 4 x 或 5.0,添加行 # include"Windows.h"Include\prnsetup.dlg 文件的顶部。 保存并关闭该文件。 重新打开它作为"资源"框文件打开对话框中使用打开。
在 Commdlg.rc 文件的资源编辑器窗口,单击 PRINTDLGORD 对话框资源、 单击 <>,然后单击 <>。 PRINTDLGORD ID 是 1538。 头文件 Dlgs.h 中定义此 PRINTDLGORD 符号。
您项目的.rc 文件的资源编辑器窗口中, 右键单击对话框,然后单击粘贴。 现在,保存并关闭项目.rc 文件,在资源编辑器窗口中,然后重然后重新打开项目的默认资源视图窗口。 如果要使用 Visual C++6.0,有复制资源的任何上下文菜单。 在文件菜单上单击打开以打开该 Commdlg.rc 文件,然后打开您的项目的资源视图菜单。 选择在 Commdlg.rc 的 1538 的资源 ID、 按下该 Ctrl 键,然后将 Commdlg.rc 窗口中的对话框框资源拖放到资源视图窗口。

在 <>菜单上单击 <>。
<>菜单与上下文相关。 资源视图处于活动状态时,不会出现在 <>菜单。 切换到解决方案资源管理器,然后在项目菜单上单击 <>。
展开 MFC 类别 MFC 类模板,单击然后单击 <>。
使用 MFC 类向导添加用于此对话框框模板如 CMyPrintDialog 的 Visual C++ 类。 从 CPrintDialog 派生此新的类。
如果您使用的 Visual C++6.0,请在 <>菜单上单击<>。

<>菜单与上下文相关。 资源视图处于活动状态时,不会出现在 <>菜单。 切换到 ClassView,然后在<>菜单上单击 <>。 为类键入名称,设置为 <>的<>,然后单击 <>。
从 CDialog 的所有引用都更改为新创建的类的页眉和实现文件中的 CPrintDialog 中。 请注意此步骤不需要如果您已直接从 CPrintDialog 派生您的类。
因为 CPrintDialog 的构造函数不同于 CDialog,修改 CMyPrintDialog 的构造函数使用以下代码: 注意: 此步骤如果不是必需您已直接从 CPrintDialog 中派生您的类.
// Header file of CMyPrintDialog.
class CMyPrintDialog : public CPrintDialog
{
// Construction.
public:

// The arguments to the following constructor closely match
// CPrintDialog. Note the difference in the second argument.
CMyPrintDialog(BOOL bPrintSetupOnly,
// TRUE for Print Setup, FALSE for Print Dialog.
DWORD dwFlags = PD_ALLPAGES | PD_USEDEVMODECOPIES |
PD_HIDEPRINTTOFILE,
// Combination of flags. Refer to the Windows SDK
// documentation for PRINTDLG structure for a
// description of all the flags that can be used.
CWnd* pParentWnd = NULL);

// Rest of the class declaration.
...

DECLARE_MESSAGE_MAP()
};

// Implementation file of CMyPrintDialog.
CMyPrintDialog::CMyPrintDialog(BOOL bPrintSetupOnly,
DWORD dwFlags /* = PD_ALLPAGES | PD_USEDEVMODECOPIES |
PD_HIDEPRINTTOFILE */,
CWnd* pParentWnd /* = NULL */)
: CPrintDialog(bPrintSetupOnly, dwFlags, pParentWnd)
{
//{{AFX_DA
TA_INIT(CMyPrintDialog)
// NOTE: the ClassWizard will add member initialization here.
//}}AFX_DA
TA_INIT
}

进行任何所需更改为复制的对话框框模板和关联的源代码中。 注意: 不要删除任何存在原始对话框框模板中的控件。 删除该控件,可导致问题的 CPrintDialog DoDataExchange 在功能。 相反,禁用不需要的控件,或在 CPrintDialog 派生类的重写 On
InitDialog 成员函数中隐藏它们。
修改 CView 派生类 (是例如 CMyView) 通过使用下面的代码中使用自定义的打印对话框:
// Implementation file of the view (for example, in myview.cpp).
...
#include "MyPrintDialog.h" // Include the CMyPrintDialog header file.
...

// Override On
PreparePrinting of the CView-derived class as below:
BOOL CMyView::On
PreparePrinting(CPrintInfo* pInfo)
{
// Delete the CPrintDialog object created in the CPrintInfo
// constructor, and substitute with customized print dialog.
delete pInfo->m_pPD;

// Construct and substitute with customized print dialog.
pInfo->m_pPD = new CMyPrintDialog(FALSE);

// Set the page range.
pInfo->m_pPD->m_pd.nMinPage = 1; // on
e based page numbers.
pInfo->m_pPD->m_pd.nMaxPage = 0xffff; // how many pages is unknown.

// Change the PRINTDLG struct so that the custom print dialog box will
// be used.
pInfo->m_pPD->m_pd.hInstance = AfxGetInstanceHandle();
pInfo->m_pPD->m_pd.lpPrintTemplateName =
MAKEINTRESOURCE(1538);

// Set the Flags of the PRINTDLG structure as shown, else the
// changes will have no effect.
pInfo->m_pPD->m_pd.Flags |= PD_ENABLEPRINTTEMPLATE;

// For details about these flags, refer to the SDK documentation
// on the PRINTDLG structure.

return DoPreparePrinting(pInfo);
}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式