我在Visual C#(.net 2.0)应用程序中使用WebBrowser控件。现在,我想添加一个显示页面设置对话框的打印按钮,然后在用户按下“确定”按钮时直接打印,或者在用户按下“取消”按钮时取消打印。但是,WebBrowser.ShowPageSetupDialog不会返回DialogResult,而只是返回void。有什么我想念的或其他了解用户操作的方法吗?
即
0.75存储为0.7500001.0被存储为1.000000
2被存储为2.000000
当您使用WebBrowser.Print()时,它将边距值转换为浮点,因此在注册表中将0.75或0.750000作为边距值会产生相同的结果。
但是,如果将它们作为字符串进行比较,则0.75和0.750000将被视为不同。
这就是窍门:
在调用WebBrowser.ShowPageSetupDialog()之前,删除注册表的边距值中的尾随0
0.750000-> 0.750.500000-> 0.5
1.000000-> 1
调用WebBrowser.ShowPageSetupDialog()
如果用户按OK,则注册表中的边距值将补0。否则,它们将保持修剪,如第1点所述。
将注册表中的边距值与变量中存储的边距值进行比较,如果它们相同,则用户按下'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()
}