visual-c++mfctooltipcolordialog

Is there any way for a tooltip to show the selected colour descriptively?


It is possible to configure the CMFCColorDialog to how the active colour in a tooltip when the mouse is over it?

enter image description here

Being colour blind I find it hard to identify "Red" for example. I got around it by clicking the Custom tab and using the RGB properties and setting Red that way:

enter image description here

But I wondered if a tooltip could be supported.


Solution

  • I didn't find any built-in functionality supporting tooltips here.

    But, if you are open to DIY project ( and I feel that you are :) ), you can easily subclass that dialog, overriding single PreTranslateMessage function:

    #pragma once
    #include <afxcolordialog.h>
    class CMyColorDlg :
        public CMFCColorDialog
    {
    public:
        virtual BOOL PreTranslateMessage(MSG* pMsg);
    private:
        CToolTipCtrl m_ToolTip;
        bool m_bCreate = true;
        bool m_bTrackingMouseLeave = false;
        COLORREF m_LastColor = 0;
    };
    

    Here is my demo implementation:

    #include "pch.h"
    #include "CMyColorDlg.h"
    
    BOOL CMyColorDlg::PreTranslateMessage(MSG* pMsg) {
        if (pMsg->message == WM_MOUSEMOVE) {
            if (m_bCreate) {
                m_bCreate = false;
                m_ToolTip.Create(&m_pColourSheetOne->m_hexpicker, TTS_ALWAYSTIP | TTS_NOANIMATE);
                m_ToolTip.AddTool(&m_pColourSheetOne->m_hexpicker, L"RGB");
            }
            CWnd* pw = WindowFromPoint(pMsg->pt);
            if (pw->m_hWnd == m_pColourSheetOne->m_hexpicker.m_hWnd) {
                if (!m_bTrackingMouseLeave)
                {
                    TRACKMOUSEEVENT tme = { 0 };
                    tme.cbSize = sizeof(TRACKMOUSEEVENT);
                    tme.dwFlags = TME_LEAVE;
                    tme.hwndTrack = m_hWnd;
                    ::_TrackMouseEvent(&tme);
    
                    m_ToolTip.Activate(TRUE);
                    m_bTrackingMouseLeave = TRUE;
                }
                pw->ScreenToClient(&pMsg->pt);
                COLORREF color = pw->GetDC()->GetPixel(pMsg->pt);
                if (m_LastColor != color) {
                    if (color == 0xF9F9F9) { // RGB(249,249,249) - gray around the color stuff
                        m_ToolTip.Activate(FALSE);
                    }
                    else if (m_LastColor == 0xF9F9F9) {
                        m_ToolTip.Activate(TRUE);
                    }
                    m_LastColor = color;
                    CString s;
                    s.Format(L"R=%d, G=%d, B=%d\n",
                        GetRValue(color), GetGValue(color), GetBValue(color));
                    m_ToolTip.UpdateTipText(s, &m_pColourSheetOne->m_hexpicker);
                    m_ToolTip.Popup();
    
                }
            }
        }
        else if (pMsg->message == WM_MOUSELEAVE) {
            m_bTrackingMouseLeave = FALSE;
            m_ToolTip.Activate(FALSE);
        }
    
        return CMFCColorDialog::PreTranslateMessage(pMsg);
    }
    

    You only get WM_MOUSEMOVE over that control after you click Select... button.

    UPDATE:

    With a credit to Ovidiu Cucu, added tracking tooltip control.