Click here to Skip to main content
Licence 
First Posted 2 Aug 2004
Views 88,654
Downloads 3,139
Bookmarked 44 times

Dialog Box with gradient background and Color changing progress bar

By Vikram Kashyap | 2 Aug 2004
A small dialog based application that allows the user to change the color of progress bar.
7 votes, 30.4%
1
1 vote, 4.3%
2
1 vote, 4.3%
3
3 votes, 13.0%
4
11 votes, 47.8%
5
2.63/5 - 23 votes
μ 2.63, σa 3.16 [?]

Sample Image - ProgDlg.jpg

Introduction

This article describes a very small dialog based application allowing to change the color of the progress bar; using PBM_SETBARCOLOR to change the color of the bars, and PBM_SETBKCOLOR to change the backcolor of the the progress bar. The dialog box is filled with gradient colored background. The application can be used to create some good looking setup application with gradient background, and colored progress bar showing the progress of the setup.

Have a look at the following code...

BOOL CProgDlgDlg::OnEraseBkgnd(CDC* pDC)
{
      CPen myPen[60] ;
      int i ;
      CRect rect ;
      for (i = 0 ; i <= 60 ; i++) 
      myPen[i].CreatePen(PS_SOLID, 1, RGB ((i * 4),0,0));
      CPen *oldPen = pDC->SelectObject(&myPen[0]) ;
      GetClientRect(&rect);
      for(i = 0 ; i <= rect.bottom;)
      {
           pDC->MoveTo(0, i);
           pDC->LineTo(rect.right, i);
           i++;
           pDC->SelectObject (&myPen[i * 64 / rect.bottom]);
       }
       pDC->SelectObject(oldPen) ;
       return TRUE ; 
}

As you can see, there is no magical trick here, it's simple drawing lines from top to bottom of the dialog box using a color. Here, first we created an array of CPen. I made my code to go through a for loop to create a solid brush with a particular color. (You can use all the three colors to create the gradient effect). Using GetClientRect(), area of dialog box has been taken. Using the CDC pointer, we are drawing lines from top to bottom. As we move downwards in the loop, the intensity of the color increases giving a gradient effect.

To change the color of the progress bar:

void CProgDlgDlg::OnBtnColor() 
{
     CColorDialog dlg;
     if(dlg.DoModal() == IDOK)
     {
          color = dlg.GetColor();
          m_myProgress.SendMessage(PBM_SETBARCOLOR, 0, color);
     }
}

Call standard color dialog box using CColorDialog's object. Check whether the user has pressed OK or Cancel button. Extract the color using GetColor() in a COLORREF object. Simply send the message to the progress bar using SendMessage with the message as the first parameter and selected color as the last parameter.

That's it, you are ready with a gradient colored dialog box, and color changing progress bar.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Vikram Kashyap

Technical Lead

India India

Member
Hi I am Vikram, did my computers from NIIT with excellent score. Passion for VC++/MFC and developing my own utilities. Loves to do a lot of R&D on registry. Working in VC++/MFC from the last 6 years with SQL and MS-Access database. Currently working as a Project Lead with an MNC in Gurgaon.
 
Got my articles posted on Codeguru.com
1. Open Six Sites in one Browser
2. Customized icon in Message Box
 
Articles On CodeProject
1. Lock WorkStation the MFC Way
2. Gradient Dialog with a Colored ProgressBar
3. Move Start Button

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Generaloutside array error PinmemberJan van Gelder1:22 11 Mar '11  
GeneralMy vote of 1 Pinmemberjoerg-schumann0:54 22 Jun '10  
GeneralOnEraseBkgnd(CDC* pDC) Pinmemberkmjackson7:18 6 May '09  
Questionhow to make different cell with different colors. Pinmemberpramod19790:20 6 Jul '07  
QuestionHow to change color PinmemberShah Satish7:04 1 Feb '07  
QuestionBackGround colour is overwrite on button Pinmemberlaymark19:32 30 Sep '06  
Generalmanifest problem Pinmemberrollocool4:50 5 Feb '05  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120210.1 | Last Updated 3 Aug 2004
Article Copyright 2004 by Vikram Kashyap
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid