Use VC to write an application with the dynamic skin replacement function

Source: Internet
Author: User
Introduction

If you can have the ability to change skin dynamically in your own program, the software will add a few highlights to make it easier for users to favor. As I know, there are few articles about this technology, which is not in coordination with the popularity of the technology. In addition, a small amount of existing materials are mostly for the dialog box, it is nothing more than overwriting an image in the dialog box, and changing the image to achieve dynamic skin replacement of the program. Although this is also a kind of dynamic skin replacement, this article will focus on another dynamic skin replacement technology, the tool bar dynamic skin replacement technology, because the technology is too simple, this is also a skin-changing technology widely used by software such as IE browser.

Program supports dynamic skin replacement

Because the skin swap function is an extension of the program framework, the relevant processing code should be carried out in the main framework class. In order for the program to have the ability to skin, the program must first have the basic conditions for skin replacement. First, it can be clarified that the processing object is a common toolbar, and its base class is ctoolbar. Msdn helps you understand this class and Its/"relatives/" inheritance relationships. By looking at these related classes, we can find that the crebar class is special. It is not used for display, but mainly used to accommodate other tool bars. For example, you can use its member function addbar () add toolbar, dialogbar, and Other types to the rebar so that multiple toolbar entries can be displayed. So we can come up with this design idea: the tool bar of the program framework is no longer implemented directly by the tool bar, but is based on the compound bar, and the original tool bar is added to it through the addbar () function of the crebar. The skin can be tiled over the compound bar by setting the rebarbandinfo structure related to the crebar. As there is a toolbar covering it, you must modify the style when creating the toolbar ), make the background transparent so that the skin located in the lower layer can be revealed.

Because crebar is used in programming, it is troublesome to implement it in common programs. You can select the/"inte.net explorer rebars/" option in Step 4 when creating a project, in this way, a toolbar and a dialog bar are added to the composite bar. You only need to remove the declaration part of the Instance Object m_wnddlgbar of cdialogbar from the header file of the main framework class and the oncreate () function of the implementation file.

When creating a toolbar, you must specify window style_transparent to make the background transparent. After creating a composite toolbar, you can use the addbar () function to add the toolbar with transparent background:

M_wndtoolbar.createex (this,
Tbstyle_flattbstyle_transparent,
Ws_childws_visiblecbrs_align_top,
Crect (0, 0, 0), afx_idw_toolbar)
......
M_wndrebar.addbar (& m_wndtoolbar,
Null, null,
Rbbs_gripperalwaysrbbs_fixedbmp rbbs_break)

After several steps above, the main framework of the program is transformed to meet the necessary conditions for dynamic skin replacement.

Skin loading and Dynamic Replacement

The skin is generally released as an external resource and dynamically loaded by the program during use. The process of loading skin to the toolbar can be roughly divided into two steps: first load the skin from the file to the memory, and then directly draw the composite bar from the memory bitmap. For the first step, you can use api+loadimageto load the external file back.bmp from the file to the memory in the bitmap format. The returned handle can be forcibly converted to get the hbitmap-type bitmap handle m_bmp back:

M_bmp back = (hbitmap) LoadImage (AfxGetInstanceHandle (), // application instance handle
/"Back.bmp/", image_bitmap, 0, 0,
Lr_loadfromfilelr_createdibsection );

After the skin is loaded into the memory, you must set the rebarbandinfo structure to tile the skin bitmap in the Composite Bar. The structure has more than 20 member variables, however, this is only used to set the tile of the background bitmap. Therefore, you only need to set fmask to rbbim_background to specify that hbmback is valid, and pass the previously loaded bitmap handle m_bmp back to the member variable to load and display the skin. The following is the detailed code of this part:

Crebarctrl & rc = m_wndrebar.getrebarctrl (); // obtain the compound control pointer.
Rebarbandinfo Info;
Memset (& info, 0, sizeof (rebarbandinfo); // Reset
Info. cbsize = sizeof (Info );
Info. fmask = rbbim_background; // specify that hbmback is valid.
// If the bitmap handle is unavailable, it is still a silver-gray background; otherwise, the bitmap is used as the background of the Compound Bar.
Info. hbmback = m_bmp back! = Invalid_handle_value? M_bmp back: NULL;
RC. setbandinfo (0, & info); // set
RC. updatewindow (); // update window

And then call the above two steps to re-load the new skin plug-in to achieve dynamic skin replacement of the program. The copy object function is generally implemented using the API copyfile (). The first two parameters of this function are the source file path and the target file path. The last Boolean parameter specifies the file copy method. It must be set to false here. That is, if the target file already exists, it is overwritten. Otherwise, the skin cannot be replaced.
Summary
This article implements dynamic skin replacement for applications under VC through the use of composite strips and crebar classes. The crebar class has rich functions. This article does not provide in-depth introductions due to its limited space. For details about this class, see Microsoft's msdn help. The program described in this article is compiled by Microsoft Visual C ++ 6.0 under Windows 98.

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.