Recommendations for painting a custom text viewer - c#

I want to develop a file viewing program for a specific read-only file format and for the most part it will just be scrollable text. The ultra-simple way is of course to use an existing text display controls, but I've come to the conclusion that I want a graphical sort of "custom colored highlighting", text coloring, and maybe other things painted in. So I was planning to handle the painting myself. I take it that attempting to line up my own graphics on top of a label or rich text box would be a bad idea, so I was planning to just paint everything except the scroll bar... unless these labels/rich text controls are a lot more extensible in some way that I don't know about?
Assuming I go the painting route, I'm not 100% sure of the specifics. Do I paint directly into a Panel? Or is there a better GUI control to paint into? Also, I think it will be better if I don't buffer the screen because I think repainting the contents on validation will be easy/efficient... but repainting from a buffer might be even faster... will it save me a lot of trouble if I just have a screen buffer... is this significantly inefficient? Is my plan of painting directly into a Panel, unbuffered, a good idea or is there a preferred method that I'm passing up?

Try to use WebBrowser control orRichTextBox (make formatted html or rtf from your text).

Related

Creating a picturebox/mapview type control with clickable rectangles and animated icons

I'm looking to add a 'mapview' type control to my project.
It must have a 'main map' image with clickable transparent rectangles with borders and icons/images that can be animated when an event occurs.
What would be the best way of achieving this using windows forms in C#?
My first thought was to use a picture box with other items on top of it but I might run into problems with transparency etc.
Are there any libraries or anything out there that would be able to achieve this?
No need for a library, really:
I would go for a regular doublebuffered Panel subclass or even a PictureBox subclass for the board/map along with a movable Label or Panel subclass fpr the rectangles/items.
Important: Make sure the Labels are not just 'put on top' of the PictureBox but really nested!! (lbl.Parent = pbox). Then transparency will work just fine..
Since PictueBox is not a 'container', to nest a control in it you need code. But since you probably want to create them dynamically this is not an issue anyway.
This assumes that the rectangles are not overlapping! For overlapping controls transparency in winforms will not work.
The clearer you understand the 'animate when event' part the easier the rest of the code will be..
Since you mention 'animation', a word of warning: Simple animation, especially in reponse to a user action is doable; for more classy animation you may run into the limits of winforms.

Custom Text Box Component / Element (Not a Control)

I am in need of a class that mimics a TextBox control but is not a Control, but instead a custom drawn component or element.
Creating one feels like re-inventing the wheel since I see them everywhere. For example, in any modern web browser the text boxes are not controls. Most Winforms controls, especially ToolStrip controls such as ToolStripTextBox, have elements which behave like text boxes (but are not Controls).
I assume that Microsoft doesn't reinvent the wheel for each control they make it. But most likely their code is proprietary and not public.
Does any one know of an open source solution for this? I am experienced with GDI+ drawing but a text field is not a trivial task when you consider caret positioning, selection, and inserting text.
Any pointers on how to go about writing the code myself would be appreciated, such as how to calculate the character at a given point. Should I create a lookup table for the measured width of each possible character? Or loop through MeasureString to take into account formatting space?
You may find the code you need inside this article/project. http://www.codeproject.com/Articles/161871/Fast-Colored-TextBox-for-syntax-highlighting
Why must it not be a Control? If you're using Windows Forms, it is far more likely that you really want a control.
Common cases where this type of question might come up are Grid editing. Instead of a non-control TextBox, what normally happens is that the grid displays simple text in the grid until the user focuses on that grid. At that point a temporary, real TextBox is inserted for editing. Leaving that cell throws the TextBox away and the possibly-changed text is now displayed by the Grid.
I assume your situation is similar. If not, please explain your goals.

Creating Overlay Control in winforms

I am using c# winforms to show an Image. The displaying of the image is done using a user control. Now I want to provide the user to draw lines, put other small images, write text etc over the image on an overlay control. How can I provide this functionality? If I use another user control to show the overlay control with transparent back, will that work?? any other solution will be welcome.
You might try approaching this with a canvas (Panel) that handles painting the image as the background and all the annotations/markup afterwards. This will make the foreground appear to be transparent. I expect you'll want to set Control.DoubleBuffer for performance.
You might experiment with setting the style ControlStyles.AllPaintingInWmPaint. Also, try overriding Control.OnPaintBackground and do nothing, and override Control.OnPaint and do all your painting inside there.
If performance is still unacceptable, pay close attention to the PaintEventArgs.ClipRect property. This is the only area you need to paint. The trick is figuring out which of your annotations/overlays intersect with this rectangle and painting them in the correct order.
Either this canvas or a higher level control will need to track mouse movement so you know where to draw the lines, paste images, etc.

Looking for suggestions on how to create an enhanced tooltip

currently I am using a tool tip to display information when it hovers over a region on a winform. This works well and I don't have any complaints, but the boss want's to display more complex data, that would best be displayed in a grid rather than text.
Is there a way that perhaps I could embed a usercontrol or a datagridview in a tool tip.
thanks
C#, .Net 2.0, windows.Forms
There's such a thing as an owner-drawn tooltip. You'd have to handle the painting of the grid yourself. You wouldn't get any interactivity, although a tooltip that lets you click and scroll sounds odd anyway.
If your boss is willing to spend money on this then I can happily recommend the DevExpress tooltip control, for its customisability.
You can write a custom control (shouldn't be too hard, just a yellow rectangle with a drop shadow) with a data grid on it. It just needs to fade out when the mouse moves away and get displayed after the mouse rests on it for a couple of seconds.

Use Label or DrawString in UserControl

I am writing my own control that will contain a panel with text, images and other media. What is the best way to render the text and images. The control may contain long texts and many images.
Should I add the text as labels and images as PictureBox or should I use the DrawString and DrawImage methods to render the text and images?
What's the best way to do this?
If you use labels, then you get all the labelly goodness for free.
If you use DrawString, then it'll probably be (a little bit) faster, but it's a lot more complicated if you need to deal with things like the text changing.
The OnPaint handler is a always a tricky one to write, and invalidating the client area is tricky to do efficiently.
Why not let the labels handle it all for you?
I would use DrawString and DrawImage - you have less resources to worry about, but with added complexity.
I don't think it's that bad drawing your own strings and images once you get into it.
This is a nice introduction to it:
https://web.archive.org/web/20150116060854/http://bobpowell.net/

Categories

Resources