Announcement

Collapse
No announcement yet.

Changing a progress bar color in Windows 7

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Changing a progress bar color in Windows 7

    I am trying to change the color of a progress bar in Windows 7. The default seems to be green, and I want to change it to red under certain circumstances.

    Below is the entire contents of a simple program with one dialog and a progress bar. I created it in PBForms, and then added the code to change the bar color. Even so, when it runs the bar still comes up green. Others who have tried this code on a Vista machine says it works, with the bar becoming red.

    I don’t know what's different about Windows 7, but I would at least like to understand why it doesn't under that O/S when it seems to with others.

    Thanks for any help you can offer.

    Cordell Jeppsen

    -----------------------------------------------

    #PBFORMS CREATED V2.01
    '------------------------------------------------------------------------------
    ' The first line in this file is a PB/Forms metastatement.
    ' It should ALWAYS be the first line of the file. Other
    ' PB/Forms metastatements are placed at the beginning and
    ' end of "Named Blocks" of code that should be edited
    ' with PBForms only. Do not manually edit or delete these
    ' metastatements or PB/Forms will not be able to reread
    ' the file correctly. See the PB/Forms documentation for
    ' more information.
    ' Named blocks begin like this: #PBFORMS BEGIN ...
    ' Named blocks end like this: #PBFORMS END ...
    ' Other PB/Forms metastatements such as:
    ' #PBFORMS DECLARATIONS
    ' are used by PB/Forms to insert additional code.
    ' Feel free to make changes anywhere else in the file.
    '------------------------------------------------------------------------------

    #COMPILE EXE
    #DIM ALL

    '------------------------------------------------------------------------------
    ' ** Includes **
    '------------------------------------------------------------------------------
    #PBFORMS BEGIN INCLUDES
    #RESOURCE "ProgressBarTest.pbr"
    %USEMACROS = 1
    #INCLUDE ONCE "WIN32API.INC"
    #INCLUDE ONCE "COMMCTRL.INC"
    #INCLUDE ONCE "PBForms.INC"
    #PBFORMS END INCLUDES
    '------------------------------------------------------------------------------

    '------------------------------------------------------------------------------
    ' ** Constants **
    '------------------------------------------------------------------------------
    #PBFORMS BEGIN CONSTANTS
    %IDD_DIALOG1 = 101
    %IDC_PROGRESSBAR1 = 1001
    #PBFORMS END CONSTANTS
    '------------------------------------------------------------------------------

    '------------------------------------------------------------------------------
    ' ** Declarations **
    '------------------------------------------------------------------------------
    DECLARE CALLBACK FUNCTION ShowDIALOG1Proc()
    DECLARE FUNCTION SampleProgress(BYVAL hDlg AS DWORD, BYVAL lID AS LONG) AS _
    LONG
    DECLARE FUNCTION ShowDIALOG1(BYVAL hParent AS DWORD) AS LONG
    #PBFORMS DECLARATIONS
    '------------------------------------------------------------------------------

    '------------------------------------------------------------------------------
    ' ** Main Application Entry Point **
    '------------------------------------------------------------------------------
    FUNCTION PBMAIN()
    PBFormsInitComCtls (%ICC_WIN95_CLASSES OR %ICC_DATE_CLASSES OR _
    %ICC_INTERNET_CLASSES)

    ShowDIALOG1 %HWND_DESKTOP
    END FUNCTION
    '------------------------------------------------------------------------------

    '------------------------------------------------------------------------------
    ' ** CallBacks **
    '------------------------------------------------------------------------------
    CALLBACK FUNCTION ShowDIALOG1Proc()

    SELECT CASE AS LONG CB.MSG
    CASE %WM_INITDIALOG
    ' Initialization handler

    CASE %WM_NCACTIVATE
    STATIC hWndSaveFocus AS DWORD
    IF ISFALSE CB.WPARAM THEN
    ' Save control focus
    hWndSaveFocus = GetFocus()
    ELSEIF hWndSaveFocus THEN
    ' Restore control focus
    SetFocus(hWndSaveFocus)
    hWndSaveFocus = 0
    END IF

    CASE %WM_COMMAND
    ' Process control notifications
    SELECT CASE AS LONG CB.CTL
    CASE %IDC_PROGRESSBAR1

    END SELECT
    END SELECT
    END FUNCTION
    '------------------------------------------------------------------------------

    '------------------------------------------------------------------------------
    ' ** Sample Code **
    '------------------------------------------------------------------------------
    FUNCTION SampleProgress(BYVAL hDlg AS DWORD, BYVAL lID AS LONG) AS LONG
    PROGRESSBAR SET RANGE hDlg, lID, 0, 100
    PROGRESSBAR SET POS hDlg, lID, 30
    END FUNCTION
    '------------------------------------------------------------------------------

    '------------------------------------------------------------------------------
    ' ** Dialogs **
    '------------------------------------------------------------------------------
    FUNCTION ShowDIALOG1(BYVAL hParent AS DWORD) AS LONG
    LOCAL lRslt AS LONG

    #PBFORMS BEGIN DIALOG %IDD_DIALOG1->->
    LOCAL hDlg AS DWORD

    DIALOG NEW hParent, "Dialog1", 374, 115, 201, 120, %WS_POPUP OR _
    %WS_BORDER OR %WS_DLGFRAME OR %WS_SYSMENU OR %WS_CLIPSIBLINGS OR _
    %WS_VISIBLE OR %DS_MODALFRAME OR %DS_3DLOOK OR %DS_NOFAILCREATE OR _
    %DS_SETFONT, %WS_EX_CONTROLPARENT OR %WS_EX_LEFT OR _
    %WS_EX_LTRREADING OR %WS_EX_RIGHTSCROLLBAR, TO hDlg
    CONTROL ADD PROGRESSBAR, hDlg, %IDC_PROGRESSBAR1, "ProgressBar1", 25, 10, _
    145, 30
    #PBFORMS END DIALOG

    SampleProgress hDlg, %IDC_PROGRESSBAR1
    CONTROL SEND hDLG, %IDC_ProgressBar1, %PBM_SETBARCOLOR, 0, %RGB_RED

    DIALOG SHOW MODAL hDlg, CALL ShowDIALOG1Proc TO lRslt

    #PBFORMS BEGIN CLEANUP %IDD_DIALOG1
    #PBFORMS END CLEANUP

    FUNCTION = lRslt
    END FUNCTION

  • #2
    When you use a manifest with apps so they use themes, Windows uses a different set of controls with different features. For example the progress bar no longer supports unique colors. The API messages which can be used to set the colors only work for a non-themed progressbar. For example %PBM_SETBARCOLOR does not work on a themed progressbar.

    You can turn off theming for any control if you want, but you also lose some of the nice effects of the themed control.
    Chris Boss
    Computer Workshop
    Developer of "EZGUI"
    http://cwsof.com
    http://twitter.com/EZGUIProGuy

    Comment


    • #3
      To set green, yellow or red color on a themed progress bar, use PBM_SETSTATE...
      Code:
       
      'DDT:
      CONTROL SEND hDLG, %Progressbar, %PBM_SETSTATE, %PBST_NORMAL, 0) 'Green - In progress
      CONTROL SEND hDLG, %Progressbar, %PBM_SETSTATE, %PBST_PAUSED, 0) 'Yellow - Paused
      CONTROL SEND hDLG, %Progressbar, %PBM_SETSTATE, %PBST_ERROR, 0)  'Red    - Error
       
      Or...
      SendDlgItemMessage(hDlg, %Progressbar, %PBM_SETSTATE, %PBST_NORMAL, 0) 'Green In progress When visual styles are enabled
      SendDlgItemMessage(hDlg, %Progressbar, %PBM_SETSTATE, %PBST_PAUSED, 0) 'Yellow Paused     When visual styles are enabled
      SendDlgItemMessage(hDlg, %Progressbar, %PBM_SETSTATE, %PBST_ERROR, 0)  'Red Error         When visual styles are enabled
      You can keep the PBM_SETBARCOLOR statement in parallel so your program will work with choosen colors in both themed and non-themed environnement.
      >>> CONTROL SEND hDLG, %IDC_ProgressBar1, %PBM_SETBARCOLOR, 0, %RGB_RED

      Pierre

      Code:
       
      'RC File named as ProgressBarDemo.rc
      'RC File: #include "resource.h"
      'RC File: #define IDR_MANIFEST 1
      'RC File: #define RT_MANIFEST 24
      'RC File:
      'RC File: IDR_MANIFEST RT_MANIFEST MOVEABLE PURE
      'RC File: {
      'RC File:     "<assembly xmlns=""urn:schemas-microsoft-com:asm.v1"" manifestVersion=""1.0"">
      'RC File:        <assemblyIdentity
      'RC File:          name=""Microsoft.Windows.Shell.shell32""
      'RC File:          processorArchitecture=""x86""
      'RC File:          version=""1.50.00.01""
      'RC File:          type=""win32""/>
      'RC File:        <description>Windows Shell</description>
      'RC File:        <dependency>
      'RC File:          <dependentAssembly>
      'RC File:            <assemblyIdentity
      'RC File:              type=""win32""
      'RC File:              name=""Microsoft.Windows.Common-Controls""
      'RC File:              version=""6.0.0.0""
      'RC File:              processorArchitecture=""x86""
      'RC File:              publicKeyToken=""6595b64144ccf1df""
      'RC File:              language=""*""
      'RC File:            />
      'RC File:          </dependentAssembly>
      'RC File:        </dependency>
      'RC File:      </assembly>"
      'RC File: }
       
      #COMPILE EXE '#8.04#
      #INCLUDE "Win32Api.inc"
      #INCLUDE "CommCtrl.inc"
      #RESOURCE "ProgressBarDemo.pbr"
       
      %Button         = 101
      %Progressbar    = 201
      %PBST_NORMAL    = 1
      %PBST_ERROR     = 2
      %PBST_PAUSED    = 3
      %PBS_MARQUEE    = 8 'Need manifest
      %PBM_SETMARQUEE = %WM_USER + 10
      %PBM_SETSTATE   = %WM_USER + 16
       
      GLOBAL hDlg  AS LONG
      '______________________________________________________________________________
       
      FUNCTION ProgressBarThread(BYVAL Something AS DWORD) AS LONG
       LOCAL RetVal AS LONG
       LOCAL Looper AS LONG
       
       SendDlgItemMessage(hDlg, %Progressbar, %PBM_SETSTATE, %PBST_NORMAL, 0)  'Green In progress When visual styles are enabled
       SendMessage(GetDlgItem(hDlg, %Progressbar), %PBM_SETBARCOLOR, 0, %GREEN)'When visual styles are disabled
       FOR Looper = 1 TO 101
         IF Looper = 50 THEN
           SendDlgItemMessage(hDlg, %Progressbar, %PBM_SETSTATE, %PBST_PAUSED, 0) 'Yellow Paused When visual styles are enabled
           SendDlgItemMessage(hDlg, %Progressbar, %PBM_SETSTATE, %PBST_ERROR, 0)  'Red Error When visual styles are enabled
           SendMessage(GetDlgItem(hDlg, %Progressbar), %PBM_SETBARCOLOR, 0, %RED) 'When visual styles are disabled
         END IF
         SendDlgItemMessage(hDlg, %Progressbar, %PBM_SETPOS, Looper, 0)
         SLEEP 100
       NEXT
       SendDlgItemMessage(hDlg, %Progressbar, %PBM_SETSTATE, %PBST_PAUSED, 0)  'When visual styles are enabled
       SendMessage(GetDlgItem(hDlg, %Progressbar), %PBM_SETBARCOLOR, 0, %YELLOW)  'When visual styles are disabled
       
       CONTROL ENABLE hDlg, %Button
       
      END FUNCTION
      '______________________________________________________________________________
       
      CALLBACK FUNCTION DlgProc
       LOCAL  RetVal    AS LONG
       LOCAL  hThread   AS DWORD
       
       SELECT CASE CBMSG
       
         CASE %WM_COMMAND
           SELECT CASE LOWRD(CBWPARAM)
             CASE %Button
               IF CBCTLMSG = %BN_CLICKED OR CBCTLMSG = 1 THEN
                 CONTROL DISABLE hDlg, %Button
                 THREAD CREATE ProgressBarThread(BYVAL RetVAl) TO hThread
                 THREAD CLOSE hThread TO RetVal
               END IF
           END SELECT
       
       END SELECT
       
      END FUNCTION
      '______________________________________________________________________________
       
      FUNCTION PBMAIN () AS LONG
       
       DIALOG NEW %HWND_DESKTOP, "ProgressBar color demo",,, 160, 90, %WS_SYSMENU, 0 TO hDlg
       
       CONTROL ADD BUTTON, hDlg, %Button, "&Test", 84, 52, 50, 14, %BS_DEFAULT
       
       CONTROL ADD "MSCTLS_PROGRESS32", hDlg, %Progressbar, "", 22, 22, 108, 16, _
       %WS_VISIBLE OR %WS_CHILD OR %PBS_SMOOTH 'OR %PBS_MARQUEE '%PBS_MARQUEE need manifest
       
       SendDlgItemMessage(hDlg, %Progressbar, %PBM_SETRANGE, 0, MAKLNG(0, 100))
       
       SendDlgItemMessage(hDlg, %Progressbar, %PBM_SETSTEP,  1, 0)
       
       DIALOG SHOW MODAL hDlg CALL DlgProc
       
      END FUNCTION
      '______________________________________________________________________________
      '
      Last edited by Pierre Bellisle; 21 May 2012, 04:39 PM.

      Comment


      • #4
        Thanks for this. This solves my immediate problem! One question, though: What if I want to use colors other than green, yellow, and red? I see constants defined for Normal, Paused and Error in comctrl.inc, but those seem to be the only choices I have. What if I wanted blue?

        Comment


        • #5
          Cordell,

          If you are using a themed progress bar, only those 3 colors are available...

          Pierre

          Comment


          • #6
            Cordell,

            Börje wrote a really beautiful 3D progress bar custom control... 3D Progressbar, it's on Börje Files's site.

            3D, seven colors, vertical/horizontal, optionnal text, up side down text option, etc...

            This may be something you want to use...

            Pierre
            Last edited by Pierre Bellisle; 27 May 2012, 05:24 PM.

            Comment


            • #7
              Thank you so much for this help. These resources made my day!

              Comment


              • #8
                Great, I'm glad to help...

                Comment


                • #9
                  I think you can disable themes for a particular control, which would give you "manual" control of it.

                  Of course, whatever you are doing which will look good with one theme might look like a bad bowling shirt with another.

                  Comment


                  • #10
                    While we're on the topic of progress bars. I have been keeping an eye out for awhile on how to get the app icon that is located on the task bar (in Windows 7) to show the progress state much like FireFox does for downloads as well as many other apps.

                    Does anyone know which API to use to get the app-icon progress to display? I've searched briefly at MSDN, but this hasn't really been a high priority issue for me, so I just thought I'd ask here in case anyone knows off the top of their head.
                    Scott Slater
                    Summit Computer Networks, Inc.
                    www.summitcn.com

                    Comment


                    • #11
                      Hey Scott,
                      Try this link, "Caption Progress Icon"

                      .... whoops, you said TaskBar, not Caption icon.

                      But I seem to remember some posts on this - some I made, asking about it. I'll go look for what I remember ...

                      ... found it ... this link, Animated Taskbar Icon, that I posted a while back.

                      ... and adding again ....

                      even more information at this link.
                      Last edited by Gary Beene; 22 May 2012, 12:53 PM.

                      Comment


                      • #12
                        Thanks Gary,

                        I'll take a look at it.
                        Scott Slater
                        Summit Computer Networks, Inc.
                        www.summitcn.com

                        Comment

                        Working...
                        X