有没有办法让工具提示以描述性方式显示所选的颜色?

huangapple go评论81阅读模式
英文:

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

问题

可以配置CMFCColorDialog,以在鼠标悬停在其上时显示活动颜色的工具提示吗?

英文:

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

有没有办法让工具提示以描述性方式显示所选的颜色?

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:

有没有办法让工具提示以描述性方式显示所选的颜色?

But I wondered if a tooltip could be supported.

答案1

得分: 1

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 <kbd>Select...</kbd> button.

英文:

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 &lt;afxcolordialog.h&gt;
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 &quot;pch.h&quot;
#include &quot;CMyColorDlg.h&quot;

BOOL CMyColorDlg::PreTranslateMessage(MSG* pMsg) {
	if (pMsg-&gt;message == WM_MOUSEMOVE) {
		if (m_bCreate) {
			m_bCreate = false;
			m_ToolTip.Create(&amp;m_pColourSheetOne-&gt;m_hexpicker, TTS_ALWAYSTIP | TTS_NOANIMATE);
			m_ToolTip.AddTool(&amp;m_pColourSheetOne-&gt;m_hexpicker, L&quot;RGB&quot;);
		}
		CWnd* pw = WindowFromPoint(pMsg-&gt;pt);
		if (pw-&gt;m_hWnd == m_pColourSheetOne-&gt;m_hexpicker.m_hWnd) {
			if (!m_bTrackingMouseLeave)
			{
				TRACKMOUSEEVENT tme = { 0 };
				tme.cbSize = sizeof(TRACKMOUSEEVENT);
				tme.dwFlags = TME_LEAVE;
				tme.hwndTrack = m_hWnd;
				::_TrackMouseEvent(&amp;tme);

				m_ToolTip.Activate(TRUE);
				m_bTrackingMouseLeave = TRUE;
			}
			pw-&gt;ScreenToClient(&amp;pMsg-&gt;pt);
			COLORREF color = pw-&gt;GetDC()-&gt;GetPixel(pMsg-&gt;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&quot;R=%d, G=%d, B=%d\n&quot;,
					GetRValue(color), GetGValue(color), GetBValue(color));
				m_ToolTip.UpdateTipText(s, &amp;m_pColourSheetOne-&gt;m_hexpicker);
				m_ToolTip.Popup();

			}
		}
	}
	else if (pMsg-&gt;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 <kbd>Select...</kbd> button.

UPDATE:

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

huangapple
  • 本文由 发表于 2023年3月3日 22:10:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/75628148.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定