CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 24

Thread: Progress bar

  1. #1
    Join Date
    Feb 2005
    Posts
    331

    Progress bar

    Hi,

    I'm trying to add a progress bar in one of my dialogs, that will step upon every iteration of a loop until completion. I've never played around with progress bars before, so I'm totally clueless as to where I should start. I've added a progress bar in resource view into my dialog, but it just shows up as an empty progress bar. I'd like to have the progress bar dynamically appear/update itself only when a button is clicked.

    Any idea what I need to do?

    Thanks.

  2. #2
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: Progress bar

    I'd like to have the progress bar dynamically appear/update itself only when a button is clicked.
    Something like : OnButtonClick do update progressbar ?

  3. #3
    Join Date
    Feb 2005
    Posts
    331

    Re: Progress bar

    Quote Originally Posted by Skizmo View Post
    Something like : OnButtonClick do update progressbar ?
    Exactly. OnButtonClick() will include a loop that does some operation, and at every iteration it will update the progressbar until completion.

    I've made the progressbar invisible, created the progressbar class in separate .cpp/.h files, as well as the OnNMCustomdrawProgress1() method by double clicking on the progressbar in resource view. I've also added a CProgressCtrl member variable. I'm unsure of what to do next in my OnButtonClick() to make the progressbar visible and start updating it.

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Progress bar

    If the c_progress is the member variable of your progress bar then:
    Code:
    c_progress.ShowWindow(SW_SHOW);  // show the control
    int nPos = ...; // calculate the progress bar position
    c_progress.SetPos(nPos); // set the position
    Victor Nijegorodov

  5. #5
    Join Date
    Feb 2005
    Posts
    331

    Re: Progress bar

    OK, stupid question:
    My ProgressBar class definitions are in CProgressBar.h/cpp. The actual progress bar however is displayed in MainDlg::OnButtonClick(). I've included CProgressBar.h in MainDlg.cpp, and c_progress is a public member variable. However, I'm still unable to access c_progress in MainDlg::OnButtonClick(). How do I access c_progress?

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Progress bar

    1. Could you show your ProgressBar class definitions, and how you are using the ProgressBar instance in the MainDlg.cpp/MainDlg.h?
    Victor Nijegorodov

  7. #7
    Join Date
    Feb 2005
    Posts
    331

    Re: Progress bar

    The ProgressBar class definitions are the default ones that are created when I use the Class Wizard to add a class in VS2005

    CProgressBar.cpp
    Code:
    // CProgressBar.cpp : implementation file
    //
    
    #include "stdafx.h"
    #include "GenKeys.h"
    #include "CProgressBar.h"
    
    
    // CProgressBar dialog
    
    IMPLEMENT_DYNAMIC(CProgressBar, CDialog)
    
    CProgressBar::CProgressBar(CWnd* pParent /*=NULL*/)
    	: CDialog(CProgressBar::IDD, pParent)
    {
    
    }
    
    CProgressBar::~CProgressBar()
    {
    }
    
    void CProgressBar::DoDataExchange(CDataExchange* pDX)
    {
    	CDialog::DoDataExchange(pDX);
    	DDX_Control(pDX, IDC_PROGRESS1, m_ProgressBar);
    }
    
    
    BEGIN_MESSAGE_MAP(CProgressBar, CDialog)
    	ON_NOTIFY(NM_CUSTOMDRAW, IDC_PROGRESS1, &CProgressBar::OnNMCustomdrawProgress1)
    END_MESSAGE_MAP()
    
    
    // CProgressBar message handlers
    
    void CProgressBar::OnNMCustomdrawProgress1(NMHDR *pNMHDR, LRESULT *pResult)
    {
    	LPNMCUSTOMDRAW pNMCD = reinterpret_cast<LPNMCUSTOMDRAW>(pNMHDR);
    	// TODO: Add your control notification handler code here
    	*pResult = 0;
    }
    CProgressBar.h
    Code:
    #pragma once
    #include "afxcmn.h"
    
    
    // CProgressBar dialog
    
    class CProgressBar : public CDialog
    {
    	DECLARE_DYNAMIC(CProgressBar)
    
    public:
    	CProgressBar(CWnd* pParent = NULL);   // standard constructor
    	virtual ~CProgressBar();
    
    // Dialog Data
    	enum { IDD = IDD_GENKEYS_DIALOG };
    
    protected:
    	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
    
    	DECLARE_MESSAGE_MAP()
    public:
    	afx_msg void OnNMCustomdrawProgress1(NMHDR *pNMHDR, LRESULT *pResult);
    	CProgressCtrl m_ProgressBar;
    };
    In MainDlg.cpp, I simply tried to access m_ProgressBar but I get an undeclared identifier build error.

  8. #8
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Progress bar

    1. How and where do you create this CProgressBar dialog? I see neither Create nor DoModal calls for it.
      Note that your progress bar can only exist after the CProgressBar dialog has been created.
    2. I see you are using CProgressBar::OnNMCustomdrawProgress1 to draw the control. But there is no implementation for drawing! So the control will be invisible!?
    Victor Nijegorodov

  9. #9
    Join Date
    Feb 2005
    Posts
    331

    Re: Progress bar

    There is not CProgressBar dialog. My progress bar is created in the main dialog(MainDlg) which is created and displayed once the program runs. In resource view it's named IDC_PROGRESS1.

    Ignore the OnNMCustomdrawProgress1. It was simply created when I double clicked on the progress bar in resource view. I don't know what it does or what to do with it. I'm simply looking for a way to implement what I have described in my original post.

  10. #10
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Progress bar

    Quote Originally Posted by galapogos View Post
    There is not CProgressBar dialog. My progress bar is created in the main dialog(MainDlg) which is created and displayed once the program runs. In resource view it's named IDC_PROGRESS1.
    So, the code snippet you have posted has nothing to do with your original code / original problem?
    Then why did you post it? You should have rather posted your actual code used in the main dialog(MainDlg)...
    Victor Nijegorodov

  11. #11
    Join Date
    Feb 2005
    Posts
    331

    Re: Progress bar

    As I've mentioned in the original posts, I drag and dropped a progress bar in resource view from the toolbox into my main dialog. I then created the CProgressBar class by right clicking on the new progress bar in resource view, and chose "Add Class". This created all the code snippet that I just posted upon your request.

    I also double clicked on the progress bar in resource view, which then created the OnNMCustomdrawProgress1.

    Lastly, I created a public member variable in CProgressBar.

    There is nothing in MainDlg that is specific to CProgressBar, because I haven't found a way to access the C_ProgressBar member variable despite including the header file. However, that is where I wish to access it in order to make it visible and start to update it.

    I hope this is clear enough.

    Now, how do I do that?

    Thanks.

  12. #12
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Progress bar

    No, it is absolutely not clear:
    • why you created this additional class CProgressBar (derived from the CDialog!) which is neither needed nor used (since your progressbar belongs to the MainDlg;
    • why you created and posted this OnNMCustomdrawProgress1 message handler despite you are not going to use it:
    • why you created the CProgressCtrl m_ProgressBar ("public member variable in CProgressBar") while progress bar you are going to use belongs to the MainDlg, not the CProgressBar class;
    Victor Nijegorodov

  13. #13
    Join Date
    Feb 2005
    Posts
    331

    Re: Progress bar

    In that case, disregard what I did completely, and please tell me exactly how to get the functionality that I described in my original post.

    Thanks.

  14. #14
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Progress bar

    1. Add progress control to your main dialog (use resource editor to do it)
    2. Add control member variable for the progress control in the main dialog class.
    3. Read about Using CProgressCtrl in MSDN.
    4. Download and learn the Microsoft samples CMNCTRL2 and FIRE
    Victor Nijegorodov

  15. #15
    Join Date
    Feb 2005
    Posts
    331

    Re: Progress bar

    1. Done
    2. Can't seem to do it. Right clicked on the progress control in resource editor and "Add Variable" is grayed out.
    3-4. Will do.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured