views:

278

answers:

2

This is a GUI application (actually MFC). I need a command window with the ability to display a prompt like such:

Name of favorite porn star:

The user should be able to enter text after the prompt like such:

Name of favorite porn star: Raven Riley

But I need to prevent the user from moving the cursor into the prompt area. Users should also be prevented from backspacing into the prompt in order to prevent the following:

Rrraven Rrrileeey Ruuuulez!!! Name of favorite porn star:

Also need to control text selection and so on. And finally, I should have no problem retrieving only the text the user entered (minus prompt text).

Will it be better to create my own window class from scratch (i.e inherit from CWnd) or should I reuse the Windows EDIT control (i.e. inherit from CEdit)?

A similar command window can be seen in AutoCAD and Visual Studio (in debug mode).

+3  A: 

I think you'd be better off creating a subclass of CEdit and limiting filtering key-presses. I suppose the hard part is not letting the user move the caret to the prompt area, but you can probably write some code to make sure the caret always get sent back to where it belongs (the input part).

Anyway, if you really, really want to implement your own control (it's not that difficult after all) I recommend you read Jacob Navia's "technical documentation" on how he built the LCC compiler and environment. Actually, it seems the docs are not online anymore, but I'm sure you can get them through his e-mail ([email protected]).

Edit: I liked your previous example better. Keep it classy, LOL :)

dguaraglia
:) I didn't want to get banned.
Vulcan Eager
+1  A: 

I had a very similar requirement and did exactly what davidg suggested; subclassed a edit control and filtered key presses. This was actually using Qt not MFC but the principle will be exactly the same.

You need to remember to filter keys such as home as well as left and backspace. I just checked to see if the move would move the caret into the prompt and if it did ignored the keypress.

Another thing to watch for is pasting multiline text, you will have to choose whether to just paste the first line or all lines, adding the prompt on all lines after the first. When subclassing the control you get lots of behaviour which won't work exactly as you want it.

David Dibben