Transparency of the cedit Control

Source: Internet
Author: User
A few days ago, Feng discussed the transparency of the cedit control. The main purpose is to create an edit control with a graphic background. After some effort, we finally made a fairly decent edit control.
The main problem with making a transparent edit control is the output of characters. There are several times to refresh the output in edit. One is when the keyboard or mouse message is received, in addition, wm_paint messages are received. During refresh, it is not all re-painted, so it is impossible to process the wm_paint message in the inherited edit class. However, the Edit Control always knows how to refresh it. Therefore, you only need to send a message to the control and let it refresh it by yourself. Through the use of spy ++, it is known that there are several times to refresh. One is when the key is pressed, the content is changed, and the other is when the change is selected, the former edit control will receive the getctlcode and keyup messages, the latter will receive getctlcode and capturechange messages or keyup messages, so the redrawwindow is called in getctlcode to force Edit to refresh the entire control. In redrawwindow, you can use the rdw_erase parameter to re-draw the background of the control, that is, calling onerasebkgnd (CDC * PDC) to re-draw the background in this function. The special case is that when you press and hold the left mouse button and drag the mouse back and forth, select to change. The received message is mousemove, and the message must be processed in order to make a positive response, however, the overhead of refreshing the display in every mousemove operation is too large and flickering, so the display is refreshed only when the left Key of the mouse is pressed.
The approximate code is as follows, mainly inheriting the ctpedit object of cedit, which can be dynamically created or subclass. I use the latter.
Class ctestdlg: Public cdialog
{
......
// Declare a ctpedit member variable
PRIVATE:
Ctpedit m_tpedit;
};

// Edit Control in the subclass dialog box template in oninitdialog
Bool ctestdlg: oninitdialog ()
{
Cdialog: oninitdialog ();
M_tpedit.subclassdlgitem (idc_edit, this );
Return true;
}

// Set the background transparency in onctlcolor. You need to change the font color of the edit control here.

Hbrush ctestdlg: onctlcolor (CDC * PDC, cwnd * pwnd, uint nctlcolor)
{
Hbrush HBr = cdialog: onctlcolor (PDC, pwnd, nctlcolor );

If (nctlcolor = ctlcolor_edit) & (pwnd-> getdlgctrlid () = idc_edit ))
{
PDC-> setbkmode (transparent); // set the background to transparent. In this way, when the characters are output
// It is a so-called hollow word, rather than a white background
PDC-> settextcolor (RGB (, 0, 0); // you can change the font color.
Return hbrush (getstockobject (hollow_brush ));
}

Return HBr;
}

// Ctpedit object

Class ctpedit: Public cedit
{
Public:
// M_mousedown is used to record whether the left mouse button is pressed
Bool m_mousedown;
Protected:
// Respond to the following message
// {Afx_msg (ctpedit)
Afx_msg bool onerasebkgnd (CDC * PDC );
Afx_msg void onmousemove (uint nflags, cpoint point );
Afx_msg void onlbuttondown (uint nflags, cpoint point );
Afx_msg void onlbuttonup (uint nflags, cpoint point );
Afx_msg uint ongetdlgcode ();
//} Afx_msg
Declare_message_map ()
};

// The message response function of ctpedit is as follows:
// Draw the background image
Bool ctpedit: onerasebkgnd (CDC * PDC)
{
// Obtain the outer box of the edit control, that is, the background area.
Rect updatarect;
Getclientrect (& updatarect );
// Draw the background. I drew a yellow rectangle.
Cbrush newbrush;
Newbrush. createsolidbrush (RGB (255,255,200 ));
Cbrush * oldbrush = PDC-> SelectObject (& newbrush );
PDC-> rectangle (& updatarect );
PDC-> SelectObject (oldbrush );
Return true;
}

// Force the Edit Control to erase the background and rewrite the character
Uint ctpedit: ongetdlgcode ()
{Redrawwindow (null, null, rdw_invalidate | rdw_erase );
Return cedit: ongetdlgcode ();
}
// Record whether the left mouse button is pressed
Void ctpedit: onlbuttondown (uint nflags, cpoint point)
{
M_mousedown = true;
Setcapture ();
Cedit: onlbuttondown (nflags, point );
}

Void ctpedit: onlbuttonup (uint nflags, cpoint point)
{
If (m_mousedown)
Releasecapture ();
M_mousedown = false;
Cedit: onlbuttonup (nflags, point );
}

// If you press the left button and drag the mouse, refresh the display.
Void ctpedit: onmousemove (uint nflags, cpoint point)
{
If (m_mousedown)
Redrawwindow (null, null, rdw_invalidate | rdw_erase );
Cedit: onmousemove (nflags, point );
}
// Initialize the member variable
Ctpedit: ctpedit ()
{
M_mousedown = false;
}

Related Article

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.