New and old MFC version implementation of CEdit transparent 2 methods of instance Code _c language

Source: Internet
Author: User
Tags textout

The MFC 4.2 (Visual Studio 6) is easy to implement by simply processing the WM_CTLCOLOR message under the dialog box class, and then the following code:

Copy Code code as follows:

Hbrush Calphaeditboxdlg::onctlcolor (cdc* PDC, cwnd* pwnd, UINT nCtlColor)
{
Hbrush HBR = Cdialog::onctlcolor (PDC, pwnd, nCtlColor);

Todo:change any attributes to the DC here
Pdc->setbkmode (Transparent);
Hbr= (Hbrush) getstockobject (Hollow_brush);
Todo:return a different brush if the default is not desired
return HBR;
}

Then invoke the invalidate in the related event of the edit control.

Copy Code code as follows:

void Calphaeditboxdlg::onkillfocuseditkey ()
{
Todo:add your control notification handler code here
Invalidate ();
}

void Calphaeditboxdlg::onkillfocuseditmessage ()
{
Todo:add your control notification handler code here
Invalidate ();
}

void Calphaeditboxdlg::onkillfocuseditpath ()
{
Todo:add your control notification handler code here
Invalidate ();
}

Don't forget to redraw the background if you delete the characters. Here's a list of only a few.

The new version of MFC is quite troublesome, because the background set to Clr_none or brush set as Hollow_brush, Microsoft will default to black background, this, Microsoft is really backwards. Less nonsense, edit control subclass inevitable, must deal with WM_PAINT, WM_CHAR, Wm_lbuttondown, wm_lbuttonup these several messages. If you want to get rid of the edit-controlled border, you have to process the wm_ncpaint message, but none of the code here is written in order to avoid performing the default Cdialogex::onncpaint () method to draw a border. The following code to achieve basic transparency effect, normal input no problem, if you want to implement delete, check and uncheck features, please append processing wm_lbuttondown, Wm_lbuttonup message.

Copy Code code as follows:

//////////////////////////////////////////////////////////////////////////
Draws a window.
//////////////////////////////////////////////////////////////////////////
void Cmyedit::onpaint ()
{
Paintstruct PS;
Textmetric TM;
int nselstart=0,nselend=0,ndrawstart=0,ndrawlen=0,ntxtlen=0;
RECT R;
CBitmap B;
LPTSTR sz= (LPTSTR) calloc (1024,sizeof (TCHAR));
cpaintdc* d2= (cpaintdc*) BeginPaint (&PS);
CDC D1;
CFont F;
cwnd* p=getparent ();
Ntxtlen=getwindowtext (sz,1024);
B.loadbitmap (IDB_BITMAP1);
D1. CreateCompatibleDC (P->GETDC ());
GetWindowRect (&R);
P->screentoclient (&R);
D1. SelectObject (b);
D2->bitblt (0,0,r.right-r.left,r.bottom-r.top,&d1,r.left,r.top,srccopy);
F.createfontindirect (&M_LF);
D2->selectobject (f);
D2->setbkmode (Transparent);
D2->gettextmetrics (&TM);
GetSel (Nselstart,nselend);
if (r.right-r.left<ntxtlen*tm.tmavecharwidth)
{
Ndrawstart=0-tm.tmavecharwidth*nselstart;
Ndrawlen= (r.right-r.left)/tm.tmavecharwidth;
}
Else
{
ndrawstart=0;
Ndrawlen=ntxtlen;
}
D2->textout (Ndrawstart,3,sz,ndrawlen);
D2->selectobject (Getstockobject (Null_brush));
D2->selectobject (CreatePen (Ps_dot,1,rgb (255,0,0)));
D2->rectangle (0,0,r.right-r.left,r.bottom-r.top);
Point pt;
Pt=getcaretpos ();
Pt.x=ndrawlen*tm.tmavecharwidth;
Setcaretpos (PT);
Delete sz;
EndPaint (&AMP;PS);
}

//////////////////////////////////////////////////////////////////////////
The Sticky keys and function keys are not dealt with in 2 cases.
//////////////////////////////////////////////////////////////////////////
void Cmyedit::onchar (UINT nChar, UINT nrepcnt, UINT nflags)
{
Textmetric TM;
int nselstart=0,nselend=0,ndrawstart=0,ndrawlen=0,ntxtlen=0;
RECT R;
CBitmap B;
LPTSTR sz= (LPTSTR) calloc (1024,sizeof (TCHAR));
LPTSTR input= (LPTSTR) calloc (1024,sizeof (TCHAR));
CClientDC D2 (this);
CDC D1;
CFont F;
cwnd* p=getparent ();
Ntxtlen=getwindowtext (sz,1024);
wsprintf (input,l "%c", NChar);
Lstrcat (Sz,input);
SetWindowText (SZ);
B.loadbitmap (IDB_BITMAP1);
D1. CreateCompatibleDC (P-&GT;GETDC ());
GetWindowRect (&AMP;R);
P->screentoclient (&AMP;R);
D1. SelectObject (b);
D2. BitBlt (0,0,r.right-r.left,r.bottom-r.top,&d1,r.left,r.top,srccopy);
F.createfontindirect (&AMP;M_LF);
D2. SelectObject (f);
D2. SetBkMode (Transparent);
D2. GetTextMetrics (&AMP;TM);
GetSel (Nselstart,nselend);
if (r.right-r.left<ntxtlen*tm.tmavecharwidth)
{
Ndrawstart=0-tm.tmavecharwidth*nselstart;
Ndrawlen= (r.right-r.left)/tm.tmavecharwidth;
}
Else
{
ndrawstart=0;
Ndrawlen=ntxtlen;
}
D2. TextOut (Ndrawstart,3,sz,ndrawlen);
D2. SelectObject (Getstockobject (Null_brush));
D2. SelectObject (CreatePen (Ps_dot,1,rgb (255,0,0)));
D2. Rectangle (0,0,r.right-r.left,r.bottom-r.top);
Point pt;
Pt=getcaretpos ();
Pt.x=ndrawlen*tm.tmavecharwidth;
Setcaretpos (PT);
Delete sz;
Delete input;
Cedit::onchar (NChar, nrepcnt, nflags);
}

That's all for now. How to implement a feature that is not implemented in annotations. I'm a rookie, please don't be laughed at. I hope you can show me a lot of advice.

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.