Design and Implementation of the status bar

Source: Internet
Author: User

The status bar is actually a window, generally divided into several panes, each pane shows different information. Appwizard automatically creates a status bar for the application. the status bar consists of several panes to display the status bar prompt and the status of the Caps Lock, num lock, and scroll lock keys. In MFC, the status bar function is implemented by the cstatusbar class. To create a status bar, follow these steps: cstatusbar object. Cstatusbar: Create create status bar window. Cstatusbar: setindicators function distribution pane, and associate each pane of the status bar with a string ID.

 

 

Build

Call

Call

The corresponding code can be found in the cmainframe: oncreate member function of the record project. As shown in listing 4.6....... The first indicators parameter of the function is an ID array, which can be found at the beginning of the CPP file where the cmainframe class is located, as shown in listing 4.7.The array provides the distribution information of the status bar pane. The first item is generally id_separator. The corresponding pane of this ID is used to display the command prompt information. The last three items are string IDs, you can find the three strings cap, num, and scrl in the string table string resource. The three corresponding panes are used to display the keyboard status.

 

Listing 4.6 creating a status bar

 

If (! M_wndstatusbar.create (this) |

! M_wndstatusbar.setindicators (indicators, sizeof (indicators)/sizeof (uint )))

{

Trace0 ("failed to create status bar/N ");

Return-1; // fail to create

}

 

Setindicators

List 4.7 ID Array

Static uint indicators [] =

{

Id_separator, // status line indicator

Id_indicator_caps,

Id_indicator_num,

Id_indicator_scrl,

};

  

 

Indicator

Now let's add a time pane for the status bar to display the system time. The displayed format is hh: mm: SS, instant: minute: second.

Insert an id named id_indicator_clock after the id_separator entry in the indicators array. Find and double-click the string resource named string table to open the string resource editing window. Then, press the Insert key in the edit window to insert a new string. Please specify the string ID as id_indicator_clock and the content is 00:00:00. The status bar determines the default width of the corresponding pane based on the length of the string. Therefore, if it is set to 00:00:00, space is reserved for the display of the time. 

Tip: The preceding method cannot dynamically change the width of the pane and is sometimes inaccurate. When the system font changes, this may cause some errors. Considering that this method is simple and intuitive, and generally it is not a problem, we will use it as an example. If you are interested in specifying the pane dynamically and accurately, see the npp mfc example provided by Visual C ++ 5.0 on the CD (under the samples/mfc/General/NPP directory ).

 

The time displayed in the time pane must be updated every second. You can call the cstatusbar: setpanetext function to update the body of the update time pane. to update the body regularly, use the wm_timer message. In Windows, you can install one or more timers. The timer automatically sends out a wm_timer message at a certain interval, which can be specified by the user. The window class of MFC provides the wm_timer message processing function ontimer. We should update the time pane in this function.

Please use classwizard to add the message processing functions ontimer of wm_timer to the cmainframe class and onclose of the message processing functions of wm_close. The specific method is to select cmainframe in the class name column, select cmainframe in the object IDs column, find the wm_timer and wm_close items in the messages column, double-click them, and press OK to exit classwizard. Cmainframe: The onclose function is called when the main frame window is closed. The program can clear some work in the function. Next, modify the program according to configuration 4.8....

 

Part of the code of the cmainframe class in listing 4.8

Int cmainframe: oncreate (maid)

{

 

 

Settimer (, null); In the cmainframe: oncreate function, cwnd: settimer is called to install a timer. The first parameter of settimer specifies that the timer ID is 1, the second parameter specifies the timer interval of 1000 ms, that is, 1 second. In this way, the ontimer function will be called once every one second.

Return 0;

}

Void cmainframe: ontimer (uint nidevent)

{

// Todo: add your message handler code here and/or call default

 

Ctime time;

Time = ctime: getcurrenttime ();

Cstring S = time. Format ("% H: % m: % s ");

M_wndstatusbar.setpanetext (m_wndstatusbar.commandtoindex (id_indicator_clock), S );

Cframewnd: ontimer (nidevent );

}

Void cmainframe: onclose ()

{

// Todo: add your message handler code here and/or call default

 

Killtimer (1 );

Cframewnd: onclose ();

}

 

In the ontimer function, a ctime object is constructed first, and then the static member function getcurrenttime of ctime is called to obtain the current system time. Then, a time: minute is returned using the ctime: Format function: second format, and finally call cstatusbar: setpanetext to update the text displayed in the time pane. The first parameter of setpanetext is the index of the pane. For a pane ID, you can use cstatusbar: commandtoindex to obtain the index.

The timer should be closed when you cancel the main frame window. Therefore, the killtimer function is called in the cmainframe: onclose function.

Now let's take a look at the message ing of cmainframe. In the beginning of the CPP file where the cmainframe class is located, we can find the message ing of this class, as shown in listing 4.9.

Configuration 4.9

Begin_message_map (cmainframe, cframewnd)

// {Afx_msg_map (cmainframe)

On_wm_create ()

On_command (id_record_stop, onrecordstop)

On_command (id_record_start, onrecordstart)

On_update_command_ui (id_record_start, onupdaterecordstart)

On_update_command_ui (id_record_stop, onupdaterecordstop)

On_command (id_high_quality, onhighquality)

On_command (id_low_quality, onlowquality)

On_update_command_ui (id_high_quality, onupdatehighquality)

On_update_command_ui (id_low_quality, onupdatelowquality)

On_command (id_view_toolbar1, onviewtoolbar1)

On_update_command_ui (id_view_toolbar1, onupdateviewtoolbar1)

On_wm_timer ()

On_wm_close ()

//} Afx_msg_map

End_message_map ()

 

 

As you can see, in the message ing table, classwizard automatically adds message ing to message processing functions and command processing functions. The automatically added part is displayed in gray, which is located between the comments line // {afx_msg_map and //} afx_msg_map. The on_command macro maps the command processing functions, and the on_update_command_ui maps the command update processing functions, while the WM _ message processing functions map the on_wm _ message macro.Compile and run record to view the new changes in the status bar. The final page is shown in Figure 4.8.

Tip: If you see the comments of // {afx _... in the future, it means that the classwizard is automatically added between them, and this part is displayed in gray. Do not modify them at will, or place the manually added parts in the/{afx _... annotation field. Otherwise, classwizard errors may occur.

 

 

Figure 4.8 Final Record Program

 

KnotThis chapter mainly introduces some practical technologies of tool bar and status bar. Key points are as follows:

In

Create a toolbar and a status bar in

In MFC, creating a window is generally divided into two steps: 1. Build a window object. The construction method is to define an object or dynamically create it using the new operator. 2. Call the create member function of the window class. This function makes the actual window and stores its hwnd in m_hwnd, a public data member of the window. Cmainframe: completed in the oncreate function. The oncreate function is called when the window is created. At this time, the window is partially created, and the hwnd handle of the window object is also valid, but the window is invisible. Therefore, the oncreate function is generally used for initialization such as creating subwindows.

Afx_msg

Tool bar has two elements: tool bar resource and tool bar class

If you do not define a command handler or update a handler for a command, the Framework automatically disables the user interface object (mainly menu items and buttons) corresponding to the command. Exploitation

Before the menu drop-down, or when the toolbar button is in an idle loop,

Call

To insert a new pane in the status bar, you must

Call

The prefix ensures that message processing functions of the correct version are called. Ctoolbar. If you only need one toolbar, you can use Appwizard to automatically generate it and then modify it. You must create multiple toolbar manually. Classwizard allows you to easily add command processing functions and command update processing functions. MFC will issue an update command, which will cause the command to update the handler function call. The command update handler uses the ccmdui class to update user interface objects. Calling ccmdui: Enable allows or disables user interface objects. Calling ccmdui: setcheck allows or disables user interface objects. Cwnd: showwindow can hide/display a window. Insert a new string ID in the indicator array. The status bar determines the default width of the new pane based on the length of the string. Cstatusbar: setpanetext updates the text displayed in the status bar pane.

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.