views:

35

answers:

1

Important: we really need the stated functionality, so please don't start suggesting alternatives.

We're trying to display and input text on a custom WinForms control that includes text and non-text regions - in other words, a modern canvas.

We need to be able to display the text and caret on the custom control at various zoom levels and using other custom effects. However, we do not want (or have the wherewithal) to write keyboard and mouse input method code, line services code, etc - the solution must support all languages that the standard TextBox supports.

Therefore, the approach we're considering is to use a hidden textbox, and to route keyboard and mouse events to it after suitable translation, and to use its public accessors to determine how the characters are laid out. This would theoretically enable us to highlight selected text, draw a caret, perform the right actions when the user presses a key (including selecting and deleting text with the keyboard.)

In other words, the text region is a proxy of the hidden textbox. The result is that we build on the functionality of the textbox while overcoming its limitations such as lack of zoom, etc.

We expect that the display part is doable but the keyboard and mouse input are probably very tricky.

Questions: Has this been tried before with any success? Any working code would be excellent.

Update: A quick and dirty experiment seems to indicate that the TextBox does not respect the mouse coordinates sent in mouse messages but seems to read the current mouse position directly. Therefore, a new question:

Is it possible to cast all TextBox mouse events in terms of direct TextBox actions, in a way that would work across all languages? Our guess that the hit testing method GetCharIndexFromPosition together with the text selection method Select, should suffice. Does this seem reasonable?

+1  A: 

I would recommend that you use a hidden TextBox as you suggest and then send it WM_PRINT messages in order to have its content drawn onto a bitmap. Then you can draw that bitmap onto the actual area of interest and in doing apply changes such as zooming or rotation and so forth. Any keyboard messages can be simply forwarded to the hidden TextBox and so the caret and contents will be updated as expected by the user. The tricky part is just the mouse handling. You need to take the client coordinate and then use the reverse transformation (zooming, rotation etc) to get from client to the actual coordinates of the hidden TextBox.

Phil Wright
Thanks, just added an update asking about turning mouse events into textbox actions.
bright