-
April 13th, 2011, 03:08 AM
#1
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.
-
April 13th, 2011, 03:13 AM
#2
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 ?
-
April 13th, 2011, 03:28 AM
#3
Re: Progress bar
Originally Posted by Skizmo
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.
-
April 13th, 2011, 03:45 AM
#4
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
-
April 13th, 2011, 03:51 AM
#5
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?
-
April 13th, 2011, 03:58 AM
#6
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
-
April 13th, 2011, 04:04 AM
#7
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.
-
April 13th, 2011, 04:12 AM
#8
Re: Progress bar
- 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. - 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
-
April 13th, 2011, 04:17 AM
#9
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.
-
April 13th, 2011, 04:25 AM
#10
-
April 13th, 2011, 04:34 AM
#11
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.
-
April 13th, 2011, 04:50 AM
#12
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
-
April 13th, 2011, 04:51 AM
#13
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.
-
April 13th, 2011, 05:01 AM
#14
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
-
April 13th, 2011, 05:04 AM
#15
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|