RGB Preview Dialog

This application shows a COLORREF structure that use Slider Control to determinates your 24-bit RGB color and shows also simple way to communicate between CSliderCtrl, CEdit, and CStatic.


void CRGBDlg::OnVScroll( UINT nSBCode,
UINT nPos,
CScrollBar* pScrollBar)
{
CSliderCtrl* pSlider = (CSliderCtrl*) pScrollBar;
CString szColorValue;
int i = pSlider->GetDlgCtrlID () – IDC_SLIDER1;

// Now, nColor = current SliderPosValue
nColor[i] = pSlider->GetPos();
szColorValue.Format(“%d”, nColor[i]);

SetDlgItemText(i + IDC_EDIT1, szColorValue);

// Get a pointer to the static window.
CStatic* pStatic = &m_ctlPaintStatic;
pStatic->UpdateWindow();
pStatic->Invalidate();

CDialog::OnVScroll(nSBCode, nPos, pScrollBar);
}

// PaintST.cpp : implementation file
//

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

CBrush* pOldBrush;
GetClientRect(rcStatic);

CBrush brBack(m_crBkColor);
dc.FillRect(rcStatic, &brBack);
pOldBrush = dc.SelectObject(&brBack);
dc.SelectObject(pOldBrush);

// Do not call CStatic::OnPaint() for painting messages
}

BOOL CPaintST::OnEraseBkgnd(CDC* pDC)
{
CBrush brBackgnd;

// Get a pointer to the parent window
CRGBDlg* pColorDlg = (CRGBDlg*) GetParent();
ASSERT(pColorDlg != NULL);

// Get new color value from SliderCtrl.
m_crBkColor = RGB ((BYTE) pColorDlg->nColor[0],
(BYTE) pColorDlg->nColor[1],
(BYTE) pColorDlg->nColor[2]);

GetClientRect(rcStatic);
brBackgnd.CreateSolidBrush(m_crBkColor);
pDC->FillRect(rcStatic, &brBackgnd);

return CStatic::OnEraseBkgnd(pDC);
}

Downloads

Download demo project – 40 Kb

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read