Implement VS.NET or Office XP-style menu with C # and VB.NET
The god of the small gas 2001.08.18
The menu in vs.net or office XP is very beautiful, anyway, I like it. Unfortunately VS.NET does not have a control or component that makes this menu, I don't know that the official version will provide a template and wizard. I still remember the feelings of the Turbo C to make menus when I have just learned the computer language, those rectangular box functions and pixel operations are really charming, and this is the fashion menu.
In this article, I will introduce the menus for making their own style in Framework SDK Beta 2, so you'd better have Framework SDK Beta 2, and VS.NET Beta 2 is not necessarily necessary. Examples in the included ZIP package are VS.NET Project.
The entire article consists of three parts:
Start I will involve the concept of some of the most basic menus in WinForm.
Then there will be an example of a previously touched menu, which is for Beta 1. Honestly, I didn't expect Beta1 to Beta2, many functions and namespace have changed. I used to test this example in Beta1, very smooth. This time there will be many errors in beta2, I have two project, one is the original Project, one is the for Beta 2 after I modified. This transplant is very boring, but you can quickly familiarize yourself with new beta2 class libraries and functions. The menu from show is not bad, it feels the Office2000 style menu. If you are interested, you can try this process, it will benefit shallow, this example also includes buttons, the original author is actually in the "Owner-Drawn Menus" technology in Demo control; but I only bought some of the menu .
The last part is an example of making a VS.NET or XP style. The effect of the above example cannot make me completely satisfied, and then I rereaded another, but I am not very satisfied with the final result, because I have no previous example. So complete, I only show this style of this style, I have not considered the event response, status bar update, tooltips, menu status (Enabled State), and I will pay them into time issues and promise yourself next time. Do it better.
1. Framework SDK Beta 2 Menu is divided into two categories of one class is a normal menu called: MainMenu, there is such a corresponding menu control in the Toolsbox of VS.NET, dragging below it into your form, set the property. See the income, this version is more useful than the vs.studio98 series. Another class called: ContextMenu menu, that is, the common pop-up menu. For VB6, all normal menus are menu that can be compatible and automatically upgraded to MainMenu types in VS.NET, but for PopMenu's menus cannot be converted to the ContextMenu type menu, you must re-modify the code implementation. Here we are mainly for MainMenu, the same is true.
The simplest menu you can do this:
Using system;
Using system.windows.forms;
Public class frMvb6: form {
Private mainmenu mumain; // mainmenu
Public Static Int
Main
(String [] args) {
Application.run (New FRMVB6 ());
Return 0;
}
Public frmvb6 () {
// The Following Code Sets Up The Form Properties.
THIS.TEXT = "Form1"; this.height = 213 systemInformation.captionHeight;
THIS.WIDTH = 312;
THIS.StartPosition = formstartPosition.windowsDefaultLocation;
Menuitem mitemfile = new menuitem ();
mitemfile.text = "& file";
Menuitem mitemexit = new menuitem ();
MIiteMexit.text = "E & XIT";
Mumain = new mainmenu ();
Mumain.MenuItems.Add (MItemfile);
Mumain.MenuItems.Add (MIiteMexit);
THIS.MENU = Mumain;
}
}
Handmade it saves it for a .cs file and compiled it:
CSC / T: Winexe /R :system.dll /r:system.windows.forms.dll /r:system.drawing.dll form1menu.cs
Under the VS.NET, just a new WinForm project, then put the mainMenu control in the default form, then set the property, F5 is ok, do not need a row code at all.
If you want to generate a master menu and a menu item, mainly MenuItems.addrange method, look at the following code:
This.mainMenu1 = new system.windows.Forms.mainMenu ();
THIS.MENUITEM1 = New System.windows.Forms.Menuitem ();
THIS.MENUITEM2 = New System.windows.Forms.MenuItem ();
THIS.MENUITEM3 = New System.windows.Forms.MenuItem ();
This.Menuitem4 = new system.windows.Forms.MenuItem ();
// mainmenu1
this.mainMenu1.Menuitems.Addrange (new system.windows.forms.MenuItem [] {
THIS.MENUITEM1, THIS.MENUITEM2};
// Menuitem1
THIS.MENUITEM1.INDEX = 0;
This.Menuitem1.Menuitems.Addrange (new system.windows.forms.MenuItem [] {
THIS.MENUITEM3, this.Menuitem4};
THIS.MENUITEM1.INDEX = 0;
THIS.MENUITEM1.TEXT = "& File";
// Menuitem2
this.Menuitem2.index = 1;
This.Menuitem2.text = "Help";
// Menuitem3
this.Menuitem3.index = 0;
This.Menuitem3.Text = "open";
This.Menuitem3.click = new system.eventhandler (this.MenuItem3_click);
// Menuitem4
this.Menuitem4.index = 1;
THIS.MENUITEM4.TEXT = "exit"; this.Menuitem4.click = new system.eventhandler (this.MenuItem4_click);
THIS.MENU = this.mainMenu1;
If the code shows Menuitem1 (File) and Menuitem2 (HELP) by AddRange to MainMenu1, MenuItem3 (Open) and Menuitem4 (Exit) have become the submenu item under the File menu in Addrange to Menuitem1 (File). .
This.Menuitem3.click = new system.eventhandler (this.MenuItem3_click); Event handler that is excited when MenuItem3 is clicked, the general event handler is like this:
Private Void Menuitem3_Click (Object Sender, System.EventArgs E)
{
MessageBox.show ("My Click Open");
}
What we care is actually Menuitem, let it draw in our way, you can use a VS.NET or XP style menu, it is simple to implement a MenuItem's inheritance class, expand the part of its DRAW. Ok, let us go deep into the second part.