【发布时间】:2022-10-01 08:23:42
【问题描述】:
问题:我需要让PrintDlgEx 为我的项目工作,但选项或参数的组合对我不起作用。它为任何选项组合提供E_INVALIDARG,就像我从 Microsoft 示例或其他在线示例中复制的那样。
用PRINTDLG 替换PRINTDLGEX 和用PrintDlg 替换PrintDlgEx(并且只从PRINTDLGEX 中删除选项组)效果很好。
不幸的是,我需要PrintDlgEx,因为我真的需要Apply 按钮来更改打印机或属性表而不打印,用于设计和预览。
请帮我找出为什么我无法显示对话框。
代码:虽然我简化了部分,比如成功返回时应该发生的事情,或者设置DEVMODE 和DEVNAMES,但我完全尝试了这个函数,结果相同:无效参数。
#include <QDebug>
#include <QWindow>
#include <windows.h>
void showPrintDialog()
{
// Simplifying the setup: real code passes in a QWidget *
QWidget *caller = this;
// Not returning a value or doing any work. I just want the dialog to pop up for now
// Create the standard windows print dialog
PRINTDLGEX printDialog;
memset(&printDialog, 0, sizeof(PRINTDLGEX));
printDialog.lStructSize = sizeof(PRINTDLGEX);
printDialog.Flags = PD_RETURNDC | // Return a printer device context. Without this, HDC may be undefined
PD_USEDEVMODECOPIESANDCOLLATE |
PD_NOSELECTION | // Don\'t allow selecting individual document pages to print
PD_NOPAGENUMS | // Disables some boxes
PD_NOCURRENTPAGE | // Disables some boxes
PD_NONETWORKBUTTON | // Don\'t allow networking (but it show \"Find printer\") so what does this do ?
PD_HIDEPRINTTOFILE; // Don\'t allow print to file
// Only on PRINTDLGEX
// Theis block copied from https://learn.microsoft.com/en-us/windows/win32/dlgbox/using-common-dialog-boxes?redirectedfrom=MSDN
// I have tried multiple combinations of options, including none, I really don\'t want any of them
printDialog.nStartPage = START_PAGE_GENERAL;
printDialog.nPageRanges = 1;
printDialog.nMaxPageRanges = 10;
LPPRINTPAGERANGE pageRange = (LPPRINTPAGERANGE) GlobalAlloc(GPTR, 10 * sizeof(PRINTPAGERANGE));
printDialog.lpPageRanges = pageRange;
printDialog.lpPageRanges[0].nFromPage = 1;
printDialog.lpPageRanges[0].nToPage = 1;
printDialog.Flags2 = 0;
printDialog.ExclusionFlags = 0;
printDialog.dwResultAction = 0; // This will tell me if PRINT
// Rest of options are also on PRINTDLG
printDialog.nMinPage = 1;
printDialog.nMaxPage = 10;
// The only options I really need
printDialog.nCopies = 1;
printDialog.hDevMode = Q_NULLPTR; // which will be better once this works
printDialog.hDevNames = Q_NULLPTR; // which will be better once this works
printDialog.hwndOwner = reinterpret_cast<HWND>(caller->windowHandle()->winId());
// Calling...
int result = PrintDlgEx(&printDialog);
qDebug() << (result == E_INVALIDARG ? \"Invalid Argument\\n\" : \"Success\\n\");
// It always is E_INVALIDARG
// Cleanup
if (printDialog.hDevMode)
GlobalFree(printDialog.hDevMode);
if (printDialog.hDevNames)
GlobalFree(printDialog.hDevNames);
if (printDialog.hDC)
DeleteDC(printDialog.hDC);
}
平台:Windows 10,最新更新;
Qt 版本:5.12.7 或更高
(因为在 VM 中我有 5.15.1)
我在 Qt 中运行的事实应该是无关紧要的,因为这都是 WIN API,超出了 c++ 版本 (11)
标签: c++ windows winapi printing printdialog