Implement the watermark effect (winforms) on textbox)

Source: Internet
Author: User

This document assumes that you have the development experience of the. NET winform control and Basic Windows message processing knowledge.

All users who use Vista know that the text box that inputs the user name and password at login has a watermark effect. When there is no text in the text box, the "user name" and "password" are displayed in gray, respectively ", after the input, the text will disappear.

Because a software developed on hand needs such a function, we have made some research on the watermark effect. Obviously, the watermark text is not the real text content (that is, the textbox. Text attribute), but the image drawn on the Textbox Control Surface using GDI. The implementation of the watermark is not complex. To create a class that inherits the textbox, when the textbox needs to be drawn, the textbox will first complete its own painting, determine whether to draw the watermark text based on whether there is content in the current textbox.

When do we need to draw? Anyone who has experience in control development knows that the watermark text is usually placed in the paint event of Textbox (onpaint method ).CodeBut it was quickly rejected because textbox won't trigger the painting event at all. The principle is simple. textbox is. net encapsulates the Native Windows Control edit, and all the controls are drawn by windows. net Code, unless you use the setstyle method to set userpaint to true. Userpaint can be used by. Net code to completely draw controls. Of course, the painting event can be triggered, but there are too many things to consider, which is not the main direction I want to study.

So how can we know when to draw the watermark text when the paint event cannot be used? Windows message, message, and powerful message. The control class provides the wndproc method for control developers to process messages. As long as they reload this method and determine whether it is the message wm_paint of the control, they can know when to draw the watermark.

OK. The following Code demonstrates the implementation of a simple watermark effect.

 

Code
Protected   Override   Void Wndproc ( Ref Message m)
{
// The base class processes messages first, because the watermark is drawn after the native control is drawn.
Base . Wndproc ( Ref M );

// 0x000f is the actual value of the constant wm_paint.
If (M. msg =   0x000f )
{
// Now you have caught the draw event and started to draw the watermark.

If ( This . Text. Length =   0 ) // A watermark is drawn only when no text is in the text box.
{
Brush = Systembrushes. graytext; // The gray text color indicates the disabled text color.
Font font =   This . Font; // Use the font of the current control.
Rectangle rect =   This . Clientrectangle; // Draw in the customer area of the current control.

Using (Graphics g =   This . Creategraphics ())
{
G. drawstring ( " Watermark " , Font, brush, rect );
}
}
}
}

 
In the next blog, I will explain how to create a watermark in the ComboBox control.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.