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

问题描述 投票:0回答:2

我在Visual C#(.net 2.0)应用程序中使用WebBrowser控件。现在,我想添加一个显示页面设置对话框的打印按钮,然后在用户按下“确定”按钮时直接打印,或者在用户按下“取消”按钮时取消打印。但是,WebBrowser.ShowPageSetupDialog不会返回DialogResult,而只是返回void。有什么我想念的或其他了解用户操作的方法吗?

c# .net printing webbrowser-control dialogresult
2个回答
1
投票

0
投票
[当您在IE页面设置中按OK按钮时,它将存储在设置中的边距值存储为长度为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将被视为不同。

这就是窍门:

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

  1. 0.750000-> 0.75

    0.500000-> 0.5

    1.000000-> 1

      将边距值之一存储在字符串变量中
  2. 调用WebBrowser.ShowPageSetupDialog()

  3. 如果用户按OK,则注册表中的边距值将补0。否则,它们将保持修剪,如第1点所述。

  4. 将注册表中的边距值与变量中存储的边距值进行比较,如果它们相同,则用户按下'Cancel',否则用户按下'OK'。

  • 示例:

    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() }

  • © www.soinside.com 2019 - 2024. All rights reserved.