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

📄 chameleonctl.cpp

📁 VC编写的变色龙按钮程序
💻 CPP
📖 第 1 页 / 共 5 页
字号:
}

long CChameleonCtrl::GetAcceleratKey() 
{
	return (long)m_ucAccessKey;
}

/////////////////////////////////////////////////////////////////////////////
//
// 重绘函数
// 参数:	pdcDraw		绘图场景
//			nStatus		按钮状态,0,正常;1,按下
//			bForce		是否强制重画
// 返回值:	0			正确
//
/////////////////////////////////////////////////////////////////////////////
int CChameleonCtrl::Redraw( int nStatus, bool bForce, CDC * pdcDraw )
{
	BOOL bDefault;
	bool bReleaseDC = false;

	CString strCaption = InternalGetText();
	CFont * pfntOld;

	if( !bForce )
	{
		//如果当前按钮状态未变化,文字也未变化,则取消不必要的重画
		if( m_nStatus == nStatus && m_strText == strCaption )
		{
			return 0;
		}
	}

	//按钮区域为空则退出
	if( m_nHeight == 0 || m_nWidth == 0 )
	{
		return 0;
	}

	//保存按钮状态、文字
	m_nStatus = nStatus;
	m_strText = strCaption;

	//获取按钮是否为Default按钮
	GetAmbientProperty( DISPID_AMBIENT_DISPLAYASDEFAULT, VT_BOOL, &bDefault );

	//如果未传入DC,则获取控件DC
	if( pdcDraw == NULL )
	{
		pdcDraw = GetDC();
		bReleaseDC = true;
	}

	//选入STOCK FONT
	pfntOld = SelectStockFont( pdcDraw );

	//设置绘画所需的各种表面颜色
	SetColor();

	DrawRectangle( pdcDraw->m_hDC, 0, 0, m_nWidth, m_nHeight, m_ulFace );

	if( m_bEnabled )
	{
		if( m_nStatus == 0 )
		{
			//正常状态
			if( m_lButtonType == 1 )
			{
				//Windows16
				if( m_bMouseOver )
				{
					DrawCaption( pdcDraw->m_hDC, 1 );
				}
				else
				{
					DrawCaption( pdcDraw->m_hDC, 0 );
				}
				DrawFrame( pdcDraw->m_hDC, m_ulHighLight, m_ulShadow, 
					m_ulHighLight, m_ulShadow, -1 );
				DrawRectangle( pdcDraw->m_hDC, 0, 0, m_nWidth, m_nHeight, 
					m_ulDarkShadow, true );
				DrawFocusR( pdcDraw->m_hDC, m_recFocusRect, m_ulText );
			}
			else if( m_lButtonType == 2 )
			{
				//Windows32
				if( m_bMouseOver )
				{
					DrawCaption( pdcDraw->m_hDC, 1 );
				}
				else
				{
					DrawCaption( pdcDraw->m_hDC, 0 );
				}
				
				if( bDefault && m_bShowFocusRect )
				{
					DrawFrame( pdcDraw->m_hDC, m_ulHighLight, m_ulDarkShadow, 
						m_ulLight, m_ulShadow, -1 );
					DrawFocusR( pdcDraw->m_hDC, m_recFocusRect, m_ulText );
					DrawRectangle( pdcDraw->m_hDC, 0, 0, m_nWidth, m_nHeight, 
						m_ulDarkShadow, true );
				}
				else
				{
					DrawFrame( pdcDraw->m_hDC, m_ulHighLight, m_ulDarkShadow, 
						m_ulLight, m_ulShadow, 0 );
				}
			}
			else if( m_lButtonType == 3 )
			{
				//WindowsXP
				float fStep;

				fStep = 25 / (float)m_nHeight;
				for( int nCount = 1; nCount <= m_nHeight; nCount++ )
				{
					DrawLine( pdcDraw->m_hDC, 0, nCount, m_nWidth, 
						nCount, ShiftColor( m_ulXPFace, (int)(-fStep * nCount), true ) );
				}

				if( m_bMouseOver )
				{
					DrawCaption( pdcDraw->m_hDC, 1 );
				}
				else
				{
					DrawCaption( pdcDraw->m_hDC, 0 );
				}
				DrawRectangle( pdcDraw->m_hDC, 0, 0, m_nWidth, m_nHeight, 0x733C00, true );
				SetPixel( pdcDraw->m_hDC, 1, 1, 0x7B4D10 );
				SetPixel( pdcDraw->m_hDC, 1, m_nHeight - 2, 0x7B4D10 );
				SetPixel( pdcDraw->m_hDC, m_nWidth - 2, 1, 0x7B4D10 );
				SetPixel( pdcDraw->m_hDC, m_nWidth - 2, m_nHeight - 2, 0x7B4D10 );

				if( m_bMouseOver )
				{
					DrawRectangle( pdcDraw->m_hDC, 1, 2, 
						m_nWidth - 2, m_nHeight - 4, 0x31B2FF, true );
					DrawLine( pdcDraw->m_hDC, 2, m_nHeight - 2, 
						m_nWidth - 2, m_nHeight - 2, 0x96E7 );
					DrawLine( pdcDraw->m_hDC, 2, 1, m_nWidth - 2, 1, 0xCEF3FF );
					DrawLine( pdcDraw->m_hDC, 1, 2, m_nWidth - 1, 2, 0x8CDBFF );
					DrawLine( pdcDraw->m_hDC, 2, 3, 2, m_nHeight - 3, 0x6BCBFF );
					DrawLine( pdcDraw->m_hDC, m_nWidth - 3, 
						3, m_nWidth - 3, m_nHeight - 3, 0x6BCBFF );
				}
				else if( ( m_bFocus || bDefault ) && m_bShowFocusRect )
				{
					DrawRectangle( pdcDraw->m_hDC, 1, 2, 
						m_nWidth - 2, m_nHeight - 4, 0xE7AE8C, true );
					DrawLine( pdcDraw->m_hDC, 2, m_nHeight - 2, 
						m_nWidth - 2, m_nHeight - 2, 0xEF826B );
					DrawLine( pdcDraw->m_hDC, 2, 1, m_nWidth - 2, 1, 0xFFE7CE );
                    DrawLine( pdcDraw->m_hDC, 1, 2, m_nWidth - 1, 2, 0xF7D7BD );
					DrawLine( pdcDraw->m_hDC, 2, 3, 2, m_nHeight - 3, 0xF0D1B5 );
					DrawLine( pdcDraw->m_hDC, m_nWidth - 3, 3, 
						m_nWidth - 3, m_nHeight - 3, 0xF0D1B5 );
				}
				else
				{
					DrawLine( pdcDraw->m_hDC, 2, m_nHeight - 2, 
						m_nWidth - 2, m_nHeight - 2, ShiftColor( m_ulXPFace, -0x30, true ) );
                    DrawLine( pdcDraw->m_hDC, 1, m_nHeight - 3, 
						m_nWidth - 2, m_nHeight - 3, ShiftColor( m_ulXPFace, -0x20, true ) );
                    DrawLine( pdcDraw->m_hDC, m_nWidth - 2, 2, 
						m_nWidth - 2, m_nHeight - 2, ShiftColor( m_ulXPFace, -0x24, true ) );
                    DrawLine( pdcDraw->m_hDC, m_nWidth - 3, 3, 
						m_nWidth - 3, m_nHeight - 3, ShiftColor( m_ulXPFace, -0x18, true ) );
                    DrawLine( pdcDraw->m_hDC, 2, 1, 
						m_nWidth - 2, 1, ShiftColor( m_ulXPFace, 0x10, true ) );
                    DrawLine( pdcDraw->m_hDC, 1, 2, 
						m_nWidth - 2, 2, ShiftColor( m_ulXPFace, 0x0A, true ) );
                    DrawLine( pdcDraw->m_hDC, 1, 2, 1, m_nHeight - 2, 
						ShiftColor( m_ulXPFace, -0x05, true ) );
                    DrawLine( pdcDraw->m_hDC, 2, 3, 2, m_nHeight - 3, 
						ShiftColor( m_ulXPFace, -0x0A, true ) );
				}
			}
			else if( m_lButtonType == 4 )
			{
				//Mac
				DrawRectangle( pdcDraw->m_hDC, 1, 1, 
					m_nWidth - 2, m_nHeight - 2, m_ulLight );
				if( m_bMouseOver )
				{
					DrawCaption( pdcDraw->m_hDC, 1 );
				}
				else
				{
					DrawCaption( pdcDraw->m_hDC, 0 );
				}
				DrawRectangle( pdcDraw->m_hDC, 0, 0, m_nWidth, m_nHeight, 
					m_ulDarkShadow, true );
				SetPixel( pdcDraw->m_hDC, 1, 1, m_ulDarkShadow );
				SetPixel( pdcDraw->m_hDC, 1, m_nHeight - 2, m_ulDarkShadow );
				SetPixel( pdcDraw->m_hDC, m_nWidth - 2, 1, m_ulDarkShadow );
				SetPixel( pdcDraw->m_hDC, m_nWidth - 2, m_nHeight - 2, m_ulDarkShadow );
				DrawLine( pdcDraw->m_hDC, 1, 2, 2, 0, m_ulFace );
				DrawLine( pdcDraw->m_hDC, 3, 2, m_nWidth - 3, 2, m_ulHighLight );
				DrawLine( pdcDraw->m_hDC, 2, 2, 2, m_nHeight - 3, m_ulHighLight );
                SetPixel( pdcDraw->m_hDC, 3, 3, m_ulHighLight );
				DrawLine( pdcDraw->m_hDC, m_nWidth - 3, 1, 
					m_nWidth - 3, m_nHeight - 3, m_ulFace );
				DrawLine( pdcDraw->m_hDC, 1, m_nHeight - 3, 
					m_nWidth - 3, m_nHeight - 3, m_ulFace );
				SetPixel( pdcDraw->m_hDC, m_nWidth - 4, m_nHeight - 4, m_ulFace );
				DrawLine( pdcDraw->m_hDC, m_nWidth - 2, 2, 
					m_nWidth - 2, m_nHeight - 2, m_ulShadow );
				DrawLine( pdcDraw->m_hDC, 2, m_nHeight - 2, 
					m_nWidth - 2, m_nHeight - 2, m_ulShadow );
				SetPixel( pdcDraw->m_hDC, m_nWidth - 3, m_nHeight - 3, m_ulShadow );
			}
			else if( m_lButtonType == 5 )
			{
				//Java
				DrawRectangle( pdcDraw->m_hDC, 1, 1, m_nWidth - 1, m_nHeight - 1, 
					ShiftColor( m_ulFace, 0x0C ) );
				if( m_bMouseOver )
				{
					DrawCaption( pdcDraw->m_hDC, 1 );
				}
				else
				{
					DrawCaption( pdcDraw->m_hDC, 0 );
				}
				DrawRectangle( pdcDraw->m_hDC, 1, 1, m_nWidth - 1, m_nHeight - 1, 
					m_ulHighLight, true );
				DrawRectangle( pdcDraw->m_hDC, 0, 0, m_nWidth - 1, m_nHeight - 1, 
					ShiftColor( m_ulShadow, -0x1A ), true );
				SetPixel( pdcDraw->m_hDC, 1, m_nHeight - 2, 
					ShiftColor( m_ulShadow, 0x1A ) );
				SetPixel( pdcDraw->m_hDC, m_nWidth - 2, 1, 
					ShiftColor( m_ulShadow, 0x1A ) );
				if( m_bFocus && m_bShowFocusRect )
				{
					DrawRectangle( pdcDraw->m_hDC, m_recText.left - 2, m_recText.top -1, 
						m_recText.Width() + 4, m_recText.Height() + 2, 0xCC9999, true );
				}
			}
			else if( m_lButtonType == 6 )
			{
				//Netscape6
				if( m_bMouseOver )
				{
					DrawCaption( pdcDraw->m_hDC, 1 );
				}
				else
				{
					DrawCaption( pdcDraw->m_hDC, 0 );
				}
				DrawFrame( pdcDraw->m_hDC, ShiftColor( m_ulLight, 0x08 ), 
					m_ulShadow, ShiftColor( m_ulLight, 0x08 ), m_ulShadow, 0 );
				
				DrawFocusR( pdcDraw->m_hDC, m_recFocusRect, m_ulText );
			}
			else if( m_lButtonType == 7 )
			{
				//SimpleFlat
				if( m_bMouseOver )
				{
					DrawCaption( pdcDraw->m_hDC, 1 );
				}
				else
				{
					DrawCaption( pdcDraw->m_hDC, 0 );
				}
				DrawFrame( pdcDraw->m_hDC, m_ulHighLight, m_ulShadow, 0, 0, 0, true );
				
				DrawFocusR( pdcDraw->m_hDC, m_recFocusRect, m_ulText );
			}
			else if( m_lButtonType == 8 )
			{
				//FlatHight
				if( m_bMouseOver )
				{
					DrawCaption( pdcDraw->m_hDC, 1 );
				}
				else
				{
					DrawCaption( pdcDraw->m_hDC, 0 );
				}
				if( m_bMouseOver )
				{
					DrawFrame( pdcDraw->m_hDC, m_ulHighLight, m_ulShadow, 0, 0, 0, true );
				}

				DrawFocusR( pdcDraw->m_hDC, m_recFocusRect, m_ulText );
			}
			else if( m_lButtonType == 9 )
			{
				//OfficeXP
				if( m_bMouseOver )
				{
					DrawRectangle( pdcDraw->m_hDC, 1, 1, m_nWidth, m_nHeight, 
						m_ulOfficeXPF );
				}
				if( m_bMouseOver )
				{
					DrawCaption( pdcDraw->m_hDC, 1 );
				}
				else
				{
					DrawCaption( pdcDraw->m_hDC, 0 );
				}
				if( m_bMouseOver )
				{
					DrawRectangle( pdcDraw->m_hDC, 0, 0, m_nWidth, m_nHeight, 
						m_ulOfficeXPB, true );
				}

				DrawFocusR( pdcDraw->m_hDC, m_recFocusRect, m_ulText );
			}
			else if( m_lButtonType == 10 )
			{
				/*
				//Transparent
				CRect recClient;
				POINT pntPos;
				CDC * pdcParent;

				pntPos.x = 0;
				pntPos.y = 0;
				GetClientRect( recClient );
				pdcParent = GetParent()->GetDC();

				CBitmap * pbmpOld;
				BITMAP uBmp;

				memset( &uBmp, 0, sizeof(BITMAP) );
				pbmpOld = pdcParent->GetCurrentBitmap();
				pbmpOld->GetBitmap( &uBmp );

				memset( &uBmp, 0, sizeof(BITMAP) );
				pbmpOld = pdcDraw->GetCurrentBitmap();
				pbmpOld->GetBitmap( &uBmp );

				MapWindowPoints( GetParent(), &pntPos, 1 );
				pdcDraw->BitBlt( 0, 0, recClient.Width(), recClient.Height(),
					pdcParent, pntPos.x, pntPos.y, SRCCOPY );

				pdcDraw->SelectObject( pbmpOld );

				GetParent()->ReleaseDC( pdcParent );
				*/
			}
			else if( m_lButtonType == 11 )
			{
				//Hover
				if( m_bMouseOver )
				{
					DrawCaption( pdcDraw->m_hDC, 1 );
				}
				else
				{
					DrawCaption( pdcDraw->m_hDC, 0 );
				}
				if( m_bMouseOver )
				{
					DrawFrame( pdcDraw->m_hDC, m_ulHighLight, m_ulDarkShadow, 
						m_ulLight, m_ulShadow, 0, false );
				}

				DrawFocusR( pdcDraw->m_hDC, m_recFocusRect, m_ulText );
			}
			else if( m_lButtonType == 12 )
			{
				//KDE2
				CFont fntBold;
				CFont * pfntCur;
				CFont * pfntOld = NULL;
				float fStep;
				if( m_bMouseOver )
				{
					DrawRectangle( pdcDraw->m_hDC, 0, 0, m_nWidth, m_nHeight, m_ulLight );
				}
				else
				{
					fStep = 58 / (float)m_nHeight;
					for( int nCount = 1; nCount <= m_nHeight; nCount++ )
					{
						DrawLine( pdcDraw->m_hDC, 0, nCount, m_nWidth, nCount, 
							ShiftColor( m_ulHighLight, (int)(-fStep * nCount) ) );
					}
				}
				if( bDefault )
				{
					LOGFONT uLogFont;
					pfntCur = pdcDraw->GetCurrentFont();
					pfntCur->GetLogFont( &uLogFont );
					uLogFont.lfWeight = FW_BOLD;
					if( fntBold.CreateFontIndirect( &uLogFont ) )
					{
						pfntOld = pdcDraw->SelectObject( &fntBold );
					}
				}
				if( m_bMouseOver )
				{
					DrawCaption( pdcDraw->m_hDC, 1 );
				}
				else
				{
					DrawCaption( pdcDraw->m_hDC, 0 );
				}
				if( bDefault )
				{
					if( pfntOld != NULL )
					{
						pdcDraw->SelectObject( pfntOld );
					}
				}
				DrawRectangle( pdcDraw->m_hDC, 0, 0, m_nWidth, m_nHeight, 
					ShiftColor( m_ulShadow, -0x32 ), true );
				DrawRectangle( pdcDraw->m_hDC, 1, 1, m_nWidth - 2, m_nHeight - 2, 
					ShiftColor( m_ulFace, -0x09 ), true );
				DrawRectangle( pdcDraw->m_hDC, 2, 2, m_nWidth - 4, 2, m_ulHighLight );
				DrawRectangle( pdcDraw->m_hDC, 2, 4, 2, m_nHeight - 6, m_ulHighLight );
				
				DrawFocusR( pdcDraw->m_hDC, m_recFocusRect, m_ulText );
			}
			else if( m_lButtonType == 13 )
			{
				//Graphic
				int nPicWidth;
				int nPicHeight;
				CRect recRender;
				CRect recWBounds;

				if( m_bMouseOver )
				{
					GetPictureSize( 3, nPicWidth, nPicHeight );
					if( nPicWidth != 0 && nPicHeight != 0 )
					{
						recRender.left = 0;
						recRender.top = 0;
						if( m_bStretch )
						{
							recRender.right = m_nWidth;
							recRender.bottom = m_nHeight;
						}
						else
						{
							recRender.right = nPicWidth;
							recRender.bottom = nPicHeight;
						}
						recWBounds.left = 0;
						recWBounds.top = 0;
						recWBounds.right = nPicWidth;
						recWBounds.bottom = nPicHeight;
						m_phdOverPicture.Render( pdcDraw, recRender, recWBounds );
					}
				}
				else
				{
					GetPictureSize( 1, nPicWidth, nPicHeight );
					if( nPicWidth != 0 && nPicHeight != 0 )
					{
						recRender.left = 0;
						recRender.top = 0;
						if( m_bStretch )
						{
							recRender.right = m_nWidth;
							recRender.bottom = m_nHeight;
						}
						else
						{
							recRender.right = nPicWidth;
							recRender.bottom = nPicHeight;
						}
						recWBounds.left = 0;
						recWBounds.top = 0;
						recWBounds.right = nPicWidth;
						recWBounds.bottom = nPicHeight;
						m_phdPicture.Render( pdcDraw, recRender, recWBounds );
					}
				}

				if( m_bMouseOver )
				{
					DrawCaption( pdcDraw->m_hDC, 1 );
				}
				else
				{
					DrawCaption( pdcDraw->m_hDC, 0 );
				}
			}
		}
		else if( m_nStatus == 1 )
		{
			//Press状态
			if( m_lButtonType == 1 )
			{
				//Windows16
				DrawCaption( pdcDraw->m_hDC, 2 );
				DrawFrame( pdcDraw->m_hDC, m_ulShadow, m_ulHighLight, 
					m_ulShadow, m_ulHighLight, -1 );
				DrawRectangle( pdcDraw->m_hDC, 0, 0, m_nWidth, m_nHeight, 
					m_ulDarkShadow, true );
				DrawFocusR( pdcDraw->m_hDC, m_recFocusRect, m_ulText );

⌨️ 快捷键说明

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