none
如何修改WTL程序中rebar中的编辑框的文本颜色 RRS feed

  • 问题

  • 开发一个WTL的单文档程序,在rebar动态创建一个编辑框,编辑框类代码如下:

    编辑框类头文件:

    #ifndef __BAS_EDIT
    #define __BAS_EDIT
    
    
    #include <atlctrls.h>
    #include <string>
    #include <memory>
    #include <functional>
    #include <atlcrack.h>
    
    using namespace std;
    
    class BASEdit : public CWindowImpl<BASEdit, CEdit>
    {
    public:
    
    	BASEdit();
    
    	~BASEdit();
    
        BEGIN_MSG_MAP_EX(BASEdit)
    		MSG_OCM_CTLCOLOREDIT(OnReflectedCtlColorEdit)
    		MSG_WM_ERASEBKGND(OnEraseBkgnd)
            DEFAULT_REFLECTION_HANDLER()
        END_MSG_MAP()
     
    	void setBkgColor(COLORREF color);
    
    	void setTextColor(COLORREF color);
    
    protected:
    
    	HBRUSH OnReflectedCtlColorEdit(CDCHandle dc, HWND edit);
    
    	BOOL OnEraseBkgnd(CDCHandle dc);
    
    private:
    
    	HBRUSH brushBkg_;
    	COLORREF colorBkg_;
    	COLORREF colorFont_;
    };
    
    #endif


    编辑框类cpp文件:

    #include "stdafx.h"
    #include "BaseEdit.h"
    
    
    BASEdit::BASEdit()
    {
    	colorBkg_ = GetSysColor(COLOR_WINDOW);
    	colorFont_ = GetSysColor(COLOR_WINDOWTEXT);;
    	brushBkg_ = ::CreateSolidBrush(colorBkg_);
    }
    
    BASEdit::~BASEdit()
    {
    	::DeleteObject(brushBkg_);
    }
    
    void BASEdit::setBkgColor(COLORREF color)
    {
    	colorBkg_ = color;
    	::DeleteObject(brushBkg_);
    	brushBkg_ = ::CreateSolidBrush(colorBkg_);
    }
    
    void BASEdit::setTextColor(COLORREF color)
    {
    	colorFont_ = color;
    }
    
    
    BOOL BASEdit::OnEraseBkgnd(CDCHandle dc)
    {
    	return TRUE;
    }
    
    HBRUSH BASEdit::OnReflectedCtlColorEdit(CDCHandle dc, HWND edit)
    {
    	::SetBkColor(dc,colorBkg_);
    	::SetTextColor(dc,colorFont_);
    	return brushBkg_;
    }


    在框架类创建编辑框的代码如下:

    class CMainFrame : public CFrameWindowImpl<CMainFrame>, 
    			public CUpdateUI<CMainFrame>,
    			public CMessageFilter, 
    			public CIdleHandler
    {
    private:
    
         	BASEdit m_wndEdit;
    
    	HWND CreateEditCtrl(HWND hWndParent);
    
    	LRESULT OnCreate(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/);
    
    }
    
    // 创建编辑框函数
    HWND CMainFrame::CreateEditCtrl(HWND hWndParent)
    {
    	RECT rc = { 50, 0, 300, 20 };
    	m_wndEdit.Create(hWndParent, rc, NULL, WS_CHILD | WS_VISIBLE | WS_BORDER | WS_CLIPSIBLINGS | WS_CLIPCHILDREN);
    	m_wndEdit.SetCueBannerText(L"Date: 2021-04-28", true);
    	m_wndEdit.setBkgColor(RGB(0, 0, 255));
    	m_wndEdit.setTextColor(RGB(255, 0, 0));
    
    	return m_wndEdit;
    }
    
    LRESULT CMainFrame::OnCreate(UINT, WPARAM, LPARAM, BOOL&)
    {
    	
    	// create command bar window
    	RECT rcCmdBar = { 0, 0, 100, 100 };
    	HWND hWndCmdBar = m_wndCmdBar.Create(m_hWnd, rcCmdBar, NULL, ATL_SIMPLE_CMDBAR_PANE_STYLE);
    	// atach menu
    	m_wndCmdBar.AttachMenu(GetMenu());
    	// load command bar images
    	m_wndCmdBar.LoadImages(IDR_MAINFRAME);
    	// remove old menu
    	SetMenu(NULL);
    
    	HWND hWndToolBar = CreateSimpleToolBarCtrl(m_hWnd, IDR_MAINFRAME, FALSE, ATL_SIMPLE_TOOLBAR_PANE_STYLE);
    
            // 创建编辑框
    	HWND hWndAddressBar = CreateEditCtrl(m_hWnd);
    
    	CreateSimpleStatusBar();
    
    
    	CreateSimpleReBar(ATL_SIMPLE_REBAR_NOBORDER_STYLE);
    
    	AddSimpleReBarBand(hWndCmdBar);
    	AddSimpleReBarBand(hWndToolBar, NULL, TRUE);
            //将编辑框加入rebar
    	AddSimpleReBarBand(hWndAddressBar, _T("Time"), TRUE);
    
    	UpdateLayout();
    
    	CMessageLoop* pLoop = _Module.GetMessageLoop();
    	pLoop->AddMessageFilter(this);
    	pLoop->AddIdleHandler(this);
    
    	return 0;
    }


    效果图如下:

    可以看到文本颜色并没有变化。经过调试,发现编辑框的OCM_CTLCOLOREDIT消息并没有响应。已经在框架类的消息映射宏加上了REFLECT_NOTIFICATIONS()

    BEGIN_MSG_MAP(CMainFrame)
    
    	REFLECT_NOTIFICATIONS()
    END_MSG_MAP()

        如何改变rebar中的编辑框的文本颜色呢?


    前无古人,后无来者



    • 已编辑 clever101 2022年5月3日 13:51 补全图片
    2022年5月3日 12:48

全部回复

  • 您好,

    尝试发送 RB_SETTEXTCOLOR

    Best Regards,
    Minxin Yu

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2022年5月4日 6:28
  • 您好,

    尝试发送 RB_SETTEXTCOLOR

    Best Regards,
    Minxin Yu

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

          试了,不行。改用子类化的方法,代码如下:

    // 创建rebar时添加

    WNDPROC g_defRebarProc; g_defRebarProc = (WNDPROC)::SetWindowLongPtr(m_hWndToolBar, GWLP_WNDPROC, (LONG_PTR)MyRebarProc);

    LRESULT CALLBACK MyRebarProc(HWND hWnd, UINT message, WPARAM wp, LPARAM lp) { if (WM_CTLCOLOREDIT== message) { HDC hDC = (HDC)wp; SetTextColor(hDC, RGB(255,0,0)); SetBkMode(hDC, TRANSPARENT); return (LRESULT)GetStockObject(WHITE_BRUSH); } return CallWindowProc(g_defRebarProc, hWnd, message, wp, lp); }


    另外

    m_wndEdit.SetCueBannerText(L"Date: 2021-04-28", true);

    改为

    m_wndEdit.SetWindowText(L"Date: 2021-04-28");


    前无古人,后无来者

    2022年5月4日 10:52