最后更新日期 2004年9月29日 15:36:58
.NET FCL 中的 PageSetupDialog 的标题文字一直是固定不变的“页面设置”,而且没有提供改变的属性或其他方式。由于该对话框组件是 sealed,无法通过继承的方式来获取其窗口的句柄。事实上也无法通过 FindWindow 等 WIN32 API 来得到。
没办法,又只能祭出 Windows Hook 这一法宝。
本文不介绍 Windows Hook 的原理和 C# 实现,详细的参考资料请参阅: http://msdn.microsoft.com/msdnmag/issues/02/10/CuttingEdge/(关于 Windows Hook 的详细介绍,本例使用其类) 在读本文之前,请先拜读上文。
其他参考:http://blog.csdn.net/ahbian/archive/2004/09/26/TimableMessageBox.aspx
但本例在 Win98 上测试时未能通过,发生了不可恢复的错误(不是 .NET 的异常)。错误信息忘记了,已是很久以前的事了。其原因也不清楚,如果哪位兄台能知一二,烦请不要忘记赐教。
public class PageSetupDialogEx {
protected LocalCbtHook m_cbt; protected PageSetupDialog m_dialog; protected IntPtr m_handle = IntPtr.Zero; protected bool m_alreadySetup = false;
private string title; public PageSetupDialogEx(PageSetupDialog dialog) { if (dialog == null) throw new ArgumentNullException("Must set a valid PageSetupDialog.");
this.m_dialog = dialog;
m_cbt = new LocalCbtHook(); m_cbt.WindowCreated += new CbtEventHandler(WndCreated); m_cbt.WindowActivated += new CbtEventHandler(WndActivated); }
public PageSetupDialogEx(PageSetupDialog dialog, string title) : this(dialog) { this.title = title; }
public string Title { get { return this.title; } set { this.title = value; } }
// Show public DialogResult ShowDialog(IWin32Window owner) { // 调整一下单位,为什么要这么做,你应该知道的吧。 PageSettings ps = this.m_dialog.PageSettings; ps.Margins = PrinterUnitConvert.Convert(ps.Margins, PrinterUnit.Display, PrinterUnit.TenthsOfAMillimeter);
DialogResult dr; m_handle = IntPtr.Zero; m_cbt.Install(); dr = m_dialog.ShowDialog(owner);
// 如果结果不是 OK,重新恢复 if (dr != DialogResult.OK) { ps.Margins = PrinterUnitConvert.Convert(ps.Margins, PrinterUnit.TenthsOfAMillimeter, PrinterUnit.Display); } return dr; }
public DialogResult ShowDialog() { return this.ShowDialog(null); }
// WndCreated event handler private void WndCreated(object sender, CbtEventArgs e) { if (e.IsDialogWindow) { m_alreadySetup = false; m_handle = e.Handle; } } // WndActivated event handler private void WndActivated(object sender, CbtEventArgs e) { if (m_handle != e.Handle) return;
// Not the first time if (m_alreadySetup) { return; } else m_alreadySetup = true;
#region Modify the Title if (this.title != null && this.title.Trim() != "") WindowsAPI.SetWindowText(m_handle, title); // WIN32 API 调用,设置标题文字
#endregion
m_cbt.Uninstall(); } } 
|