views:

1373

answers:

4

Is it possible to draw over a TextBox control in .NET Compact Framework? I want to create a watermark over it. I have read this answer. It's currently my best approach, but I do not want to limit myself to displaying the watermark only when the TextBox does not have the focus.

I'm ready to try any hack!

A: 

Unfortunately, the .NET CF TextBox can't be inherited and used as a base for this task (OnPaint doesn't get called, for starters), so I think honestly you'll save some time creating your own control.

Just to get a handle on creating custom controls in .NET CF if you haven't already, I'd highly recommend following this blog post on making controls transparent as the concept is largely the same:

http://christian-helle.blogspot.com/2008/01/transparent-controls-in-netcf.html

If I were doing this from scratch, I'd start by inheriting from a Panel and make it into a textbox. A textbox is probably one of the hardest controls to create (especially if the text can be longer than the textbox), so you might also see if other vendors have a TextBox component with source code that you could start from, that way you could just add a .DrawImage to the end of the paint method and you'd have the watermark.

routeNpingme
+1  A: 

If I were to do this, I'd create a control that derives from TextBox, that way you get all of the base rendering, events, text and selection, yada-yada. I'd then P/Invoke to SetWindowLong to change the window handler, and handle WM_PAINT in a custom handler, drawing in the watermark or whatever you want.

A good base example is the OpenNETCF.Windows.Forms.TextBox2 class that does this subclassing to handle cut/copy/paste operations. I believe that code has been there since the 1.x days, so the code is freely available (bottom of the page in the link above) if you don't have/want the latest.

ctacke
A: 

Nice to see you're still around Chris. I've been working on allot of BlackBerry and other platforms for the past year, but back on CF v3.5 .... nice to be home again.

I ran into a similar issue on the TextBox control where I wanted to handle the painting of the boarder and some additional effects, but didn't feel like writing a TextBox from the ground-up. That's allot of work, as I now know after building a fully functional one for BlackBerry. Much better to extend the existing TextBox and simply provide your own painting logic. I wish I knew why there's no exposed paint event to register for, or why the OnPaint() from the Control class never gets invoked from the TextBox .... would have made life MUCH simplier!!

Very happy I came across your reply here because it got me thinking in a different way on how to solve my problem. I have essentially replaced the default Windows Procedure function with my own C# delegate. But, in order to have the "default behavior" still execute ... painting all the text, cursors, scrollbars in the text field, etc., etc., thereby leaving me with the small task of handling my boarder and special effects (much less work then building a TextBox control from scratch) I simply invoke the "OldWindowsProc" before I execute my painting commands.

Works like a charm!!

The following links on MSDN gave me the needed pieces to make it work:

http://msdn.microsoft.com/en-us/library/ms633569%28VS.85%29.aspx Also, check out ms229681(VS.80).aspx too.

Thanks Chris for posting your reply above because this is a much better way then either building a full TextBox control, or building some lame excuse of a Textbox control all in the name of being able add a little additional painting logic.

Thanks

MarkIsMobile
A: 

Any chance you might want to post some code on how to invoke OldWindowsProc? I'm struggeling with the textbox as the next of my AlphaControls pack (Form, CheckBox, Label, Button, Panel, GroupBox) which is coming along quite nicely IMHO :)

Miros
To whom are you asking this? You might want to position this question under an existing answer, as a comment.
Martin Plante