colourpopup.cpp

来自「管理项目进度工具的原代码」· C++ 代码 · 共 959 行 · 第 1/2 页

CPP
959
字号
// ColourPopup.cpp : implementation file
//
// Written by Chris Maunder (chrismaunder@codeguru.com)
// Extended by Alexander Bischofberger (bischofb@informatik.tu-muenchen.de)
// Copyright (c) 1998.
//
// Updated 30 May 1998 to allow any number of colours, and to
//                     make the appearance closer to Office 97. 
//                     Also added "Default" text area.         (CJM)
//
//         13 June 1998 Fixed change of focus bug (CJM)
//         30 June 1998 Fixed bug caused by focus bug fix (D'oh!!)
//                      Solution suggested by Paul Wilkerson.
//
// ColourPopup is a helper class for the colour picker control
// CColourPicker. Check out the header file or the accompanying 
// HTML doc file for details.
//
// This code may be used in compiled form in any way you desire. This
// file may be redistributed unmodified by any means PROVIDING it is 
// not sold for profit without the authors written consent, and 
// providing that this notice and the authors name is included. 
//
// This file is provided "as is" with no expressed or implied warranty.
// The author accepts no liability if it causes any damage to you or your
// computer whatsoever. It's free, so don't hassle me about it.
//
// Expect bugs.
// 
// Please use and enjoy. Please let me know of any bugs/mods/improvements 
// that you have found/implemented and I will fix/incorporate them into this
// file. 

#include "stdafx.h"
#include <math.h>
#include "ColourPopup.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#ifdef _AFXDLL
#  define COMPILE_MULTIMON_STUBS
#endif

#pragma warning(disable:4706)
#include <multimon.h>
#pragma warning(default:4706)

#define DEFAULT_BOX_VALUE -3
#define CUSTOM_BOX_VALUE  -2
#define INVALID_COLOUR    -1

#define MAX_COLOURS      100


ColourTableEntry CColourPopup::m_crColours[] = 
{
    { RGB(0x00, 0x00, 0x00),    _T("Black")             },
    { RGB(0xA5, 0x2A, 0x00),    _T("Brown")             },
    { RGB(0x00, 0x40, 0x40),    _T("Dark Olive Green")  },
    { RGB(0x00, 0x55, 0x00),    _T("Dark Green")        },
    { RGB(0x00, 0x00, 0x5E),    _T("Dark Teal")         },
    { RGB(0x00, 0x00, 0x8B),    _T("Dark blue")         },
    { RGB(0x4B, 0x00, 0x82),    _T("Indigo")            },
    { RGB(0x28, 0x28, 0x28),    _T("Dark grey")         },

    { RGB(0x8B, 0x00, 0x00),    _T("Dark red")          },
    { RGB(0xFF, 0x68, 0x20),    _T("Orange")            },
    { RGB(0x8B, 0x8B, 0x00),    _T("Dark yellow")       },
    { RGB(0x00, 0x93, 0x00),    _T("Green")             },
    { RGB(0x38, 0x8E, 0x8E),    _T("Teal")              },
    { RGB(0x00, 0x00, 0xFF),    _T("Blue")              },
    { RGB(0x7B, 0x7B, 0xC0),    _T("Blue-grey")         },
    { RGB(0x66, 0x66, 0x66),    _T("Grey - 40")         },

    { RGB(0xFF, 0x00, 0x00),    _T("Red")               },
    { RGB(0xFF, 0xAD, 0x5B),    _T("Light orange")      },
    { RGB(0x32, 0xCD, 0x32),    _T("Lime")              }, 
    { RGB(0x3C, 0xB3, 0x71),    _T("Sea green")         },
    { RGB(0x7F, 0xFF, 0xD4),    _T("Aqua")              },
    { RGB(0x7D, 0x9E, 0xC0),    _T("Light blue")        },
    { RGB(0x80, 0x00, 0x80),    _T("Violet")            },
    { RGB(0x7F, 0x7F, 0x7F),    _T("Grey - 50")         },

    { RGB(0xFF, 0xC0, 0xCB),    _T("Pink")              },
    { RGB(0xFF, 0xD7, 0x00),    _T("Gold")              },
    { RGB(0xFF, 0xFF, 0x00),    _T("Yellow")            },    
    { RGB(0x00, 0xFF, 0x00),    _T("Bright green")      },
    { RGB(0x40, 0xE0, 0xD0),    _T("Turquoise")         },
    { RGB(0xC0, 0xFF, 0xFF),    _T("Skyblue")           },
    { RGB(0x48, 0x00, 0x48),    _T("Plum")              },
    { RGB(0xC0, 0xC0, 0xC0),    _T("Light grey")        },

    { RGB(0xFF, 0xE4, 0xE1),    _T("Rose")              },
    { RGB(0xD2, 0xB4, 0x8C),    _T("Tan")               },
    { RGB(0xFF, 0xFF, 0xE0),    _T("Light yellow")      },
    { RGB(0x98, 0xFB, 0x98),    _T("Pale green ")       },
    { RGB(0xAF, 0xEE, 0xEE),    _T("Pale turquoise")    },
    { RGB(0x68, 0x83, 0x8B),    _T("Pale blue")         },
    { RGB(0xE6, 0xE6, 0xFA),    _T("Lavender")          },
    { RGB(0xFF, 0xFF, 0xFF),    _T("White")             }
};

/////////////////////////////////////////////////////////////////////////////
// CColourPopup

CColourPopup::CColourPopup()
{
    Initialise();
}

CColourPopup::CColourPopup(CPoint p, COLORREF crColour, CWnd* pParentWnd, 
						   UINT nID /* = 0*/,
                           LPCTSTR szDefaultText /* = NULL */,
                           LPCTSTR szCustomText  /* = NULL */,
						   BOOL bIgnoreFirstLBtnUp/* = FALSE*/)
{
    Initialise();

    CColourPopup::Create(p, crColour, pParentWnd, nID, szDefaultText, szCustomText, bIgnoreFirstLBtnUp);
}

void CColourPopup::Initialise()
{
    m_nNumColours       = sizeof(m_crColours)/sizeof(ColourTableEntry);
    ASSERT(m_nNumColours <= MAX_COLOURS);
    if (m_nNumColours > MAX_COLOURS)
        m_nNumColours = MAX_COLOURS;

    m_nNumColumns       = 0;
    m_nNumRows          = 0;
    m_nBoxSize          = 18;
    m_nMargin           = ::GetSystemMetrics(SM_CXEDGE);
    m_nCurrentSel       = INVALID_COLOUR;
    m_nChosenColourSel  = INVALID_COLOUR;
    m_pParent           = NULL;
    m_crColour          = m_crInitialColour = RGB(0,0,0);

	m_bIgnoreFirstLBtnUp = FALSE;
    m_bChildWindowVisible = FALSE;

    // Idiot check: Make sure the colour square is at least 5 x 5;
    if (m_nBoxSize - 2*m_nMargin - 2 < 5) m_nBoxSize = 5 + 2*m_nMargin + 2;

    // Create the font
    NONCLIENTMETRICS ncm;
    ncm.cbSize = sizeof(NONCLIENTMETRICS);
    VERIFY(SystemParametersInfo(SPI_GETNONCLIENTMETRICS, sizeof(NONCLIENTMETRICS), &ncm, 0));
    m_Font.CreateFontIndirect(&(ncm.lfMessageFont));

    // Create the palette
    struct {
        LOGPALETTE    LogPalette;
        PALETTEENTRY  PalEntry[MAX_COLOURS];
    } pal;

    LOGPALETTE* pLogPalette = (LOGPALETTE*) &pal;
    pLogPalette->palVersion    = 0x300;
    pLogPalette->palNumEntries = (WORD) m_nNumColours; 

    for (int i = 0; i < m_nNumColours; i++)
    {
        pLogPalette->palPalEntry[i].peRed   = GetRValue(m_crColours[i].crColour);
        pLogPalette->palPalEntry[i].peGreen = GetGValue(m_crColours[i].crColour);
        pLogPalette->palPalEntry[i].peBlue  = GetBValue(m_crColours[i].crColour);
        pLogPalette->palPalEntry[i].peFlags = 0;
    }

    m_Palette.CreatePalette(pLogPalette);
}

CColourPopup::~CColourPopup()
{
    m_Font.DeleteObject();
    m_Palette.DeleteObject();
}

BOOL CColourPopup::Create(CPoint p, COLORREF crColour, CWnd* pParentWnd,
						  UINT nID /* = 0*/,
                          LPCTSTR szDefaultText /* = NULL */,
                          LPCTSTR szCustomText  /* = NULL */,
						  BOOL bIgnoreFirstLBtnUp/* = FALSE*/)
{
    ASSERT(pParentWnd && ::IsWindow(pParentWnd->GetSafeHwnd()));
//    ASSERT(pParentWnd->IsKindOf(RUNTIME_CLASS(CColourPicker)));

    m_pParent  = pParentWnd;
    m_crColour = m_crInitialColour = crColour;
	m_bIgnoreFirstLBtnUp = bIgnoreFirstLBtnUp;
	m_nID = (nID != 0) ? nID : pParentWnd->GetDlgCtrlID();

    // Get the class name and create the window
    CString szClassName = AfxRegisterWndClass(CS_CLASSDC|CS_SAVEBITS|CS_HREDRAW|CS_VREDRAW,
                                              0,
                                              (HBRUSH) (COLOR_BTNFACE+1), 
                                              0);

    if (!CWnd::CreateEx(0, szClassName, _T(""), /*WS_VISIBLE|*/WS_POPUP, 
                        p.x, p.y, 100, 100, // size updated soon
                        pParentWnd->GetSafeHwnd(), 0, NULL))
        return FALSE;

    // Store the Custom text
    if (szCustomText != NULL) 
        m_strCustomText = szCustomText;

    // Store the Default Area text
    if (szDefaultText != NULL) 
        m_strDefaultText = szDefaultText;
        
    // Set the window size
    SetWindowSize();

    // Create the tooltips
    CreateToolTips();

    // Find which cell (if any) corresponds to the initial colour
    FindCellFromColour(crColour);

	pParentWnd->SetFocus();
	ShowWindow(SW_SHOWNOACTIVATE);

    // Capture all mouse events for the life of this window
    SetCapture();

	// Hook the parent wnd
	return ScHookWindow(pParentWnd->GetSafeHwnd());
}

BEGIN_MESSAGE_MAP(CColourPopup, CWnd)
    //{{AFX_MSG_MAP(CColourPopup)
    ON_WM_NCDESTROY()
    ON_WM_LBUTTONUP()
    ON_WM_PAINT()
    ON_WM_MOUSEMOVE()
    ON_WM_QUERYNEWPALETTE()
    ON_WM_PALETTECHANGED()
	ON_WM_KILLFOCUS()
	ON_WM_ACTIVATEAPP()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CColourPopup message handlers

// For tooltips
BOOL CColourPopup::PreTranslateMessage(MSG* pMsg) 
{
    m_ToolTip.RelayEvent(pMsg);

    // Fix (Adrian Roman): Sometimes if the picker loses focus it is never destroyed
    if (GetCapture()->GetSafeHwnd() != m_hWnd)
        SetCapture(); 

    return CWnd::PreTranslateMessage(pMsg);
}

LRESULT CColourPopup::ScWindowProc(HWND hRealWnd, UINT msg, WPARAM wp, LPARAM /*lp*/)
{
	switch (msg)
	{
	case WM_KEYDOWN:
		{
			int row = GetRow(m_nCurrentSel),
				col = GetColumn(m_nCurrentSel);

			switch (wp)
			{
			case VK_DOWN:
				{
					if (row == DEFAULT_BOX_VALUE) 
						row = col = 0; 

					else if (row == CUSTOM_BOX_VALUE)
					{
						if (m_strDefaultText.GetLength())
							row = col = DEFAULT_BOX_VALUE;
						else
							row = col = 0;
					}
					else
					{
						row++;
						if (GetIndex(row,col) < 0)
						{
							if (m_strCustomText.GetLength())
								row = col = CUSTOM_BOX_VALUE;
							else if (m_strDefaultText.GetLength())
								row = col = DEFAULT_BOX_VALUE;
							else
								row = col = 0;
						}
					}
					ChangeSelection(GetIndex(row, col));
				}
				return 0L; // eat
				
			case VK_UP:
				{
					if (row == DEFAULT_BOX_VALUE)
					{
						if (m_strCustomText.GetLength())
							row = col = CUSTOM_BOX_VALUE;
						else
						{ 
							row = GetRow(m_nNumColours-1); 
							col = GetColumn(m_nNumColours-1); 
						}
					}
					else if (row == CUSTOM_BOX_VALUE)
					{ 
						row = GetRow(m_nNumColours-1); 
						col = GetColumn(m_nNumColours-1); 
					}
					else if (row > 0) 
						row--;
					else /* row == 0 */
					{
						if (m_strDefaultText.GetLength())
							row = col = DEFAULT_BOX_VALUE;
						else if (m_strCustomText.GetLength())
							row = col = CUSTOM_BOX_VALUE;
						else
						{ 
							row = GetRow(m_nNumColours-1); 
							col = GetColumn(m_nNumColours-1); 
						}
					}
					ChangeSelection(GetIndex(row, col));
				}
				return 0L; // eat
				
			case VK_RIGHT:
				{
					if (row == DEFAULT_BOX_VALUE) 
						row = col = 0; 
					
					else if (row == CUSTOM_BOX_VALUE)
					{
						if (m_strDefaultText.GetLength())
							row = col = DEFAULT_BOX_VALUE;
						else
							row = col = 0;
					}
					else if (col < m_nNumColumns-1) 
						col++;
					else 
					{ 
						col = 0; row++;
					}
					
					if (GetIndex(row,col) == INVALID_COLOUR)
					{
						if (m_strCustomText.GetLength())
							row = col = CUSTOM_BOX_VALUE;
						else if (m_strDefaultText.GetLength())
							row = col = DEFAULT_BOX_VALUE;
						else
							row = col = 0;
					}
					
					ChangeSelection(GetIndex(row, col));
				}
				return 0L; // eat
				
			case VK_LEFT:
				{
					if (row == DEFAULT_BOX_VALUE)
					{
						if (m_strCustomText.GetLength())
							row = col = CUSTOM_BOX_VALUE;
						else
						{ 
							row = GetRow(m_nNumColours-1); 
							col = GetColumn(m_nNumColours-1); 
						}
					}
					else if (row == CUSTOM_BOX_VALUE)
					{ 
						row = GetRow(m_nNumColours-1); 
						col = GetColumn(m_nNumColours-1); 
					}
					else if (col > 0) 
						col--;
					else /* col == 0 */
					{
						if (row > 0) { row--; col = m_nNumColumns-1; }
						else 
						{
							if (m_strDefaultText.GetLength())
								row = col = DEFAULT_BOX_VALUE;
							else if (m_strCustomText.GetLength())
								row = col = CUSTOM_BOX_VALUE;
							else
							{ 
								row = GetRow(m_nNumColours-1); 
								col = GetColumn(m_nNumColours-1); 
							}
						}
					}
					ChangeSelection(GetIndex(row, col));
				}
				return 0L; // eat
				
			case VK_ESCAPE:
				m_crColour = m_crInitialColour;
				EndSelection(CPN_SELENDCANCEL);
				return 0L; // eat
				
			case VK_RETURN:
			case VK_SPACE:
				EndSelection(CPN_SELENDOK);
				return 0L; // eat
			}
		}
		break;
	}

	return CSubclasser::ScDefault(hRealWnd);
}

// auto-deletion
void CColourPopup::OnNcDestroy() 
{
    CWnd::OnNcDestroy();
    delete this;
}

void CColourPopup::OnPaint() 
{
    CPaintDC dc(this); // device context for painting

    // Draw the Default Area text
    if (m_strDefaultText.GetLength())
        DrawCell(&dc, DEFAULT_BOX_VALUE);
 
    // Draw colour cells
    for (int i = 0; i < m_nNumColours; i++)
        DrawCell(&dc, i);
    
    // Draw custom text
    if (m_strCustomText.GetLength())
        DrawCell(&dc, CUSTOM_BOX_VALUE);

    // Draw raised window edge (ex-window style WS_EX_WINDOWEDGE is sposed to do this,
    // but for some reason isn't
    CRect rect;
    GetClientRect(rect);
    dc.DrawEdge(rect, EDGE_RAISED, BF_RECT);
}

void CColourPopup::OnMouseMove(UINT nFlags, CPoint point) 
{
    int nNewSelection = INVALID_COLOUR;

    // Translate points to be relative raised window edge
    point.x -= m_nMargin;
    point.y -= m_nMargin;

    // First check we aren't in text box
    if (m_strCustomText.GetLength() && m_CustomTextRect.PtInRect(point))
        nNewSelection = CUSTOM_BOX_VALUE;
    else if (m_strDefaultText.GetLength() && m_DefaultTextRect.PtInRect(point))
        nNewSelection = DEFAULT_BOX_VALUE;
    else
    {
        // Take into account text box
        if (m_strDefaultText.GetLength()) 
            point.y -= m_DefaultTextRect.Height();  

        // Get the row and column
        nNewSelection = GetIndex(point.y / m_nBoxSize, point.x / m_nBoxSize);

        // In range? If not, default and exit
        if (nNewSelection < 0 || nNewSelection >= m_nNumColours)
        {
            CWnd::OnMouseMove(nFlags, point);

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?