Excerpt from: http://www.jcwcn.com/html/VC/10_19_51_12.htm
The main problem of making a transparent edit control is the output of the character, and there are several times when the output is refreshed in edit, one is when the keyboard or mouse message is received, and the WM_PAINT message is received. It is not all redrawn at the time of the refresh, so it is not feasible to handle WM_PAINT messages in the inherited edit class. But the edit control always knows how to refresh, so just send a message to the control so that it can refresh itself. By using Spy + + to know the need to refresh a few times, one is the key when the content changes, the other is the choice of changes, the former edit control will receive the GetCtlCode and KeyUp messages, The latter receives GetCtlCode and capturechange messages or KeyUp messages, so RedrawWindow is called in GetCtlCode to force edit to flush the entire control. In RedrawWindow, you can make the control repaint the background by using the parameter rdw_erase, which is called ONERASEBKGND (cdc* PDC), where the background is redrawn. A special case is to hold down the left mouse button and drag the mouse back and forth, when the choice to change, receive the message is MouseMove, in order to correct the response also to process the message, but in each MouseMove refresh display of the overhead is too large, and inevitably flicker sense, So only when the left mouse button is pressed to refresh the display.
The approximate code is the following, mainly inherited a CEdit object Ctpedit, when used can be dynamically created, or the use of subclass method. I'm using the latter.
[CPP]View PlainCopyprint?
- Class Ctestdlg: Public CDialog
- {
- ......
- Declaring a CTPEDIT member variable
- Private
- Ctpedit M_tpedit;
- };
- Subclass the Edit Control in a dialog box template in OnInitDialog
- BOOL Ctestdlg::oninitdialog ()
- {
- CDialog::OnInitDialog ();
- M_tpedit. SubclassDlgItem (Idc_edit, this);
- return TRUE;
- }
- Set the transparency of the background in OnCtlColor, to change the color of the edit control font is also 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 background transparent, so that when the output character is
- is the so-called hollow word, instead of having a white background
- Pdc->settextcolor (RGB (255,0,0)); //Change the color of the font
- Return Hbrush (Getstockobject (Hollow_brush));
- }
- return HBR;
- }
- Ctpedit Object
- #ifndef _ccolor_edit_h_
- #define _ccolor_edit_h_
- 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 ()
- };
- {{Afx_insert_location}}
- #endif
- The message response function for Ctpedit is as follows
- #include "stdafx.h"
- #include "ColorEdit.h"
- Drawing background picture
- BOOL ctpedit::onerasebkgnd (cdc* PDC)
- {
- Get the edit control's outer frame, which is the background area
- RECT Updatarect;
- GetClientRect (&updatarect);
- Drawing background, I'm drawing 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 edit control to erase background, write characters
- 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 left-click and drag the mouse, the display will refresh
- void Ctpedit::onmousemove (UINT nflags, CPoint point)
- {
- if (M_mousedown)
- RedrawWindow (NULL, Null,rdw_invalidate | Rdw_erase);
- Cedit::onmousemove (nflags, point);
- }
- Initializing member variables
- Ctpedit::ctpedit ()
- {
- M_mousedown=false;
- }
http://blog.csdn.net/witch_soya/article/details/6889970
About Transparent (redrawing) of CEdit controls