R – Drawing over a TextBox in .NET Compact Framework


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!

Best Solution

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.