我做了一個類CGreyEditControl基于CEdit來實作編輯框控制元件的自繪,但是在自繪時遇到了個問題,如圖:

編輯框中文字部分是白的,我現在想要它變成RGB(63, 63, 70)。
要求:父視窗中不做任何修改,只在CGreyEditControl中做修改實作
以下是代碼:(部分)
//父視窗CPP WM_CTLCOLOR -> OnCtlColor
HBRUSH CGreyDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialogEx::OnCtlColor(pDC, pWnd, nCtlColor);
if (pWnd->GetDlgCtrlID() != MY_EDIT)
{
pDC->SetBkColor(RGB(37, 37, 38));
pDC->SetBkMode(TRANSPARENT);
pDC->SetTextColor(RGB(255, 255, 255));
return CreateSolidBrush(RGB(37, 37, 38));
}
return hbr;
}
// GreyEditControl.h: GreyEditControl DLL 的主標頭檔案
//
#pragma once
#ifndef __AFXWIN_H__
#error "在包含此檔案之前包含“stdafx.h”以生成 PCH 檔案"
#endif
#include "resource.h" // 主符號
//我是做成MFC dll類的
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 OnMouseMove(UINT nFlags, CPoint point);
afx_msg void OnMouseLeave();
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(CDC* pDC);
afx_msg void OnMouseHover(UINT nFlags, CPoint point);
afx_msg void OnNcPaint();
void DrawBorder();
afx_msg void OnEnChange();
afx_msg HBRUSH CtlColor(CDC* /*pDC*/, UINT /*nCtlColor*/);
};
// GreyEditControl.cpp: 定義 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, &CGreyEditControl::OnEnChange)
ON_WM_CTLCOLOR_REFLECT()
END_MESSAGE_MAP()
void CGreyEditControl::OnPaint()
{
CEdit::OnPaint();
DrawBorder();
}
void CGreyEditControl::OnMouseMove(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(&tme);
}
CEdit::OnMouseMove(nFlags, point);
}
void CGreyEditControl::OnMouseLeave()
{
m_bTracking = FALSE;
m_bHover = FALSE;
DrawBorder();
CEdit::OnMouseLeave();
}
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->rgrc[0].top += 3;
lpncsp->rgrc[0].bottom -= 3;
lpncsp->rgrc[0].left += 1;
lpncsp->rgrc[0].right -= 1;
CEdit::OnNcCalcSize(bCalcValidRects, lpncsp);
}
BOOL CGreyEditControl::OnEraseBkgnd(CDC* pDC)
{
// return 0;
return CEdit::OnEraseBkgnd(pDC);
}
void CGreyEditControl::OnMouseHover(UINT nFlags, CPoint point)
{
m_bHover = TRUE;
DrawBorder();
CEdit::OnMouseHover(nFlags, point);
}
void CGreyEditControl::OnNcPaint()
{
CEdit::OnNcPaint();
DrawBorder();
}
void CGreyEditControl::DrawBorder()
{
HDC hDC = ::GetWindowDC(m_hWnd);
CRect rc;
::GetWindowRect(m_hWnd, &rc);
rc = CRect(0, 0, rc.right - rc.left, rc.bottom - rc.top);
if (m_bHover) {
::FrameRect(hDC, &rc, CBrush(RGB(0, 151, 251)));
}
else if (m_bFocus) {
::FrameRect(hDC, &rc, CBrush(RGB(0, 151, 251)));
}
else {
::FrameRect(hDC, &rc, CBrush(RGB(0, 122, 204)));
}
if (m_nPaint < 2)
{
rc.InflateRect(-1, -1);
if (GetStyle()&ES_READONLY)
FillRect(hDC, rc, CBrush(RGB(85, 85, 85)));
else
FillRect(hDC, rc, CBrush(RGB(63, 63, 70)));
++m_nPaint;
}
::ReleaseDC(m_hWnd, hDC);
}
void CGreyEditControl::OnEnChange()
{
DrawBorder();
}
HBRUSH CGreyEditControl::CtlColor(CDC* pDC, UINT /*nCtlColor*/)
{
pDC->SetTextColor(RGB(255, 0, 0));
if (GetStyle()&ES_READONLY)
pDC->SetTextColor(RGB(85, 85, 85));
else
pDC->SetTextColor(RGB(63, 63, 70));
if (GetStyle()&ES_READONLY)
return CreateSolidBrush(RGB(85, 85, 85));
else
return CreateSolidBrush(RGB(63, 63, 70));
}
uj5u.com熱心網友回復:
補充下,CtlColor(...)除錯時有呼叫過,但是對結果沒有作用uj5u.com熱心網友回復:
CWnd::OnCtlColorafx_msg HBRUSH OnCtlColor( CDC* pDC, CWnd* pWnd, UINT nCtlColor );
Remarks
The framework calls this member function when a child control is about to be drawn. Most controls send this message to their parent (usually a dialog box) to prepare the pDC for drawing the control using the correct colors.
uj5u.com熱心網友回復:
因為是反射訊息HBRUSH CMyEdit::CtlColor(CDC* pDC, UINT nCtlColor)
{
// TODO: Change any attributes of the DC here
// TODO: Return a non-NULL brush if the parent's handler should not be called return NULL;
}
uj5u.com熱心網友回復:
所以 反射 CtlColor , 和 OnCtlColor 父視窗 不能 同時 有。uj5u.com熱心網友回復:
懂了,就是你父視窗OnCtlColor了,子視窗再CtlColor就是沒用了吧。但是有沒有解決方法?不洗掉父視窗的OnCtlColor
uj5u.com熱心網友回復:
把 功能 都 放 CtlColor 里// TODO: Return a non-NULL brush if the parent's handler should not be called return NULL;
OnCtlColor 不會被呼叫
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/65444.html
標籤:基礎類
下一篇:mfc編譯出問題
