⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 colorbtn.cpp

📁 HEX编辑器
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// ColorBtn.cpp : implementation file

#include "stdafx.h"
#include "ColorBtn.h"

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

// The color table, initialized to windows' 20 static colors

COLORREF CColorBtnDlg::colors[20] =
{
    RGB(0,0,0),
    RGB(128,0,0),
    RGB(0,128,0),
    RGB(128,128,0),
    RGB(0,0,128),
    RGB(128,0,128),
    RGB(0,128,128),
    RGB(192,192,192),
    RGB(192,220,192),
    RGB(166,202,240),
    RGB(255,251,240),
    RGB(160,160,164),
    RGB(128,128,128),
    RGB(255,0,0),
    RGB(0,255,0),
    RGB(255,255,0),
    RGB(0,0,255),
    RGB(255,0,255),
    RGB(0,255,255),
    RGB(255,255,255)
};

// MRU table. See notes for Reset()

BYTE CColorBtnDlg::used[20] =
{    
    1,3,5,7,9,11,13,15,17,19,20,18,16,14,12,10,8,6,4,2    
};

/////////////////////////////////////////////////////////////////////////////
// CColorBtn

CColorBtn::CColorBtn()
{
    currentcolor = RGB(255,255,255);

    dlg.parent = this;   // This will allow the dialog to position itself

    // Create the pens and brushes that we'll need to draw the button

    nullpen.CreateStockObject(NULL_PEN);
    blackpen.CreateStockObject(BLACK_PEN);
    whitepen.CreateStockObject(WHITE_PEN);

    nullbrush.CreateStockObject(NULL_BRUSH);
    backbrush.CreateSolidBrush(GetSysColor(COLOR_3DFACE));
    dkgray.CreatePen(PS_SOLID,1,RGB(128,128,128));         
}


CColorBtn::~CColorBtn()
{
}


BEGIN_MESSAGE_MAP(CColorBtn, CButton)
	//{{AFX_MSG_MAP(CColorBtn)	
	ON_CONTROL_REFLECT(BN_CLICKED, OnClicked)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CColorBtn message handlers


void CColorBtn::DrawItem(LPDRAWITEMSTRUCT lpd)
{
    // Draw the button

    CBrush colorbrush;

    CDC DC;

    DC.Attach(lpd->hDC);
    
    int top,left,bottom,right;

    // Store this for convinience

    top    = lpd->rcItem.top;
    left   = lpd->rcItem.left;
    bottom = lpd->rcItem.bottom;
    right  = lpd->rcItem.right;
                
    colorbrush.CreateSolidBrush(currentcolor);
    
    oldpen   = DC.SelectObject(&nullpen);
    oldbrush = DC.SelectObject(&backbrush);

    // Clear the background using the 3DFACE color.

    DC.Rectangle(&lpd->rcItem);

    // Draw the border

    if (!(lpd->itemState & ODS_SELECTED))
    {
        // Button is up

        DC.SelectObject(&blackpen);

        DC.MoveTo(left,bottom-1);
        DC.LineTo(right-1,bottom-1);
        DC.LineTo(right-1,top);

        DC.SelectObject(&dkgray);

        DC.MoveTo(left+1,bottom-2);
        DC.LineTo(right-2,bottom-2);
        DC.LineTo(right-2,top+1);

        DC.SelectObject(&whitepen);

        DC.LineTo(left+1,top+1);
        DC.LineTo(left+1,bottom-2);
        
    }
    else
    {
        // Button is down

        DC.SelectObject(&dkgray);            

        DC.MoveTo(left,bottom-1);

        DC.LineTo(left,top);
        DC.LineTo(right-1,top);

        DC.SelectObject(&whitepen);

        DC.MoveTo(right-1,top-1);

        DC.LineTo(right-1,bottom-1);
        DC.LineTo(left+1,bottom-1);

        DC.SelectObject(&blackpen);

        DC.MoveTo(left+1,bottom-2);
        DC.LineTo(left+1,top+1);
        DC.LineTo(right-2,top+1);

        // by moving this, we get the things inside the button
        // to draw themselves one pixel down and one to the right.
        // This completes the "pushed" effect

        left++;
        right++;
        bottom++;
        top++;

    }

    // The division

    DC.SelectObject(&whitepen);
    
    DC.MoveTo(right-10,top+4);
    DC.LineTo(right-10,bottom-4);

    DC.SelectObject(dkgray);

    DC.MoveTo(right-11,top+4);
    DC.LineTo(right-11,bottom-4);

    // The triangle

    if (lpd->itemState & ODS_DISABLED)
        DC.SelectObject(dkgray);
    else
        DC.SelectObject(blackpen);
    
    DC.MoveTo(right-4,(bottom/2)-1);
    DC.LineTo(right-9,(bottom/2)-1);

    DC.MoveTo(right-5,(bottom/2));
    DC.LineTo(right-8,(bottom/2));

    if (lpd->itemState & ODS_DISABLED)    
    {
        DC.SetPixel(right-4,(bottom/2)-1,RGB(255,255,255));
        DC.SetPixel(right-5,(bottom/2),RGB(255,255,255));
        DC.SetPixel(right-6,(bottom/2)+1,RGB(255,255,255));
    }
    else
    {
        DC.SetPixel(right-6,(bottom/2)+1,RGB(0,0,0));
    }

    if (!(lpd->itemState & ODS_DISABLED))
    {
        // The color rectangle, only if enabled

        DC.SelectObject(&colorbrush);

        DC.Rectangle(left+5,top+4,right-15,bottom-4);    
    }

    if (lpd->itemState & ODS_FOCUS)
    {
        // Draw the focus
        //
        // It would have been nice just to
        // draw a rectangle using a pen created
        // with the PS_ALTERNATE style, but
        // this is not supported by WIN95

        int i;

        for (i=left+3;i<right-4;i+=2)
        {
            DC.SetPixel(i,top+3,RGB(0,0,0));
            DC.SetPixel(i,bottom-4,RGB(0,0,0));
        }

        for (i=top+3;i<bottom-4;i+=2)
        {
            DC.SetPixel(left+3,i,RGB(0,0,0));
            DC.SetPixel(right-4,i,RGB(0,0,0));
        }       
    }
        
    DC.SelectObject(oldpen);
    DC.SelectObject(oldbrush);


    DC.Detach();    
}


void CColorBtn::OnClicked() 
{
    // When the button is clicked, show the dialog.
	if (dlg.colorindex == -1)
	{
        for (int i=0;i<20;i++)
        {
            if (dlg.colors[i] == currentcolor)
            {
                dlg.colorindex = i;
                break;
            }
        }
	}
	if (dlg.DoModal() == IDOK)
    {
        currentcolor = CColorBtnDlg::colors[dlg.colorindex];
        InvalidateRect(NULL);
    }	
}

// Store and Load use an undocumented CWinApp function

BOOL CColorBtn::Store()
{
    return (AfxGetApp()->WriteProfileBinary("ColorData", "ColorTable",(LPBYTE)CColorBtnDlg::colors,sizeof(COLORREF)*20) &&
            AfxGetApp()->WriteProfileBinary("ColorData", "MRU",(LPBYTE)CColorBtnDlg::used,sizeof(BYTE)*20));

}

BOOL CColorBtn::Load()
{
    BYTE *data = NULL;
    UINT size;

    // This function allocates the memory it needs

    AfxGetApp()->GetProfileBinary("ColorData", "ColorTable",&data,&size);	

    if (data)
    {
        // Copy the data into our table and get rid of the buffer

        memcpy((void *)CColorBtnDlg::colors,(void *)data,size);
        free((void *)data);

        AfxGetApp()->GetProfileBinary("ColorData", "MRU",&data,&size);	

        if (data)
        {
            memcpy((void *)CColorBtnDlg::used,(void *)data,size);
            free((void *)data);
            return TRUE;
        }
        
    }

    // If the loading fails, back to the defaults
    
    Reset();

    return FALSE;
}


void CColorBtn::Reset()
{
    CColorBtnDlg::colors[0]  = RGB(0,0,0);
    CColorBtnDlg::colors[1]  = RGB(128,0,0);
    CColorBtnDlg::colors[2]  = RGB(0,128,0);
    CColorBtnDlg::colors[3]  = RGB(128,128,0);
    CColorBtnDlg::colors[4]  = RGB(0,0,128);
    CColorBtnDlg::colors[5]  = RGB(128,0,128);
    CColorBtnDlg::colors[6]  = RGB(0,128,128);
    CColorBtnDlg::colors[7]  = RGB(192,192,192);
    CColorBtnDlg::colors[8]  = RGB(192,220,192);
    CColorBtnDlg::colors[9]  = RGB(166,202,240);
    CColorBtnDlg::colors[10] = RGB(255,251,240);
    CColorBtnDlg::colors[11] = RGB(160,160,164);
    CColorBtnDlg::colors[12] = RGB(128,128,128);
    CColorBtnDlg::colors[13] = RGB(255,0,0);
    CColorBtnDlg::colors[14] = RGB(0,255,0);
    CColorBtnDlg::colors[15] = RGB(255,255,0);
    CColorBtnDlg::colors[16] = RGB(0,0,255);
    CColorBtnDlg::colors[17] = RGB(255,0,255);
    CColorBtnDlg::colors[18] = RGB(0,255,255);
    CColorBtnDlg::colors[19] = RGB(255,255,255);

    // This "colorful" (no pun intended) order ensures
    // that the colors at the center of the color table
    // will get replaced first. This preserves the white
    // and black colors even if they're not used (They'll
    // get replaced last).

⌨️ 快捷键说明

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