Implement office2003-style menu components in C #

Source: Internet
Author: User

Brief introduction

With just one simple line of programs, you can make all the menu and context menus of your Windows Forms have a office2003 menu appearance. In the same way, you can add a nice icon to your menu with just one line of programs. This article implements a component that has this functionality. If you want your menu to revert to its original appearance, just call the end method.

Use of components

To use components correctly, you must first add your components to the Toolbox. Then drag and drop the component from the toolbox and drop it into form. You will see an icon named OfficeMenus1 in the design page of your form, which shows that the menu component has been added to the form. The following methods are called:

//开始显示office 2003菜单
OfficeMenus1.Start( FormName ); 注:FormName为要改变菜单风格的窗口名称。
Similarly, you can terminate the office2003 style of the menu by calling the following method to get back to the original appearance: // 改变菜单的外观风格到原始状态
OfficeMenus1.End();
adding an icon to the top of the menu is simple, adding a ImageList (Image List control) to the project. Then change the ImageList attribute of the Officemenu component to the ImageList you added, using the following code:// 为菜单添加图像
// OfficeMenus.AddPicture( MenuItem MenuItemToAddPictureTo, int ImageIndex );
OfficeMenus1.ImageList = imageList1;
OfficeMenus1.AddPicture(menuItem2, 1);

As you can see, only a few lines of code can easily make your menu office2003 style.

The realization method and principle of the component

The component is implemented by three classes, each of which is officemenus,mainmenuitemdrawing and menuitemdrawing, all in the namespace Dev4arabs. Because the implementation code is longer, only the idea of component implementation is given here.

The first step in component implementation is to derive class Officemenus from the System.ComponentModel.Component class. Defined as follows: public class OfficeMenus : System.ComponentModel.Component then define two static variables in the class:

The image list is used to store a collection of names for the icon static ImageList _imageList; //store details in the menu, and the NameValueCollection details refer to MSDN, which is primarily used to make each menu handle have a one by one corresponding relationship to each icon. To quickly find the icon for a menu when you draw the icon at the top of the menu. static NameValueCollection picDetails = new NameValueCollection();Next, define the public interface method start and end.

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.