C# PrintDocument 更改事件

发布于 2024-08-13 18:23:37 字数 975 浏览 9 评论 0原文

我的问题是,我创建了一个“扩展”RichTextBox 控件,该控件使用本机 API 添加许多标准控件中缺少的 RichEdit 功能(即:修改选定内容上的单个字体属性而不更改其他字体属性,自动换行到打印机[如写字板]等)。作为控件的一部分,我公开了一个 PrintDocument,用于打印 RichTextBox 的格式化内容。当 wordwrap 设置为“换行到打印机”时,我将 EM_SETTARGETDEVICE 消息发送到 RichTextBox 并使其换行到适当的长度。

当某些内容(用户/代码)更改我的控件的 WordWrap 属性时,这一切都可以正常工作。但是,如果 PrintDocument 之后被修改,我就无法知道它。因此,即使用户可能更改了 PrintDocument 上的边距,我的 RichTextBoxEx 也不会重新发送新宽度的 EM_SETTARGETDEVICE,直到 WordWrap 属性发生更改。

我看到了一些克服这个问题的选择,但我不是其中任何一个的忠实粉丝。这是我所拥有的:

  1. 添加一个 UpdatePrintDocument() 方法或类似方法,在控件外部的某些内容(即:父窗体上的 PageSetupDialog)更新 PrintDocument 中的设置后需要调用该方法。缺点:我将分发控件,因此我希望使其尽可能友好。虽然我可能会记得在成功更新 PrintDocument 设置时调用该方法,但其他人可能不会。优点:实现起来很简单。

  2. 创建一个新的 PrintDocumentEx 类,该类基于 PrintDocument 并实现所需的“Changed”事件。缺点:可能还不够,可能需要创建 PrintSettingsEx、PageSettingsEx 等。优点:实现一次,无需再担心。

我真的认为 #2 是我必须选择的选项,但对于下一个我需要一些类似功能的实例来说,它的可重用性不太好。我想我正在寻找一种将“通用 PropertyChanged 事件”附加到任何现有类属性的方法,因为这适用于未来的情况。迫不及待地想看看你们给我带来了什么:-)

My issue is that I've created an "extended" RichTextBox control that uses native API to add many RichEdit features that are missing from the standard control (ie: modifying a single font property on a selection w/o changing the other font properties, word wrap to a printer [like WordPad], etc.). As part of the control I expose a PrintDocument that is used to print the formatted contents of the RichTextBox. When wordwrap is set to "Wrap To Printer" I SendMessage the EM_SETTARGETDEVICE message to the RichTextBox and cause it to wrap to the appropriate length.

This all works fine when something (user/code) changes the WordWrap property of my control. However if the PrintDocument is modified after that I have no way of knowing it. So even though the user may have changed the margins on the PrintDocument my RichTextBoxEx doesn't resend the EM_SETTARGETDEVICE for the new width until the WordWrap property is changed.

I see a few options to overcome this but I'm not a big fan of any of them. Here's what I have:

  1. Add a UpdatePrintDocument() method or similiar that would need to be called after something external from the control (ie: a PageSetupDialog on the parent form) updated the settings in the PrintDocument. Cons: I'll be distributing the control so I'd like to make it as friendly as possible. While I may remember to call the method anytime I successfully update the PrintDocument settings someone else might not. Pro: It's simple to implement.

  2. Create a new, PrintDocumentEx class that bases from PrintDocument and implements the needed "Changed" events. Cons: Might not be enough, might need to create PrintSettingsEx, PageSettingsEx, etc.. Pro: Implement once and no one has to worry about it again.

I really think #2 is the option I'm going to have to go with but it's not very reusable for the next instance I need some similiar functionality. I guess what I'm looking for is a way to attach a "generic PropertyChanged event" to any existing classes property since this would be applicable in future situations. Anxious to see what you guys have for me :-)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

吐个泡泡 2024-08-20 18:23:37

如果我正确理解您的问题,则更改 WordWrap 属性时会发送您所需的信息。

当其他事情发生变化时,没有事件更新打印文档。下次更改 WordWrap 属性时,将发送所有信息。

解决此问题的最佳方法是,每当您更改要发送到打印文档的属性时,更改 WordWrap 属性。将其更改为临时值,然后再次更改回来。

If I have understood your question correctly, the information that you require is sent when the WordWrap property is changed.

When other things are changed, no events updates the Print Document. The next time the WordWrap property is changed all information is sent.

The hack way to fix this is then to change the WordWrap property, whenever you change a property that you want to send to the Print Document. Change it to a temporary value, then change it back again.

灯角 2024-08-20 18:23:37

只是跟进我的 PrintDocumentEx (和相关的)类不起作用。查看 PrintDialog 和相关控件,这是因为它们使用本机方法来实际更新 PrintDocument。因此,我附加到“Ex”类中的属性的事件从未触发,因为从未调用 set 访问器。

Just following up that my PrintDocumentEx (and associated) classed didn't work. Looking into the PrintDialog and related controls it's because they use native methods to acctually update the PrintDocument. So the events I attached to the properties in my "Ex" classes never fired because the set accessor was never invoked.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文