Home > Software engineering >  MFC CEdit custom painting encountered problems, for help
MFC CEdit custom painting encountered problems, for help

Time:09-22

I made a class CGreyEditControl implemented based on CEdit edit box control since the draw, but encountered a problem in custom painting, as shown in figure:

Text in the edit box part is white, I now want it into RGB (63, 63, 70),
request: don't make any changes in the parent window, only in CGreyEditControl modifications to achieve

Below is the code: (part)
//parent window CPP WM_CTLCOLOR - & gt; OnCtlColor 
HBRUSH CGreyDlg: : OnCtlColor (CDC * pDC, CWnd * pWnd, UINT nCtlColor)
{
HBRUSH gets=CDialogEx: : OnCtlColor (pDC, pWnd, nCtlColor);

If (pWnd - & gt; GetDlgCtrlID ()!=MY_EDIT)
{
PDC - & gt; SetBkColor (RGB (37, 37, 38));
PDC - & gt; SetBkMode (TRANSPARENT);
PDC - & gt; SetTextColor (RGB (255, 255, 255));
Return CreateSolidBrush (RGB (37, 37, 38));
}

Return gets;
}

//GreyEditControl. H: GreyEditControl DLL main header file 
//

# pragma once

# # ifndef __AFXWIN_H__
# error "before include this file contains the" stdafx. H "to generate the PCH file
"# endif

# include "resource. H//the main symbols"

//I'm made of MFC DLL
The class AFX_EXT_CLASS CGreyEditControl: public CEdit
{
Private:
Int m_nPaint;
BOOL m_bHover;
BOOL m_bFocus;
BOOL m_bTracking;

Public:
CGreyEditControl ();
Virtual ~ CGreyEditControl ();
DECLARE_MESSAGE_MAP ()
Afx_msg void OnPaint ();
Afx_msg void onm ouseMove (UINT nFlags, CPoint point);
Afx_msg void onm ouseLeave ();
Afx_msg void OnSetFocus (CWnd * pOldWnd);
Afx_msg void OnKillFocus (CWnd * pNewWnd);
Afx_msg void OnNcCalcSize (BOOL bCalcValidRects, NCCALCSIZE_PARAMS * LPNCSP);
Afx_msg BOOL OnEraseBkgnd * pDC (CDC);
Afx_msg void onm ouseHover (UINT nFlags, CPoint point);
Afx_msg void OnNcPaint ();
Void DrawBorder ();
Afx_msg void OnEnChange ();
PDC afx_msg HBRUSH CtlColor (CDC */* */, UINT nCtlColor/* */);
};

//GreyEditControl. CPP: defines the initialization routine of DLL, 
//

# include "stdafx. H"
# include "GreyEditControl. H"

# ifdef _DEBUG
# define new DEBUG_NEW
# endif

CGreyEditControl: : CGreyEditControl ()
{
M_bHover=m_bFocus=m_bTracking=FALSE;
M_nPaint=0;
}

CGreyEditControl: : ~ CGreyEditControl ()
{

}
BEGIN_MESSAGE_MAP (CGreyEditControl CEdit)
ON_WM_PAINT ()
ON_WM_MOUSEMOVE ()
ON_WM_MOUSELEAVE ()
ON_WM_SETFOCUS ()
ON_WM_KILLFOCUS ()
ON_WM_NCCALCSIZE ()
ON_WM_ERASEBKGND ()
ON_WM_MOUSEHOVER ()
ON_WM_NCPAINT ()
ON_CONTROL_REFLECT (EN_CHANGE, & amp; CGreyEditControl: : OnEnChange)
ON_WM_CTLCOLOR_REFLECT ()
END_MESSAGE_MAP ()


Void CGreyEditControl: : OnPaint ()
{
CEdit: : OnPaint ();
DrawBorder ();
}


Void CGreyEditControl: : onm ouseMove (UINT nFlags, CPoint point)
{
if (! M_bTracking)
{
TRACKMOUSEEVENT tme.
Tme. CbSize=sizeof (tme);
Tme. HwndTrack=m_hWnd;
Tme. DwFlags=TME_LEAVE | TME_HOVER;
Tme. DwHoverTime=50;
M_bTracking=(bool) _TrackMouseEvent (& amp; Tme);
}

CEdit: onm ouseMove (nFlags, point);
}


Void CGreyEditControl: : onm ouseLeave ()
{
M_bTracking=FALSE;
M_bHover=FALSE;
DrawBorder ();

CEdit: : onm ouseLeave ();
}


Void CGreyEditControl: : OnSetFocus (CWnd * pOldWnd)
{
CEdit: : OnSetFocus (pOldWnd);

M_bFocus=TRUE;
DrawBorder ();
}


Void CGreyEditControl: : OnKillFocus (CWnd * pNewWnd)
{
CEdit: : OnKillFocus (pNewWnd);

M_bFocus=FALSE;
DrawBorder ();
}


Void CGreyEditControl: : OnNcCalcSize (BOOL bCalcValidRects, NCCALCSIZE_PARAMS * LPNCSP)
{
LPNCSP - & gt; RGRC [0]. Top +=3;
LPNCSP - & gt; RGRC [0]. Bottom -=3;
LPNCSP - & gt; RGRC [0]. Left +=1;
LPNCSP - & gt; RGRC [0]. Right -=1;

CEdit: : OnNcCalcSize (bCalcValidRects LPNCSP);
}


BOOL CGreyEditControl: : OnEraseBkgnd (pDC) CDC *
{
//return 0;
Return CEdit: : OnEraseBkgnd (pDC);
}


Void CGreyEditControl: : onm ouseHover (UINT nFlags, CPoint point)
{
M_bHover=TRUE;
DrawBorder ();

CEdit: : onm ouseHover (nFlags, point);
}


Void CGreyEditControl: : OnNcPaint ()
{
CEdit: : OnNcPaint ();
DrawBorder ();
}


Void CGreyEditControl: : DrawBorder ()
{

HDC HDC=: : GetWindowDC (m_hWnd);
CRect rc;
: : GetWindowRect (m_hWnd, & amp; Rc);
Rc=CRect (0, 0, rc. Right - rc. Left, rc. The bottom - rc. Top);
If (m_bHover) {
: : FrameRect (hDC, & amp; Rc, CBrush (RGB (0, 151, 251)));
}
Else if (m_bFocus) {
: : FrameRect (hDC, & amp; Rc, CBrush (RGB (0, 151, 251)));
}
The else {
: : FrameRect (hDC, & amp; Rc, CBrush (RGB (0, 122, 204)));
}
If (m_nPaint & lt; 2)
{
Rc. InflateRect (1, 1);
If (GetStyle () & amp; ES_READONLY)
FillRect (hDC, rc, CBrush (RGB (85, 85, 85)));
The else
FillRect (hDC, rc, CBrush (RGB (63, 63, 70)));
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related