Problems with MFC toolbars separating bitmaps into icons

Source: Internet
Author: User
Tags array length reset

There is a problem in programming that you need to refer to the icons in the toolbar, and the icons are stored as bitmaps, referencing a single icon to the left of the menu bar, similar to the following illustration:


Example Figure 1

About referencing a single icon in a bitmap, you first need to detach the individual icons in the bitmap. Found some information on the internet, but did not find their expectations. Now in MFC to implement the icon in the split bitmap to do a simple summary, there may be a better way to follow up to do the supplement.

1. Create a new MFC dialog based project named Toolbaricontest

2. Open the Resource view to add a toolbar resource and import the bitmap resource that holds the icon you want, associating the new toolbar with the target bitmap

3. Define the individual icons in the target bitmap, and implement the icon and bitmap separation (as described in your own understanding) in the following steps:

First define the individual icon resources in the Resource.h, the above diagram is an example:

#define IDC_FILE_NEW 1500

#define Id_file_open 1501

#define ID_FILE_SAVE 1502

#define ID_CUT 1503

#define ID_COPY 1504

#define ID_PAST 1505

#define ID_PRINT 1506

#define ID_HELP 1507

#define ID_BUTTON32801 2222

#define ID_BUTTON32771 2223

#define ID_BUTTON32772 2224

And then add the toolbar in the Toolbaricontest.rc

Idr_toolbar1 TOOLBAR 16, 15

BEGIN

BUTTON Idc_file_new

BUTTON Id_file_open

BUTTON Id_file_save

SEPARATOR //Separator Bar

BUTTON Id_cut

BUTTON id_copy

BUTTON Id_past

BUTTON Id_print

BUTTON Id_help

BUTTON id_button32801

BUTTON id_button32771

BUTTON id_button32772

END

In this way, the running program opens the toolbar you just imported, as shown in the following illustration:

Example Figure 3

At this point, the desired requirements have been fulfilled, and the corresponding macros in the program can be referenced in places where the icon needs to be referenced.

Extended content:

In MFC, two ways to create a toolbar toolbar

Method One:

1. Create a Toolbar Resource (Toolbar Resource)
2. Constructing CToolBar objects
3. Call the Member method of the CToolBar object create or CreateEx to make a toolbar
4. Call LoadToolBar to load a toolbar resource

Method Two:

1. Constructing CToolBar objects
2. Call the Member method of the CToolBar object create or CreateEx to make a toolbar
3. Call the LoadBitmap method to load the bitmap (bitmap)
4. Call the Setbutton method to set the style of the button on the toolbar and to establish a correspondence between the image in the bitmap and the button

Description: Method One is generally used by default when the wizard tool is generated, which means that there are not too many customization requirements for toolbars. When you need to customize the toolbar, such as loading a custom bitmap, use the second way to create the toolbar. Pay more attention to the second way of creating.

Some basic concepts:
Button: The final arrangement of buttons in the toolbar is a variety of styles that can be set to Pushbuttons (default), Check-box buttons, or radio buttions. All buttons are the same size on the same toolbar, and the default is 22 pixels (pixel) According to the Windows Interface Guidelines for software design. A button corresponds to an image. The buttons on the toolbar are the same as the normal buttons, and the IDs correspond to them.


Image: A bitmap (bitmap) is provided in both of these constructs, and image is taken out of the bitmap. By default, each image is sequentially corresponding to a button. As with the button, all images in the bitmap have the same size, and the default is 15 pixels wide by 16 pixels high . The button is larger than the position of image, so the extra space between the two forms the boundary of the image (border).


Seprator: You can insert a separator bar (seprator) between buttons. These separators can be used to group buttons of similar functions on the display effect, adding some benefits to the operation.

Customize toolbars

In the second method above, the 4th step setbuttons to complete the corresponding relationship between the button and the image. In the SetButtons parameter, the first is an array that points to the control ID (command ID), and the second parameter is the array length. This method assigns the ID in the array to each button, and the image position in the bitmap corresponds to the button establishment order. If a special value id_seprator appears in the array, the delimiter is inserted here.


CToolBar provides methods to modify the order in which the button corresponds to the image (SetButtonInfo), or to dynamically modify the button's display style (Getbuttonstyle/setbuttonstyle). You can also set text (Getbuttontext/setbuttontext) on a button, and so on.


There are two more important ways to create a second method. Because the custom bitmap may exceed the default pixel value (16 x), you need to resize it. You can call setsizes to reset the size (size) of the button on the toolbar. After you set the size, you also need to call SetHeight to reset the height of the toolbar (override the Standard toolbar height), otherwise the button size changes while the toolbar default height is unchanged. When displayed, the button will be truncated at the bottom.


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.