如何从 WebBrowser.ShowPageSetupDialog 获取对话框结果
我在 Visual C# (.net 2.0) 应用程序中使用 WebBrowser 控件。
现在我想添加一个打印按钮,该按钮显示页面设置对话框,然后在用户按“确定”按钮时直接打印,或在用户按“取消”按钮时取消打印。
但是 WebBrowser.ShowPageSetupDialog 不会返回 DialogResult,而只是返回 void。
是否有我错过的东西或任何其他方式来了解用户的操作?
I'm using a WebBrowser control in my Visual C# (.net 2.0) application.
Now I'd like to add a print button which shows the page setup dialog and then directly prints when the user presses the OK button or cancels the printing when the user presses the cancel button.
However WebBrowser.ShowPageSetupDialog doesn't return DialogResult, but just void.
Is there something I've missed or any other way to know the users action?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
WebBrowser 控件的页面设置对话框设置注册表项。 我过去所做的就是在代码中为用户设置这些值,并且只给他们打印的选项。
The page setup dialog box off a WebBrowser control sets registry entries. What I've done in the past was to set those values for the user in code and only gave them the option to print.
我遇到了同样的问题,通过观察 IE 页面设置在 @"Software\Microsoft\Internet Explorer\PageSetup" 注册表中存储边距值的方式,能够找到一个巧妙的解决方法。
当您在IE页面设置中按确定按钮时,它会将设置中写入的边距值存储为长度为8的字符串(REG_SZ),剩余空间用0填充。
当您使用 WebBrowser.Print() 时,它将边距值转换为浮点数,因此在注册表中使用 0.75 或 0.750000 作为边距值会产生相同的结果。
但是,如果将它们作为字符串进行比较,则 0.75 和 0.750000 将被视为不同。
技巧来了:
将边距值之一存储在字符串变量中
调用 WebBrowser.ShowPageSetupDialog()
如果用户按下“确定”,注册表中的边距值将用 0 填充。 否则,它们将保持第 1 点中提到的修剪状态。
将注册表中的边距值与存储在变量中的边距值进行比较,如果它们相同,则用户按“取消”,否则用户按“确定” '.
例子 :
I had the same problem, was able to find a gimmicky workaround by observing the way IE Page Setup stores margin values in the @"Software\Microsoft\Internet Explorer\PageSetup" registry.
When you press the OK button in IE Page Setup, it stores the margin values written in the setup as string (REG_SZ) with a length of 8, with the remaining space padded with 0s.
When you use WebBrowser.Print(), it converts the margin values into floating points, so having 0.75 or 0.750000 as margin values in the registry produces the same result.
However, if you compare them as strings, 0.75 and 0.750000 would be regarded as different.
And here comes the trick :
Store one of the margin values inside a string variable
Call WebBrowser.ShowPageSetupDialog()
If the user pressed OK, the margin values in the registry would be padded back with 0s. Else, they would remain trimmed as mentioned in point 1.
Compare the margin values in the registry with the one stored in the variable, if they are the same, then the user pressed 'Cancel', otherwise the user pressed 'OK'.
Example :