most economic way to display formatted rich text in wpf? - c#

I'm looking for basically fastest way to display non-editable FlowDocument with continuous, variously decorated text.
I know RichTextBox and usually use FlowDowcumentScrollViewer that can display FlowDocument content.
I know TextBlock can (kind of) be fed Inlines in code, but doens't support Paragraphs etc.
But did anybody measure which of the former two are faster with different occasions (scrolling / non scrolling, long / shorter document etc.) ?
Or is there some other, more effective solution ?
Some people claim that FlowDowcumentScrollViewer is actually slower because it supports more (advanced) formatting scenarios. Not sure if thats true.
On the other hand RichTextBox has all the editing functionality etc. that drags its speed down.
As Im currently writing a RichText heavy application, I would be glad for any tips on this

Related

Suppressing word wrap in TextBox and RichTextBox (yes, it is turned off)

I have tried both the regular TextBox and RichTextBox in an app where I don't want any word wrap. Yes, of course I turned off word wrap in Properties. This is something that occurs at extremely long lines, like around 3000 chars or so in the RichTextBox (regular TextBox much sooner, but I can get by without it if RichTextBox works). I recognize that it is cruel and unusual punishment to subject the poor RichTextBox to such line lengths, but unavoidable. Still, it seems like abnormal behavior.
The text is just long continuous strings of repetitive ascii (genetic data), so nothing unusual there. In fact it's usually just A,C,T,G with no punctuation or spaces.
Is this known behavior?
Assuming you are using WinForms, both RichTextBox and TextBox have built-in limits for lengths of lines.
An alternative is to use a scroll viewer and manually add Labels. I do not believe there will be any limit in this scenario, although I could be wrong.
If you check Notepad (notepad.exe) in Windows, it also has a limit before it starts wrapping text, so this is standard behavior for Windows apps.
the Multiline property should be false in order for the lines not to wrap.
You could try a RichTextBox and extend the right margin to the width of the text.
richTextBox1.RightMargin = TextRenderer.MeasureText(yourString, this.richTextBox1.Font).Width;
This way you maintain the ability to have other lines.

How to display large amount of text for ereader application

I need to create a windows store application with e reader functionality. I have to provide a fluid reading experience. Currently, my knowledge of displaying text consists of dropping textblock or textbox on the UI. I do not know how to display large amount of text to create the E Reader experience i am looking for. Which control or combination of controls should i use?
Your text rendering options are
XAML controls
TextBlock
RichTextBlock
WebView + WebViewBrush
DirectWrite - what all the above ultimately use. You can use it with C# through SharpDX.
The controls might be a bit cumbersome to use at times if you need to do measurements etc. and might not give you as much power or performance as DirectWrite does, but will give you support for text selection, copying to clipboard etc. (make sure IsTextSelectionEnabled is set to true).
For measuring text dimensions for paged display in a TextBlock - create a TextBlock in code behind and call Measure() and Arrange(), then get ActualWidth/ActualHeight to get the measurement.
Read Charles Petzold's Principles of Pagination article.
Consider Rapid Serial Visual Presentation (RSVP) speed-reading method used in the ReadQuick app.

Cambria Math big top and bottom margin

I wanted to show some mathematical expressions in a winforms textbox. So I thought the "Cambria Math" font would be a good choice but the text looked strange due the high top and bottom margin of the font. First I thought I made a mistake but according to this question, it's the correct behavior of the font.
Why does Cambria Math have these big margin values and how can I display my string correctly in the textbox like Word 2010?
(Note that I know only a little bit about typography ;)
Edit: I had to make the textbox that tall otherwise the caret would be invisible. The font size of the textbox is set to 8.25pt
Cambria Math uses Microsoft's mathematical OpenType extensions.
Word 2007 and later understand these and display the text with reasonable spacing.
However, notepad and Word 2000 display the text with enormous spacing, just like winforms. I guess the font has this much space by default because some characters (like U+2320, top half integral) are much larger than the alphanumerics.
If you use Cambria Math with a font engine (such as the one used by winforms) that doesn't understand the math extensions, you're going to get the big spacing.
If you're displaying simple expressions you might as well use Cambria.

Using a (Rich)TextBox as a window onto a larger buffer

I feel like I'm missing an obvious point here, but let's say I'm working on a text editor, and I'd like to store a file's (partial?) contents as a text buffer. My text buffer is some specialized data structure for holding lots of text, not just a simple string or StringBuilder. I'd also like to expose the text to the user, probably through a standard text box or rich text box.
What is the best means of doing this?
Would I copy a segment out of the buffer into the textbox, then adjust everything manually and copy changes back? Would I copy the entire buffer as a string into the textbox? If I was only loading part of a file into the buffer (to handle very large files) would I have to set up a second translation layer from buffer to file? Do I need an entirely different control from the built in textboxes to accomplish this?
EDIT: I'd also accept suggestions for 3rd-party text controls that would work well. My main block here is not wanting to build/learn/import the entirety of (for example) GTK or QT just to get a decent text editor control.
See the ebook referenced from this question, the sharpdevelop guys go into some detail on how to construct a text editor.
I use a RichTextBox in logview4net as a window on whatever data I'm looking at.
It is only forward scrolling and I can't really say I like it.
I think you're better off looking for some other editor control.

C# Custom Button

I'm attempting to write a custom button user control. I have run into a challenge when drawing the image.
Is there a simple way to draw the image accounting for the ImageAlign and TextImageRelation? (Kind of like StringFormat makes text aligning a breeze)
Or do I have to do all the aligning logic and stuff manually?
Thanks
What functionality are you trying to achieve? Perhaps it should instead inherit from the Button class (asssuming WinForms), and override appropiate methods. Depending on what you need to do, you will propably get much of the lower levels of functionality in the button for free, if you do this.
To get back to your question; No, if you need to draw stuff yourself, there is no magic easy way to determine where the individual pixels should go :-) One great helper in doing this, that you should be aware of, is the Graphics.DrawString method. It lets you measure the dimensions of a given text string when drawn on the control with the selected font and size.
I do not know of anything that does this stuff for you, but be aware of the ControlPaint class as that has a bunch of handy utility methods for painting controls.

Categories