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


  • Please.. please..

    Posted by Hakan Bulut on 06/24/2013 05:02am

    Please, remove this article.. and close this ass door.

  • In what time? 2008? 2001? 2009? What the difference?

    Posted by Hakan Bulut on 01/12/2008 04:49pm

    When these codes write would be curious about?

    • Please

      Posted by Hakan BULUT on 06/24/2013 02:10am

      Please close this door.

  • Use Wm_CtlColor

    Posted by Legacy on 05/15/2002 12:00am

    Originally posted by: Eric Schmidt

    I used OnCtlColor which was much easier than creating a static box class

    // CColorChooserDlg message handlers

    HBRUSH CColorChooserDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
    HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

    if (pWnd == GetDlgItem(IDC_STATIC_PAINT))
    CRect rcStatic;
    CDC *pDlgDc = m_stcBox.GetDC();
    CBrush* pOldBrush;

    CBrush brBack(RGB(m_nColors[0],m_nColors [1],m_nColors[2]));
    pDlgDc->FillRect(rcStatic, &brBack);
    pOldBrush = pDlgDc->SelectObject(&brBack);

    return hbr;

    • Good idea..

      Posted by Hakan Bulut on 01/12/2008 04:07pm

      Very good.

  • heh...

    Posted by Legacy on 04/21/2002 12:00am

    Originally posted by: g.sharp

    it was so amusing to read everyone getting all serious about code like this... i just had to say something

    • So you say

      Posted by Hakan Bulut on 01/12/2008 04:10pm

      You may say as you wanted.

  • You've missed the point...

    Posted by Legacy on 03/09/2002 12:00am

    Originally posted by: Ryan Binns

    How many times have you borrowed someone else's code and not had to modify it, even one tiny little bit??? I have used a fair amount of code from this site, and I have never had one bit of code that I didn't have to change to suit my application.

    This code is obviously not going to suit every situation, but it works perfectly for the situation it was designed for. If you make sure that the slider control IDs are defined consecutively, then there will be no problem at all.

    If you "criticisers" are not smart enough to work this out, then I suggest you take your comments elsewhere, rather than abusing someone who wants to share some of their code with other people.

    • The answer of this unrequired polemic.

      Posted by Hakan Bulut on 01/13/2008 10:30am

      I'm 33 years old!. If you are just as allright?

    • Thank you

      Posted by Hakan Bulut on 01/12/2008 04:13pm

      Thanks for carriage. It works very fast.

  • Thanks...

    Posted by Legacy on 03/08/2002 12:00am

    Originally posted by: Hakan

    Thanks to Andy because of this good comment. Thanks as well to Mr. Bruce. I know, this just the first day's homework of Visual C++ programming class. I didn't say "I am a professional". For Painter: Sorry pal but I think of this is fastest and simple the code way to control 3 slider controls, at the same time. I want to note something down:
    The important line of this function is this? (int i = pSlider->GetDlgCtrlID () - IDC_SLIDER1;)
    If you ask to this question that means you must obtain information about GetDlgCtrlID() Please take a look at the MSDN for "GetDlgCtrlID()"..
    By the way, the greatest thanks go to Mr. Rick Wallach who support me morally..
    Yours faithfully

  • Thanks..

    Posted by Legacy on 03/07/2002 12:00am

    Originally posted by: Anoop

    Sometimes some code is not submitted just to prove something, but to show how something can be done.

    • At first!

      Posted by Hakan Bulut on 01/13/2008 10:47am

      We can be a people who have an idea of what is the people and a people is what mean that in the humanity history. We can be ask a question to ourselves in our minds, so could i be a people? Some take a look at the back whom are there in this world and whom are came to the world. I am so sorry that because of the putting up this bad things and the bad position in which completely oputside of the humanity.

    • I wont reguest from you feeling shame!.

      Posted by Hakan Bulut on 01/13/2008 10:35am

      This is just to be brazen-faced!. If I am going to go to put something down!...

    • Anyway

      Posted by Hakan Bulut on 01/12/2008 04:44pm

      All posts are looks like a game piece.

    • I have been coming more.

      Posted by Hakan Bulut on 01/12/2008 04:40pm

      How much more can i come? To be rescued from this LOL position.

    • Yes

      Posted by Hakan Bulut on 01/12/2008 04:31pm

      Because the Visual C++ Resource editor has been counted it automatically.

    • Yes so.

      Posted by Hakan Bulut on 01/12/2008 04:27pm

      You are right. I think so too alltime.

    • Sorry pal

      Posted by Hakan Bulut on 01/12/2008 04:24pm

      There is something in this life but no left choice to coming success. This was the one of that things.

    • LOL?

      Posted by Hakan Bulut on 01/12/2008 04:21pm

      PLEASE, I WANT TO DRAW THE ATTENTION IN THAT DOT. I am not taking a clue if the life is to take only a clue. I can not take it anymore this is impossible.

  • Ahh...I'm sorry but what's the point of this code

    Posted by Legacy on 03/06/2002 12:00am

    Originally posted by: painter

    I'm sorry but this looks like the first day's homework of Visual C++ programming class...
    and this line -
    int i = pSlider->GetDlgCtrlID () - IDC_SLIDER1;

    has huge possibilities to produce bug.

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Moving from an on-premises environment to Office 365 does not remove the need to plan for disruptions or reduce the business risk requirements for protecting email services. If anything, some risks increase with a move to the cloud. Read how to ease the transition every business faces if considering or already migrating to cloud email. This white paper discusses: Setting expectations when migrating to Office 365 Understanding the implications of relying solely on Exchange Online security Necessary archiving …

  • Anthony Christie, the Chief Marketing Officer for Level Communications, is responsible for customer experience, worldwide marketing and product management. In this informative asset, he shares his insights into why a private network connection to cloud-bases applications is the right decision for your enterprise. Download now to find out more.

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date