예제 #1
0
///////////////////////////////////////////////////////////////////////////////
// DrawVertical
void CXScrollBar::DrawVertical()
{
	TRACE(_T("in CXScrollBar::DrawVertical\n"));

	CClientDC dc(this);
	CMemDC memDC(&dc, &m_rectClient);

	CBrush brushFrame(FRAME_COLOR);

	CDC bitmapDC;
	bitmapDC.CreateCompatibleDC(&dc);
	CBitmap bitmap;

	// =====  draw Up arrow  =====

	VERIFY(bitmap.LoadBitmap(IDB_VERTICAL_SCROLLBAR_UPARROW));
	CBitmap* pOldBitmap = bitmapDC.SelectObject(&bitmap);

	// NOTE:  thumb and arrow bitmaps are assumed to be same width and height
	CRect rectUpArrow(m_rectClient.left, m_rectClient.top, 
		m_rectClient.right, m_rectClient.top + m_nBitmapHeight);

	memDC.StretchBlt(rectUpArrow.left, rectUpArrow.top, 
		rectUpArrow.Width(), rectUpArrow.Height(), 
		&bitmapDC, 0, 0, m_nBitmapWidth, m_nBitmapHeight, SRCCOPY);

	memDC.FrameRect(&rectUpArrow, &brushFrame);
	if (pOldBitmap)
	{
		bitmapDC.SelectObject(pOldBitmap);
	}

	if (bitmap.GetSafeHandle())
	{
		bitmap.DeleteObject();
	}

	pOldBitmap = NULL;

	int nChannelStart = m_rectClient.top + m_nBitmapHeight;
	int nChannelHeight = m_rectClient.Height() - 2*m_nBitmapHeight;

	// =====  draw channel  =====

	// save new thumb position
	TRACE(_T("m_nThumbTop=%d\n"), m_nThumbTop);
	m_rectThumb.left   = m_rectClient.left;
	m_rectThumb.right  = m_rectThumb.left + m_rectClient.Width();
	m_rectThumb.top    = m_rectClient.top + m_nThumbTop;
	m_rectThumb.bottom = m_rectThumb.top + m_nBitmapHeight;

	VERIFY(bitmap.LoadBitmap(IDB_VERTICAL_SCROLLBAR_CHANNEL));
	pOldBitmap = bitmapDC.SelectObject(&bitmap);

	CRect rectChannelDown(m_rectClient.left, m_rectThumb.top + m_nBitmapHeight/2, 
		m_rectClient.right, nChannelStart + nChannelHeight);

	memDC.StretchBlt(rectChannelDown.left+1, rectChannelDown.top, 
		rectChannelDown.Width()-1, rectChannelDown.Height(), 
		&bitmapDC, 0, 0, m_nBitmapWidth, 1, SRCCOPY);

	if (m_bChannelColor && m_bThumbColor)
	{
		// thumb has a color, so use same (lightened) color for channel
		CColor color;
		color.SetRGB(GetRValue(m_ThumbColor),
					 GetGValue(m_ThumbColor), 
					 GetBValue(m_ThumbColor));
		color.ToHLS();
		float fLuminance = color.GetLuminance();

		// use 80% L, 150% S for main color
		fLuminance = .80f;
		float fSaturation = color.GetSaturation();
		fSaturation = 0.5f * fSaturation;
		float fHue = color.GetHue();
		color.SetHLS(fHue, fLuminance, fSaturation);
		color.ToRGB();
		COLORREF rgb3 = RGB(color.GetRed(), color.GetGreen(), color.GetBlue());

		// use .87 L for second highlight color
		fLuminance = .87f;
		color.SetHLS(fHue, fLuminance, fSaturation);
		color.ToRGB();
		COLORREF rgb2 = RGB(color.GetRed(), color.GetGreen(), color.GetBlue());

		// use .92 L for first highlight color
		fLuminance = .92f;
		color.SetHLS(fHue, fLuminance, fSaturation);
		color.ToRGB();
		COLORREF rgb1 = RGB(color.GetRed(), color.GetGreen(), color.GetBlue());

		BITMAP bm;
		bitmap.GetBitmap(&bm);

		// set highlight colors
		bitmapDC.SetPixel(0, 0, rgb1);
		bitmapDC.SetPixel(1, 0, rgb2);

		// set main color
		for (int x = 2; x < (bm.bmWidth); x++)
		{
			bitmapDC.SetPixel(x, 0, rgb3);
		}
	}

	CRect rectChannelUp(m_rectClient.left, nChannelStart, 
		m_rectClient.right, m_rectThumb.top + m_nBitmapHeight/2);

	memDC.StretchBlt(rectChannelUp.left, rectChannelUp.top, 
		rectChannelUp.Width(), rectChannelUp.Height(), 
		&bitmapDC, 0, 0, m_nBitmapWidth, 1, SRCCOPY);

	if (pOldBitmap)
	{
		bitmapDC.SelectObject(pOldBitmap);
	}
	if (bitmap.GetSafeHandle())
	{
		bitmap.DeleteObject();
	}

	pOldBitmap = NULL;

	// =====  draw down arrow  =====

	VERIFY(bitmap.LoadBitmap(IDB_VERTICAL_SCROLLBAR_DOWNARROW));
	pOldBitmap = bitmapDC.SelectObject(&bitmap);

	CRect rectDownArrow(m_rectClient.left, m_rectClient.bottom - m_nBitmapHeight, 
		m_rectClient.right, m_rectClient.bottom);

	memDC.StretchBlt(rectDownArrow.left, rectDownArrow.top, 
		rectDownArrow.Width()-1, rectDownArrow.Height(), 
		&bitmapDC, 0, 0, m_nBitmapWidth, m_nBitmapHeight, SRCCOPY);
	memDC.FrameRect(&rectDownArrow, &brushFrame);

	if (pOldBitmap)
	{
		bitmapDC.SelectObject(pOldBitmap);
	}
	if (bitmap.GetSafeHandle())
	{
		bitmap.DeleteObject();
	}

	pOldBitmap = NULL;

	// If there is nothing to scroll then don't show the thumb
	if (m_nRange)
	{
		// =====  draw thumb  =====

		VERIFY(bitmap.LoadBitmap(m_bThumbColor ? 
			IDB_VERTICAL_SCROLLBAR_THUMB : 
			IDB_VERTICAL_SCROLLBAR_THUMB_NO_COLOR));
		pOldBitmap = bitmapDC.SelectObject(&bitmap);

		COLORREF rgbThumb = m_bThumbHover ? m_ThumbHoverColor : m_ThumbColor;
		COLORREF rgbPrev = 0;

		// add desired color to thumb
		for (int x = 0; x < m_nBitmapWidth; x++)
		{
			for (int y = 0; y < m_nBitmapHeight; y++)
			{
				COLORREF rgb = bitmapDC.GetPixel(x, y);
				switch (rgb)
				{
				case THUMB_MASK_COLOR:
					if (m_bThumbColor)
					{
						bitmapDC.SetPixel(x, y, rgbThumb);
					}
					break;

				case THUMB_GRIPPER_MASK_COLOR:
					bitmapDC.SetPixel(x, y, m_bThumbGripper ? 
						THUMB_GRIPPER_COLOR : rgbPrev);
					break;

				case THUMB_UP_TRANSPARENT_MASK_COLOR:
					bitmapDC.SetPixel(x, y, 
						memDC.GetPixel(x, nChannelStart));
					break;

				case THUMB_DOWN_TRANSPARENT_MASK_COLOR:
					bitmapDC.SetPixel(x, y, 
						memDC.GetPixel(x, nChannelStart+nChannelHeight-1));
					break;

				default:
					break;
				}
				rgbPrev = rgb;
			}
		}

		memDC.StretchBlt(m_rectThumb.left, m_rectThumb.top, 
			m_rectThumb.Width(), m_rectThumb.Height(), 
			&bitmapDC, 0, 0, m_nBitmapWidth, m_nBitmapHeight, SRCCOPY);

		if (pOldBitmap)
		{
			bitmapDC.SelectObject(pOldBitmap);
		}

		if (bitmap.GetSafeHandle())
		{
			bitmap.DeleteObject();
		}
		pOldBitmap = NULL;
	}
	else
	{
		m_rectThumb = CRect(-1,-1,-1,-1);
	}

	memDC.FrameRect(&m_rectClient, &brushFrame);
}
예제 #2
0
///////////////////////////////////////////////////////////////////////////////
// DrawHorizontal
void CXScrollBar::DrawHorizontal()
{
	TRACE(_T("in CXScrollBar::DrawHorizontal\n"));

	CClientDC dc(this);
	CMemDC memDC(&dc, &m_rectClient);

	CBrush brushFrame(FRAME_COLOR);

	CDC bitmapDC;
	bitmapDC.CreateCompatibleDC(&dc);
	CBitmap bitmap;

	// =====  draw left arrow  =====

	VERIFY(bitmap.LoadBitmap(IDB_HORIZONTAL_SCROLLBAR_LEFTARROW));
	CBitmap* pOldBitmap = bitmapDC.SelectObject(&bitmap);

	// NOTE:  thumb and arrow bitmaps are assumed to be same width and height

	CRect rectLeftArrow(m_rectClient.left, m_rectClient.top,
		m_rectClient.left + m_nBitmapWidth, m_rectClient.bottom);

	memDC.StretchBlt(rectLeftArrow.left, rectLeftArrow.top+1,
		rectLeftArrow.Width(), rectLeftArrow.Height()-1,
		&bitmapDC, 0, 0, m_nBitmapWidth, m_nBitmapHeight, SRCCOPY);

	memDC.FrameRect(&rectLeftArrow, &brushFrame);

	int nChannelStart = m_rectClient.left + m_nBitmapWidth;
	int nChannelWidth = m_rectClient.Width() - 2*m_nBitmapWidth;

	if (pOldBitmap)
	{
		bitmapDC.SelectObject(pOldBitmap);
	}
	if (bitmap.GetSafeHandle())
	{
		bitmap.DeleteObject();
	}
	pOldBitmap = NULL;

	// =====  draw channel  =====

	// save new thumb position
	TRACE(_T("m_nThumbLeft=%d\n"), m_nThumbLeft);
	m_rectThumb.left   = m_rectClient.left + m_nThumbLeft;
	m_rectThumb.right  = m_rectThumb.left + m_nBitmapWidth;
	m_rectThumb.top    = m_rectClient.top;
	m_rectThumb.bottom = m_rectThumb.top + m_rectClient.Height();

	VERIFY(bitmap.LoadBitmap(IDB_HORIZONTAL_SCROLLBAR_CHANNEL));

	pOldBitmap = bitmapDC.SelectObject(&bitmap);

	CRect rectChannelRight(m_rectThumb.left + m_nBitmapWidth/2, m_rectClient.top,
		nChannelStart + nChannelWidth, m_rectClient.bottom);

	memDC.StretchBlt(rectChannelRight.left, rectChannelRight.top+1,
		rectChannelRight.Width(), rectChannelRight.Height()-1,
		&bitmapDC, 0, 0, 1, m_nBitmapHeight, SRCCOPY);

	if (m_bChannelColor && m_bThumbColor)
	{
		// thumb has a color, so use same (lightened) color for channel
		CColor color;
		color.SetRGB(GetRValue(m_ThumbColor),
					 GetGValue(m_ThumbColor),
					 GetBValue(m_ThumbColor));
		color.ToHLS();
		float fLuminance = color.GetLuminance();

		// use 80% L, 150% S for main color
		fLuminance = .80f;
		float fSaturation = color.GetSaturation();
		fSaturation = 0.5f * fSaturation;
		float fHue = color.GetHue();
		color.SetHLS(fHue, fLuminance, fSaturation);
		color.ToRGB();
		COLORREF rgb3 = RGB(color.GetRed(), color.GetGreen(), color.GetBlue());

		// use .87 L for second highlight color
		fLuminance = .87f;
		color.SetHLS(fHue, fLuminance, fSaturation);
		color.ToRGB();
		COLORREF rgb2 = RGB(color.GetRed(), color.GetGreen(), color.GetBlue());

		// use .92 L for first highlight color
		fLuminance = .92f;
		color.SetHLS(fHue, fLuminance, fSaturation);
		color.ToRGB();
		COLORREF rgb1 = RGB(color.GetRed(), color.GetGreen(), color.GetBlue());

		BITMAP bm;
		bitmap.GetBitmap(&bm);

		// set highlight colors
		bitmapDC.SetPixel(0, 0, rgb1);
		bitmapDC.SetPixel(0, 1, rgb2);

		// set main color
		for (int y = 2; y < (bm.bmHeight); y++)
		{
			bitmapDC.SetPixel(0, y, rgb3);
		}
	}

	CRect rectChannelLeft(nChannelStart, m_rectClient.top,
		m_rectThumb.left + m_nBitmapWidth/2, m_rectClient.bottom);

	memDC.StretchBlt(rectChannelLeft.left, rectChannelLeft.top+1,
		rectChannelLeft.Width(), rectChannelLeft.Height()-1,
		&bitmapDC, 0, 0, 1, m_nBitmapHeight, SRCCOPY);

	if (pOldBitmap)
	{
		bitmapDC.SelectObject(pOldBitmap);
	}

	if (bitmap.GetSafeHandle())
	{
		bitmap.DeleteObject();
	}
	pOldBitmap = NULL;

	// =====  draw right arrow  =====

	VERIFY(bitmap.LoadBitmap(IDB_HORIZONTAL_SCROLLBAR_RIGHTARROW));

	pOldBitmap = bitmapDC.SelectObject(&bitmap);

	CRect rectRightArrow(m_rectClient.right - m_nBitmapWidth, m_rectClient.top,
		m_rectClient.right, m_rectClient.bottom);

	memDC.StretchBlt(rectRightArrow.left, rectRightArrow.top+1,
		rectRightArrow.Width(), rectRightArrow.Height()-1,
		&bitmapDC, 0, 0, m_nBitmapWidth, m_nBitmapHeight, SRCCOPY);

	memDC.FrameRect(&rectRightArrow, &brushFrame);

	if (pOldBitmap)
	{
		bitmapDC.SelectObject(pOldBitmap);
	}

	if (bitmap.GetSafeHandle())
	{
		bitmap.DeleteObject();
	}

	pOldBitmap = NULL;

	// If there is nothing to scroll then don't show the thumb
	if (m_nRange)
	{
		// =====  draw thumb  =====

		if (m_bThumbColor)
		{
			VERIFY(bitmap.LoadBitmap(IDB_HORIZONTAL_SCROLLBAR_THUMB));
		}
		else
		{
			VERIFY(bitmap.LoadBitmap(IDB_HORIZONTAL_SCROLLBAR_THUMB_NO_COLOR));
		}

		pOldBitmap = bitmapDC.SelectObject(&bitmap);

		COLORREF rgbThumb = m_ThumbColor;
		if (m_bThumbHover)
		{
			rgbThumb = m_ThumbHoverColor;
		}

		COLORREF rgbPrev = 0;

		// add desired color to thumb
		for (int x = 0; x < m_nBitmapWidth; x++)
		{
			for (int y = 0; y < m_nBitmapHeight; y++)
			{
				COLORREF rgb = bitmapDC.GetPixel(x, y);

				if (m_bThumbColor && (rgb == THUMB_MASK_COLOR))
				{
					bitmapDC.SetPixel(x, y, rgbThumb);
				}
				else if (rgb == THUMB_GRIPPER_MASK_COLOR)
				{
					if (m_bThumbGripper)
					{
						bitmapDC.SetPixel(x, y, THUMB_GRIPPER_COLOR);
					}
					else
					{
						bitmapDC.SetPixel(x, y, rgbPrev);
					}
				}
				else if (rgb == THUMB_LEFT_TRANSPARENT_MASK_COLOR)
				{
					COLORREF rgbLeftChannel = memDC.GetPixel(nChannelStart, y);
					bitmapDC.SetPixel(x, y, rgbLeftChannel);
				}
				else if (rgb == THUMB_RIGHT_TRANSPARENT_MASK_COLOR)
				{
					COLORREF rgbRightChannel =
						memDC.GetPixel(nChannelStart+nChannelWidth-1, y);
					bitmapDC.SetPixel(x, y, rgbRightChannel);
				}

				rgbPrev = rgb;
			}
		}

		memDC.StretchBlt(m_rectThumb.left, m_rectThumb.top,
			m_rectThumb.Width(), m_rectThumb.Height(),
			&bitmapDC, 0, 0, m_nBitmapWidth, m_nBitmapHeight, SRCCOPY);

		if (pOldBitmap)
		{
			bitmapDC.SelectObject(pOldBitmap);
		}
		if (bitmap.GetSafeHandle())
		{
			bitmap.DeleteObject();
		}
		pOldBitmap = NULL;
	}
	else
	{
		m_rectThumb = CRect(-1,-1,-1,-1);
	}

	memDC.FrameRect(&m_rectClient, &brushFrame);
}