如何从 WebBrowser.ShowPageSetupDialog 获取对话框结果

发布于 2024-07-10 20:43:55 字数 220 浏览 11 评论 0原文

我在 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 技术交流群。

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

发布评论

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

评论(2

戒ㄋ 2024-07-17 20:43:55

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.

最佳男配角 2024-07-17 20:43:55

我遇到了同样的问题,通过观察 IE 页面设置在 @"Software\Microsoft\Internet Explorer\PageSetup" 注册表中存储边距值的方式,能够找到一个巧妙的解决方法。

当您在IE页面设置中按确定按钮时,它会将设置中写入的边距值存储为长度为8的字符串(REG_SZ),剩余空间用0填充。

0.75 存储为 0.750000

1.0 存储为 1.000000

2 存储为 2.000000

当您使用 WebBrowser.Print() 时,它将边距值转换为浮点数,因此在注册表中使用 0.75 或 0.750000 作为边距值会产生相同的结果。

但是,如果将它们作为字符串进行比较,则 0.75 和 0.750000 将被视为不同。

技巧来了:

  1. 在调用 WebBrowser.ShowPageSetupDialog() 之前,删除注册表边距值中的尾随 0

0.750000 -> 0.75

0.500000 -> 0.5

1.000000 -> 1

  1. 将边距值之一存储在字符串变量中

  2. 调用 WebBrowser.ShowPageSetupDialog()

  3. 如果用户按下“确定”,注册表中的边距值将用 0 填充。 否则,它们将保持第 1 点中提到的修剪状态。

  4. 将注册表中的边距值与存储在变量中的边距值进行比较,如果它们相同,则用户按“取消”,否则用户按“确定” '.

例子 :

private void ie_DocumentCompleted(object _sender, WebBrowserDocumentCompletedEventArgs e)
{
    System.Windows.Forms.WebBrowser ie = (System.Windows.Forms.WebBrowser)_sender;

    string strKey = "Software\\Microsoft\\Internet Explorer\\PageSetup";
    bool bolWritable = true;
    RegistryKey ok = Registry.CurrentUser.OpenSubKey(strKey, bolWritable);

    ok.SetValue("margin_left", 0.75, RegistryValueKind.String);

    string reg_validation = (string) ok.GetValue("margin_left");

    ie.ShowPageSetupDialog();

    if (reg_validation.Equals((string)ok.GetValue("margin_left")))
    {
        MessageBox.Show("Cancel");
    }
    else
    {
        MessageBox.Show("OK");
        ie.Print();
    }
    ok.Close()
}

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.

i.e.

0.75 is stored as 0.750000

1.0 is stored as 1.000000

2 is stored as 2.000000

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 :

  1. Before calling WebBrowser.ShowPageSetupDialog(), remove the trailing 0s in registry's margin values

i.e.

0.750000 -> 0.75

0.500000 -> 0.5

1.000000 -> 1

  1. Store one of the margin values inside a string variable

  2. Call WebBrowser.ShowPageSetupDialog()

  3. 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.

  4. 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 :

private void ie_DocumentCompleted(object _sender, WebBrowserDocumentCompletedEventArgs e)
{
    System.Windows.Forms.WebBrowser ie = (System.Windows.Forms.WebBrowser)_sender;

    string strKey = "Software\\Microsoft\\Internet Explorer\\PageSetup";
    bool bolWritable = true;
    RegistryKey ok = Registry.CurrentUser.OpenSubKey(strKey, bolWritable);

    ok.SetValue("margin_left", 0.75, RegistryValueKind.String);

    string reg_validation = (string) ok.GetValue("margin_left");

    ie.ShowPageSetupDialog();

    if (reg_validation.Equals((string)ok.GetValue("margin_left")))
    {
        MessageBox.Show("Cancel");
    }
    else
    {
        MessageBox.Show("OK");
        ie.Print();
    }
    ok.Close()
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文