About the transparency of the CEDIT control

xiaoxiao2021-03-06  70

A few days ago and the wind here discussed the transparent problem of CEDIT controls. The main purpose is to be an edit control with a graphic background. After some nuts, it has finally made an still considered an edit control.

The main problem with a transparent EDIT control is the output of characters, and there are several timers in the Edit output, one is when the keyboard or mouse message is received, and there is also a WM_PAINT message. When refreshing is not all heavy, it is not possible to handle the WM_PAINT message in the inherited EDIT class. But the Edit control always knows how to refresh, so just send a message to the control, let him come to refresh. By using SPY , you need to refresh if there are several timings. When one is button, the content changes, the other is when selecting changes, the former EDIT control will receive getctlcode and keyup messages, the latter will receive GetctLcode and CaptureChange Messages Or Keyup message, therefore call RedRawWindow in getctlcode to force the Edit to refresh the entire control. In RedRawwindow, by using parameter RDW_ERASE, the control can be retrangry the background, that is, calling the OneRaseBkGnd (CDC * PDC), re-draws the background in this function. The more special case is to hold the left mouse button and drag the mouse. At this time, choose to change, the received message is mousemove, in order to properly respond, to process the message, but refresh the display in each mousemove Too big, and inevitably there is a flashing sense, so it is only refreshed when the left mouse button is pressed.

The probably code is as follows, mainly inheriting a CEDIT's object CTpedit, can be created dynamically, or use Subclass method. I use the latter.

Class Ctestdlg: Public CDIALOG

{

......

// Declare a CTPEDIT member variable

Private:

CTPEDIT M_TPEDIT;

}

// EDIT control in the Subclass dialog template in OnInitDialog

Bool ctestdlg :: oninitdialog ()

{

CDIALOG :: OnInitdialog ();

m_tpedit.subclassdlgitem (idc_edit, this);

Return True;

}

// Set the transparency of the background in ONCTLCOLOR, 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); // Setting the background transparent, so, when the character is output

// is the so-called hollow word, not a white background

PDC-> SetTextColor (RGB (255, 0)); // Change the color of the font

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 ()

}

// ctpedit message response function is as follows

// painted background diagram

Bool ctpedit :: OneraseBkGnd (CDC * PDC)

{

// Get the outer frame of the EDIT control, that is, the background area

Rect updata;

GetClientRect (& Updatarect);

// Draw background, I painted a yellow rectangle

CBRUSH newbrush;

Newbrush.createsolidbrush (RGB (255, 255, 200));

CBRUSH * OLDBRUSH = PDC-> SelectObject (& newbrush);

PDC-> Rectangle (& UpDataRect);

PDC-> SELECTOBJECT (OLDBRUSH);

Return True;

}

/ / Forced Edit control 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 the left button is pressed and drag the mouse to 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 member variables

Ctpedit :: ctpedit ()

{

m_mousedown = false;

}

转载请注明原文地址:https://www.9cbs.com/read-92307.html

New Post(0)