MFC - 打印 - 从自定义页面设置对话框更改页面方向

发布于 2024-10-15 16:52:49 字数 2201 浏览 2 评论 0原文

我正在使用 MFC 和 VS2008 为我的 Win32 程序开发自定义打印对话框和页面设置。由于代码是遗留的,我无法从 MFC 视图/文档架构中充分利用。结果我完全从头开始写了一段打印代码。

我设置了 CPrintInfo,实例化我的自定义打印对话框并将该对话框挂接到我刚刚创建的 CPrintInfo。当我的自定义打印对话框打开时,我有一个单选按钮可以让用户切换页面方向。由于某些原因,我无法在运行时修改当前的 DEVMODE。结果,我打印的每一页最终都会成为一幅肖像。

即使我从自定义打印对话框的事件处理程序中手动将 pDevMode->dmOrientation 设置为 DMORIENT_LANDSCAPE,打印结果仍然最终为纵向。我真的不确定为什么会发生这种情况以及如何在打印对话框打开后修改 DevMode。

预先感谢您的帮助。

这是我的代码:

void PrintSomething(CWnd* currentWnd) {
  // Create CPrintInfo
  CPrintInfo* pPrintInfo = new CPrintInfo;
  SetupPrintInfo(pPrintInfo); // simply setup some member variables of CPrintInfo

  // Create a custom print dialog 
  CustomPrintDlg* pCustomPrtDlg = new CustomPrintDlg(FALSE, PD_ALLPAGES | PD_USEDEVMODECOPIES | PD_NOPAGENUMS
           | PD_HIDEPRINTTOFILE | PD_NOSELECTION, pPrintInfo, currentWnd);
  SetupPrintDialog(pPrintInfo,pCustomPrtDlg);

  if ( AfxGetApp()->DoPrintDialog(pCustomPrtDlg) == IDOK ) {
        ... // proceed a print loop 
  }
}

设置自定义打印对话框的代码:

void SetupPrintDialog(CPrintInfo* pPrintInfo,CustomPrintDlg* pCustomPrtDlg) {
  delete pInfo->m_pPD;
  pInfo->m_pPD = pCustomPrtDlg;

  pInfo->m_pPD->m_pd.hInstance = AfxGetInstanceHandle();
  pInfo->m_pPD->m_pd.lpPrintTemplateName = MAKEINTRESOURCE(IDD_CUSTOM_PRTDLG);

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

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

当用户将单选按钮切换为横向时,将调用此函数:< /strong>

void CustomPrintDlg::OnLandscapeChecked() {
  // set the current Devmode to landscape
  LPDEVMODE pDevMode = GetDevMode();
  GlobalUnlock(pDevMode);
  pDevMode->dmOrientation = DMORIENT_LANDSCAPE;
}

自定义打印对话框类的伪代码:

class CustomPrintDlg: public CPrintDialog {

  ...   // just override some methods from CPrintDialog
};

再次感谢, 鳗鱼

I am developing a custom print dialog and page setup using MFC and VS2008 for my Win32 program. Since the code is legacy, I can't take much advantage from MFC view/doc architecture. As a result, I wrote a printing code completely from scratch.

I setup CPrintInfo, instantiate my custom print dialog box and hook this dialog box to the CPrintInfo I just created. When my custom print dialog is up, I have a radio button to let a user toggles the page orientation. For some reasons, I couldn't modify the current DEVMODE at the run-time. As a result, every page I print will end up as a portrait.

Even if I manually set pDevMode->dmOrientation to DMORIENT_LANDSCAPE from the event handler of the custom print dialog, the printing result is still ended up as portrait. I am really not sure why this is happening and how to modify the DevMode after the print dialog is up.

Thank you in advance for any help.

Here is the code I have:

void PrintSomething(CWnd* currentWnd) {
  // Create CPrintInfo
  CPrintInfo* pPrintInfo = new CPrintInfo;
  SetupPrintInfo(pPrintInfo); // simply setup some member variables of CPrintInfo

  // Create a custom print dialog 
  CustomPrintDlg* pCustomPrtDlg = new CustomPrintDlg(FALSE, PD_ALLPAGES | PD_USEDEVMODECOPIES | PD_NOPAGENUMS
           | PD_HIDEPRINTTOFILE | PD_NOSELECTION, pPrintInfo, currentWnd);
  SetupPrintDialog(pPrintInfo,pCustomPrtDlg);

  if ( AfxGetApp()->DoPrintDialog(pCustomPrtDlg) == IDOK ) {
        ... // proceed a print loop 
  }
}

Code for setting up the custom print dialog:

void SetupPrintDialog(CPrintInfo* pPrintInfo,CustomPrintDlg* pCustomPrtDlg) {
  delete pInfo->m_pPD;
  pInfo->m_pPD = pCustomPrtDlg;

  pInfo->m_pPD->m_pd.hInstance = AfxGetInstanceHandle();
  pInfo->m_pPD->m_pd.lpPrintTemplateName = MAKEINTRESOURCE(IDD_CUSTOM_PRTDLG);

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

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

When a user toggles the radio button to Landscape, this function will be invoked:

void CustomPrintDlg::OnLandscapeChecked() {
  // set the current Devmode to landscape
  LPDEVMODE pDevMode = GetDevMode();
  GlobalUnlock(pDevMode);
  pDevMode->dmOrientation = DMORIENT_LANDSCAPE;
}

A pseucode for the custom print dialog class:

class CustomPrintDlg: public CPrintDialog {

  ...   // just override some methods from CPrintDialog
};

Thanks again,
Unagi

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

樱桃奶球 2024-10-22 16:52:49

我找到了解决方案:

我需要的只是在更改当前 DevMode 之前调用 GlobalLock 来获取指向 Devmode 的指针。

void CustomPrintDlg::OnLandscapeChecked()
{
      // set the current Devmode to landscape
      LPDEVMODE pDevMode = GetDevMode();
      GlobalLock(pDevMode);
      pDevMode->dmOrientation = DMORIENT_LANDSCAPE;
      GlobalUnlock(pDevMode)
}

再次感谢您对我的帮助。

I figured out the solution:

All I need is to call GlobalLock to obtain a pointer to the Devmode before changing the current DevMode.

void CustomPrintDlg::OnLandscapeChecked()
{
      // set the current Devmode to landscape
      LPDEVMODE pDevMode = GetDevMode();
      GlobalLock(pDevMode);
      pDevMode->dmOrientation = DMORIENT_LANDSCAPE;
      GlobalUnlock(pDevMode)
}

Thanks again for helping me.

数理化全能战士 2024-10-22 16:52:49

示例代码中没有显示如何创建用于打印的 DC。当你调用CreateDC时,你必须传递一个指向DEVMODE结构的指针;这定义了打印是纵向还是横向。

Nowhere in your example code do you show how you're creating the DC for printing. When you call CreateDC, you must pass a pointer to a DEVMODE structure; this defines whether the printing will be portrait or landscape.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文