MFC custom Toolbar

Source: Internet
Author: User
Document directory
  • Method 1:
  • Method 2:
  • Method 3:
  • Method 4:
  • Method 5:

A good software interface is very important, and its functions should be clear to the customer. These operations can be completed by means of the toolbar, such as the toolbar in Microsoft Word. Next, I will write something I know about the toolbar in VC and share it with you. If not, please help us to correct it.

The following uses a single document/view program under Visual studio.net 2003 as an example.

In vs, the tool bar class is ctoolbar. When a single document/view program is created, the system automatically creates a tool bar in oncreate of the framework class. As follows:

If (! M_wndtoolbar.createex (this, tbstyle_flat, ws_child | ws_visible | cbrs_top | cbrs_gripper | cbrs_tooltips | cbrs_flyby | cbrs_size_dynamic) |

! M_wndtoolbar.loadtoolbar (idr_mainframe ))
{
Trace0 ("Toolbar \ n not created ");
Return-1; // creation failed
}

Method 1:

To modify or add the content in the toolbar, go to "resource view", select "toolbar", and modify or add the content in the toolbar, and add response messages for them. This is the simplest and sometimes the most direct and effective method.

Method 2:

If you want to create a toolbar that meets your requirements and does not want to be restricted by the toolbar in the resource view, you can write a program to create a toolbar. in this case, you only need to comment out the code automatically generated by the above project, and then write a function by yourself. The name here is createextoolbar (), and use this function to replace the above Code.

Then add your own special toolbar code. Of course, you need to create a toolbar first, as shown below:

If (! M_wndtoolbar.createex (this, tbstyle_flat, ws_child | ws_visible | cbrs_top | cbrs_tooltips | cbrs_flyby | cbrs_size_dynamic ))

{
Return-1;
}

Note: Do not m_wndtoolbar.loadtoolbar (idr_mainframe). In this way, you can do what you want in the new toolbar. Here, we cannot mention another class, ctoolbarctrl. As for the relationship and difference between it and ctoolbar, you can find it on the Internet. I will not talk about it here. However, you can use m_wndtoolbar.gettoolbarctrl () to obtain its ctoolbarctrl object from the ctoolbar object. In this way, you can also operate ctoolbarctrl in the ctoolbar object. For example, set the length and width of each button in the toolbar as follows:

M_wndtoolbar.gettoolbarctrl (). setbuttonwidth (40, 80)

According to my understanding, the functions of these two classes can be complementary.

Of course, you can also use other methods of the ctoolbar class to set your toolbar, such as setting the text display of each button:

M_wndtoolbar.setbuttontext (1, "system message ");

For more information about the attributes of a toolbar, see

M_wndtoolbar.setbuttoninfo function.

Method 3:

You also want to make your Toolbar more beautiful. You can display icons and the like on each button. Then you will involve another class: cimagelist, it can be seen from its name that this is an image/icon list class. Its usage is as follows:

1. Create a cimagelist object as follows:

Cimagelist IMG;

IMG. Create (22, 22, ilc_color8 | ilc_mask, 2, 2 );

For specific parameter meanings, refer to msdn or csdn.

2. Optional. Set the background color of the icon.

IMG. setbkcolor (: getsyscolor (color_btnface ))

3. Mount the image/icon to the list

IMG. Add (afxgetapp-> loadicon (ID)

4. Set the image list in the toolbar.

M_wndtoolbar.gettoolbarctrl (). setimagelist (& IMG)

There are two functions, setimagelist and sethotimagelist, which are used to set the list of highlighted images when the mouse is placed on the button. The front is the icon displayed when the mouse is not on it.

5. Release the image list object

IMG. Detach ()

Now, you can use this image list in the toolbar. As follows:

M_wndtoolbar.setbuttoninfo (1, ID, tbstyle_button, 1 );

The first 1 indicates the first space in the toolbar, and tbstyle_button indicates that the first control in the toolbar is button.

The next 1 is to display the 1st images in the image list.

Okay, everything is OK. You can see the beautiful toolbar after you run it.

Method 4:

If you have not only buttons but other spaces in the toolbar, you can also group them by function in the toolbar. Like the toolbar below, is it cool!

If you have mastered the above methods, then here we will introduce another class, the crebar class, you can easily solve it. In fact, the crebar class is a bit like a ctoolbar. In the crebar, it can contain several bands, and each band can contain different types of controls. In addition, each band can be adjusted freely.

Its usage is as follows:

1. First create a crebar object

Crebar m_wndrebar;

M_wndrebar.create (this );

2. Add the new toolbar or other windows to the rebar.

M_wndrebar.addbar (& m_wndtoolbar)

3. Set the crebar attribute

The Code is as follows:

// Change some attributes
Rebarbandinfo rbbi;
Rbbi. cbsize = sizeof (rbbi); // required
Rbbi. fmask = rbbim_childsize | rbbim_idealsize | rbbim_size | rbbim_background;
// Width of the toolbar
Rbbi. cxminchild = recttoolbar. Width ();
// Height
Rbbi. cyminchild = recttoolbar. Height ();
// The following line of code adds a background bitmap to the toolbar. Note the rbbim_background flag in rbbi. fmask.
Rbbi. hbmback = loadbitmap (: AfxGetInstanceHandle (), makeintresource (idb_toolbarbkmap ));

Rbbi. Cx = rbbi. cxideal = recttoolbar. Width () * 10;
// Set attributes
M_wndrebar.getrebarctrl (). setbandinfo (0, & rbbi );

For details about this code analysis, you can refer to an msdn. If you want to explain more, you may as well try it yourself.

OK. This is basically the case. You can continue to explore the details.

Method 5:

If you have read the CCTV news report, you should have read the electronic whiteboard, which is a tool bar on the edge. This tool bar can be used to control a common function of reading reports. And the toolbar is floating above and can be moved freely.

Here, we will introduce how to perform imaging of the tool bar described above.

Step 1:

Add a cdialogbar derived class to add a dialog box in the resource, and then use the Class Wizard to add classes. cdialogbar cannot be found as the base class. You can use cdialog as the base class to generate, then, replace "cdialog" with "cdialogbar", and the replacement is complete. Compile it. ^ _ ^ there is an error !! See step 2.

Step 2:

Resolving compilation errors and improving the class errors is actually a problem occurred while constructing the base class for function calls ,:

Cdialogbar (/* cdlgbar: IDD, pparent */) can be commented out like this. You can add a function similar to oninitdialog, and there is no oninitdialog message in cdialogbar, at least I still don't know, because the initialization is called after creation, we will rewrite the virtual bool create (cwnd * pparentwnd, uint nidtemplate, uint nstyle, uint NID) function.

Note: The parameters of the create function added by using the wizard are incorrect. Check the above.

The following is the implementation code (very simple)

Bool cdlgxxx: Create (cwnd * pparentwnd, uint nidtemplate, uint nstyle, uint NID)

{

// Todo: add your specialized code here and/or call the base class

Bool Bres = cdialogbar: Create (pparentwnd, nidtemplate, nstyle, NID );

Initdialogbar (); // Add a member function to the class.

Return Bres;

}

Bool cdlgxxx: initdialogbar ()

{

Updatedata (false); // this must be done. This will have the same data exchange effect as cdialog.

Return true;

}

Step 3:

Create and use

If (! M_barattrib.create (this, idd_dlg_com_attrib, cbrs_right | cbrs_gripper, XXX ))

{

Trace0 ("failed to create dialogbar \ n ");

Return-1;

}

M_barattrib.setwindowtext ("part attributes"); XXX is a resource ID that is manually added directly to the. h file of the resource. No, it will not be taught here.

The code for displaying and hiding toolbar is as follows:

Showcontrolbar (& m_barattrib, (m_barattrib.getstyle () & ws_visible) = 0, false );

After the above Code is implemented, dodataexchange can also be used. Adding controls to controls is just as convenient as cdialog. However, you must note that the addition of control class objects, I tried it. It seems that the window handle is always 0 and cannot be used.

Use getdlgitem (idc_driver_list) to obtain the control pointer.

Others use dodataexchange to control custom input formats. Here is an example of a text box. After adding a variable to the text control, the following code appears in dodataexchange:

Ddx_text (PDX, idc_com_var, m_strvar); //

Ddv_maxchars (PDX, m_strvar, var_max_len); //

Ddv_filenamestring (PDX, m_strvar); // The Custom manual addition implementation can be found below

Void CXXX: ddv_filenamestring (cdataexchange * PDX, cstring m_strfilename)

{

Cstring strerror = _ T ("\\/:*? \ "<> | ");

If (m_strfilename.spanexcluding (strerror )! = M_strfilename)

{

: Afxmessagebox (_ T ("the file name cannot contain" + strerror + "character "));

PDX-> fail (); // The key is that after this sentence is executed, an exception will be thrown and the following statement will not be executed.

}

}

Note:

1. The dodataexchange function is called only when updatedata () is executed. If PDX-> fail (); updatedata () is executed halfway, false is returned.

2. ddx_text (PDX, idc_com_var, m_strvar); // generated by the system

Ddv_maxchars (PDX, m_strvar, var_max_len); //

Ddv_filenamestring (PDX, m_strvar); // for the implementation of custom manual addition, see the preceding statements below to control the content of a control. They must be placed in one piece, the ddx_text should be placed in the first sentence, so that the interface can correctly point out that there is a problem with the control content, the control will be set to focus and select all the content.

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.