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;

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

// PaintST.cpp : implementation file

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

CBrush* pOldBrush;

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

// 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]);

pDC->FillRect(rcStatic, &brBackgnd);

return CStatic::OnEraseBkgnd(pDC);


Download demo project – 40 Kb

More by Author

Must Read