简体   繁体   English

C#PrintDocument已更改事件

[英]C# PrintDocument Changed event

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.). 我的问题是我创建了一个“扩展”的RichTextBox控件,它使用本机API添加标准控件中缺少的许多RichEdit功能(即:修改选择的单个字体属性,而不更改其他字体属性,自动换行到打印机[如写字板]等)。 As part of the control I expose a PrintDocument that is used to print the formatted contents of the RichTextBox. 作为控件的一部分,我公开了一个PrintDocument,用于打印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. 当wordwrap设置为“Wrap To Printer”时,我将EM_SETTARGETDEVICE消息发送到RichTextBox,并使其换行到适当的长度。

This all works fine when something (user/code) changes the WordWrap property of my control. 当某些内容(用户/代码)更改了我的控件的WordWrap属性时,这一切都正常。 However if the PrintDocument is modified after that I have no way of knowing it. 但是,如果之后修改了PrintDocument,我无法知道它。 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. 因此,即使用户可能更改了PrintDocument上的边距,我的RichTextBoxEx也不会为更新的宽度重新发送EM_SETTARGETDEVICE,直到WordWrap属性发生更改。

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. 添加一个UpdatePrintDocument()方法或类似的方法,需要在控件外部的东西之后调用(即:父窗体上的PageSetupDialog)更新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. 虽然我可能记得在我成功更新PrintDocument设置时随时调用该方法,但其他人可能不会。 Pro: It's simple to implement. 亲:实施起来很简单。

  2. Create a new, PrintDocumentEx class that bases from PrintDocument and implements the needed "Changed" events. 创建一个新的PrintDocumentEx类,它基于PrintDocument并实现所需的“Changed”事件。 Cons: Might not be enough, might need to create PrintSettingsEx, PageSettingsEx, etc.. Pro: Implement once and no one has to worry about it again. 缺点:可能还不够,可能需要创建PrintSettingsEx,PageSettingsEx等.Pro:实施一次,没有人再担心它。

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. 我真的认为#2是我必须选择的选项但是对于下一个实例它不是非常可重用我需要一些类似的功能。 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. 我想我正在寻找的是一种将“通用PropertyChanged事件”附加到任何现有类属性的方法,因为这将适用于未来的情况。 Anxious to see what you guys have for me :-) 急于看到你们对我有什么:-)

If I have understood your question correctly, the information that you require is sent when the WordWrap property is changed. 如果我已正确理解您的问题,则在更改WordWrap属性时会发送您需要的信息。

When other things are changed, no events updates the Print Document. 更改其他内容时,没有任何事件更新打印文档。 The next time the WordWrap property is changed all information is sent. 下次更改WordWrap属性时,将发送所有信息。

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. 修复此问题的黑客方法是在更改要发送到打印文档的属性时更改WordWrap属性。 Change it to a temporary value, then change it back again. 将其更改为临时值,然后再将其更改回来。

Just following up that my PrintDocumentEx (and associated) classed didn't work. 只是跟进我的PrintDocumentEx(和相关的)类别不起作用。 Looking into the PrintDialog and related controls it's because they use native methods to acctually update the PrintDocument. 查看PrintDialog及相关控件是因为它们使用本机方法来实际更新PrintDocument。 So the events I attached to the properties in my "Ex" classes never fired because the set accessor was never invoked. 因此,我附加到“Ex”类中的属性的事件从未被触发,因为从未调用set访问器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM