I recently made a C++/MFC program, this program sometimes to load large files, in order to make the file loading process is not too monotonous, I want to use a progress indicator in the UI to display the file loading process, and I want to be in the program's status bar using this indicator control. After some research and try, I realized my own idea. This article will describe the entire implementation process in detail. I hope we can take some detours when solving similar problems ...
Although MFC provides standard progress indicator controls (progress control) but cannot use this control directly in the status bar, I created my own reusable C + + class to implement progress instructions. This class derives from CStatusBar. The whole implementation process is not difficult, the idea is to create a progress indicator control in the status bar, treat it as a child window, and then display or hide the progress indicator according to the different states. This article provides an example program Pgrsbar, which uses the MFC document/view architecture to display text files in edit view. When you open the file, Pgrsbar the simulation for a long loading process and displays progress instructions in the status bar, as shown in figure one. I encapsulated this status bar with a progress indicator in a CStatusBar derived class--cprogstatusbar.
Figure one shows progress instructions in the status bar
The following is a detailed description of the class and how to use it:
Cprogstatusbar is derived from the standard MFC class CStatusBar. I added a CProgressCtrl type of data member--m_wndprogbar to the Cprogstatusbar derived class and implemented three important member functions or methods: OnCreate, OnSize, and OnProgress. The following is a detailed description of these three functions:
OnCreate is responsible for receiving control when the status bar is first created, and then creating a progress indicator and initializing it as a child window.
int CProgStatusBar::OnCreate(LPCREATESTRUCT lpcs)
{
lpcs->style |= WS_CLIPCHILDREN;
VERIFY(CStatusBar::OnCreate(lpcs)==0);
VERIFY(m_wndProgBar.Create(WS_CHILD, CRect(), this, 1));
m_wndProgBar.SetRange(0,100);
return 0;
}
OnCreate adds a ws_clipchildren to the style of the status bar, which tells Windows not to draw the status bar area below the child window, which can reduce the screen flicker. Then OnCreate creates a progress indicator control and sets its range to [0,100]. Note that there is no ws_visible when creating a progress indicator control here, because I want to hide it at the beginning of the program.