问题
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!
回答1:
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.
回答2:
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.
来源:https://stackoverflow.com/questions/524784/drawing-over-a-textbox-in-net-compact-framework