问题:我需要让PrintDlgEx
为我的项目工作,但没有任何选项或参数组合对我有效。它为任何选项组合提供E_INVALIDARG
,就像我从Microsoft示例或其他在线示例中复制的一样。
用PRINTDLG
替换PRINTDLGEX
,用PrintDlg
替换PrintDlgEx
(并仅从PRINTDLGEX
中删除该组选项(效果良好。
不幸的是,我需要PrintDlgEx
,因为我真的需要Apply
按钮,在不打印的情况下更改打印机或属性表,用于设计和预览。
请帮我找出为什么我无法显示对话框。
代码:虽然我简化了一些部分,比如成功返回时应该发生的事情,或者设置DEVMODE
和DEVNAMES
,但我完全尝试了这个函数,得到了相同的结果:Invalid Argument。
#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 Argumentn" : "Successn");
// 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(
如果删除PD_NONETWORKBUTTON
标志,我可以使您的示例工作。
请注意,虽然它是为PRINTDLGA结构记录的,但它没有列在PRINTDLGEXA 中
注意:我确实在使用该标志时遇到了同样的错误。