About transparency of CEdit controls

Source: Internet
Author: User
Tags bool

A few days ago and the wind here to discuss the transparency of the CEdit control. The main goal is to do a graphics background edit control, after a while, finally made a decent edit control.

The main problem with making a transparent edit control is the output of the characters, and there are several times when the output is refreshed in edit, either when a keyboard or mouse message is received, or when a WM_PAINT message is received. The refresh is not all repaint, so it is not feasible to handle WM_PAINT messages in the inherited edit class. But the edit control always knows how to refresh itself, so just send a message to the control and let it refresh itself. By using Spy + + know need to refresh a few times, one is the button, the content changes, the other is the choice of change, the former edit control will receive GetCtlCode and KeyUp messages, The latter receives GetCtlCode and capturechange messages or KeyUp messages, so call RedrawWindow in GetCtlCode to force edit to refresh the entire control. Using parameter Rdw_erase in RedrawWindow enables the control to repaint the background, that is, the OnEraseBkgnd (cdc* PDC) is invoked, and the background is repaint in the function. The more special situation is to hold down the left mouse button and drag the mouse back and forth when you choose to change, the message received is MouseMove, in order to respond to the correct response also to process the message, but in each MouseMove refresh the display is too expensive, and inevitably have a flicker, Therefore, the display is refreshed only when the left mouse button is pressed.

The approximate code is as follows, mainly inherits a CEdit object Ctpedit, when uses can create dynamically, or adopts the subclass method. I use the latter.

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 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 the background transparent so that when the output character is
is called the Hollow word, instead of having a white undertone
Pdc->settextcolor (RGB (255,0,0)); Change the color of a font
Return Hbrush (Getstockobject (Hollow_brush));
}
return HBR;
}
Ctpedit objects
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 ();
/

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.