精华内容
下载资源
问答
  • VB 为无标题栏窗体添加菜单,为无标题栏窗体添加菜单,添加了一种弹出式菜单,窗口美化的挺漂亮,如示例截图所示,源码运行于VB6.0环境。相关代码:  Select Case Index  Case 0 '添加菜单  PopupMenu Frm_menu....
  • VB窗体主菜单中动态添加菜单项,在窗口的“菜单”中可自定义随时添加一个菜单项,在文本框中输入菜单文字即可,程序会以输入的文字为菜单名称新建一个菜单。
  • 逆向添加菜单vb

    2020-03-06 21:46:37
    就两个函数一个是 GetMenu,一个是AppendMenu Private Declare Function GetMenu Lib “user32” (ByVal hwnd As Long) As Long Private Declare Function...AppendMenu hM, MF_STRING, 123, “我的菜单” End Sub

    就两个函数一个是 GetMenu,一个是AppendMenu
    Private Declare Function GetMenu Lib “user32” (ByVal hwnd As Long) As Long
    Private Declare Function AppendMenu Lib “user32” Alias “AppendMenuA” (ByVal hMenu As Long, ByVal wFlags As Long, ByVal wIDNewItem As Long, ByVal lpNewItem As Any) As Long
    Private Const MF_STRING = &H0&

    Private Sub Command1_Click()
    Dim hM As Long
    hM = GetMenu(记事本的窗口句柄)

    AppendMenu hM, MF_STRING, 123, “我的菜单”
    End Sub

    展开全文
  • 4-向窗体添加菜单

    千次阅读 2007-05-18 11:55:00
    Windows 窗体支持菜单和上下文菜单。主菜单菜单栏上显示,菜单栏位于窗体标题栏的紧下方。菜单栏包含用于将相关子菜单项分组的顶级菜单项。例如,通过单击“文件”顶级菜单项,可以显示与文件操作相关的菜单项。...


    Windows 窗体支持菜单和上下文菜单。主菜单在菜单栏上显示,菜单栏位于窗体标题栏的紧下方。菜单栏包含用于将相关子菜单项分组的顶级菜单项。例如,通过单击“文件”顶级菜单项,可以显示与文件操作相关的菜单项。菜单项通常显示为应用程序的命令(如“新建”和“打开”),但是它们也可以显示为分隔条和子菜单项。可以在菜单项的旁边显示选中标记,以显示应用程序中命令的状态或功能的状态。在 Windows 窗体中,主菜单由 MainMenu 控件表示。

    可为窗体的特定控件或区域显示上下文菜单。通常通过单击鼠标右键访问它们。在 Windows 窗体中,上下文菜单由 ContextMenu 控件表示。

    ContextMenu 和 MainMenu 从 Menu 派生。它们共享许多属性、方法和事件。

    下面的代码演示如何向窗体添加 MainMenu。

    C# source:
    MainMenu mainMenu = new MainMenu();
    this.Menu = mainMenu;

    VB source:
    Dim mainMenu As New MainMenu
    Me.Menu = mainMenu 

     
    向控件添加上下文菜单
    下面的代码演示如何创建 ContextMenu 并将其分配到控件。

    C#
    ContextMenu label1ContextMenu = new ContextMenu();
    Label label1 = new Label();
    label1.ContextMenu = label1ContextMenu;

    VB
    Dim label1ContextMenu As New ContextMenu
    Dim label1 As New Label
    label1.ContextMenu = label1ContextMenu

     
    添加菜单项
    在下面的示例中,将 File 菜单项添加到 MainMenu 中。File 菜单项包含名为 Open 和 Exit 的子菜单项。
    C#
    //Add File Menu
    MenuItem miFile = mainMenu.MenuItems.Add("&File");
    miFile.MenuItems.Add(new MenuItem("&Open..."));
    miFile.MenuItems.Add("-");     // Gives us a seperator
    miFile.MenuItems.Add(new MenuItem("E&xit"));

    VB
    'Add File Menu
    Dim miFile As MenuItem = mainMenu.MenuItems.Add("&File")
    miFile.MenuItems.Add(new MenuItem("&Open..."))
    miFile.MenuItems.Add("-")     ' Gives us a seperator
    miFile.MenuItems.Add(new MenuItem("E&xit"))


    下面的代码演示如何为在前面代码示例中创建的 Open 和 Exit 菜单项处理 Click 事件。

    C# 
    ....
    //Add File Menu
    MenuItem miFile = mainMenu.MenuItems.Add("&File");
    miFile.MenuItems.Add(new MenuItem("&Open...", new EventHandler(this.FileOpen_Clicked), Shortcut.CtrlO));
    miFile.MenuItems.Add("-");     // Gives us a seperator
    miFile.MenuItems.Add(new MenuItem("E&xit", new EventHandler(this.FileExit_Clicked), Shortcut.CtrlX));
    ....

    //File->Exit Menu item handler
    private void FileExit_Clicked(object sender, System.EventArgs e) {
        this.Close();
    }

    //File->Open Menu item handler
    private void FileOpen_Clicked(object sender, System.EventArgs e) {
        MessageBox.Show("And why would this open a file?");
    }

    ....

    VB
    ....
    'Add File Menu
    Dim miFile As MenuItem = mainMenu.MenuItems.Add("&File")
    miFile.MenuItems.Add(new MenuItem("&Open...", new EventHandler(AddressOf Me.FileOpen_Clicked)))
    miFile.MenuItems.Add("-")     ' Gives us a seperator
    miFile.MenuItems.Add(new MenuItem("E&xit", new EventHandler(AddressOf Me.FileExit_Clicked)))
    ....

    'File->Exit Menu item handler
    Private Sub FileExit_Clicked(sender As object, e As System.EventArgs)
        Me.Close
    End Sub

    'File->Open Menu item handler
    Private Sub FileOpen_Clicked(sender As object, e As System.EventArgs)
        MessageBox.Show("And why would this open a file?")
    End Sub

    ....

     
    添加子菜单
    下面的示例演示如何创建子菜单。
    C#

    //Add Format Menu
    MenuItem miFormat = mainMenu.MenuItems.Add("F&ormat");

    //Font Face sub-menu
    mmiSansSerif = new MenuItem("&1. " + sansSerifFontFamily.Name, new EventHandler(this.FormatFont_Clicked));
    mmiSerif = new MenuItem("&2. " + serifFontFamily.Name, new EventHandler(this.FormatFont_Clicked));
    mmiMonoSpace = new MenuItem("&3. " + monoSpaceFontFamily.Name, new EventHandler(this.FormatFont_Clicked));

    miFormat.MenuItems.Add( "Font &Face",
                            (new MenuItem[]{ mmiSansSerif, mmiSerif, mmiMonoSpace })
                            );

    //Font Size sub-menu
    MenuItem miSmall = new MenuItem("&Small", new EventHandler(this.FormatSize_Clicked));
    MenuItem miMedium = new MenuItem("&Medium", new EventHandler(this.FormatSize_Clicked));
    MenuItem miLarge = new MenuItem("&Large", new EventHandler(this.FormatSize_Clicked));

    miFormat.MenuItems.Add( "Font &Size"
                          , new EventHandler(this.FormatSize_Clicked)
                          , (new MenuItem[]{ miSmall, miMedium, miLarge }));

    VB
    'Add Format Menu
    Dim miFormat As MenuItem = mainMenu.MenuItems.Add("F&ormat")

    'Font Face sub-menu
    mmiSansSerif = New MenuItem("&1. " & sansSerifFontFamily.Name, New EventHandler(AddressOf Me.FormatFont_Clicked))
    mmiSerif = New MenuItem("&2. " & serifFontFamily.Name, New EventHandler(AddressOf Me.FormatFont_Clicked))
    mmiMonoSpace = New MenuItem("&3. " & monoSpaceFontFamily.Name, New EventHandler(AddressOf Me.FormatFont_Clicked))

    miFormat.MenuItems.Add("Font &Face" _
                          , (New MenuItem() {mmiSansSerif, mmiSerif, mmiMonoSpace}))

    'Font Size sub-menu
    mmiSmall = New MenuItem("&Small", AddressOf Me.FormatSize_Clicked)
    mmiMedium = New MenuItem("&Medium", AddressOf Me.FormatSize_Clicked)
    mmiLarge = New MenuItem("&Large", AddressOf Me.FormatSize_Clicked)

    miFormat.MenuItems.Add( "Font &Size" _
                          , New EventHandler(AddressOf Me.FormatSize_Clicked) _
                          , (New MenuItem(){ mmiSmall, mmiMedium, mmiLarge }) _
                          )

     
     
    添加默认菜单项
    下面的示例演示如何指定默认菜单项。

    C# 
    //Add Format Menu
    mmiSansSerif = new MenuItem("&1. " + sansSerifFontFamily.Name, new EventHandler(this.FormatFont_Clicked));
    mmiSansSerif.DefaultItem = true ;

    VB
    'Font Face sub-menu
    mmiSansSerif = New MenuItem("&1. " & sansSerifFontFamily.Name, New EventHandler(AddressOf Me.FormatFont_Clicked))
    mmiSansSerif.DefaultItem = True


     
    向菜单项添加选中标记
    下面的示例演示如何在菜单项的旁边显示选中标记。该代码还演示如何跟踪选中了哪个项。
    C#
    //Add Format Menu
    MenuItem miMedium = new MenuItem("&Medium", new EventHandler(this.FormatSize_Clicked));
    miMedium.Checked = true;

    ....


    private void FormatSize_Clicked(object sender, System.EventArgs e) {
        MenuItem miClicked = (MenuItem)sender;

        // Uncheck old selection
        miMainFormatSizeChecked.Checked = false;

        //Do Menu item action and make a new selection
        ....
        miMainFormatSizeChecked = miSmall;
        ....

        // Check the new selection
        miMainFormatSizeChecked.Checked = true;
    }


    VB
    ' Add Format Menu
    Dim miMedium As MenuItem = new MenuItem("&Medium", AddressOf Me.FormatSize_Clicked)
    miMedium.Checked = True

    ....


    Private Sub FormatSize_Clicked(sender As object, e As System.EventArgs)
        Dim miClicked As MenuItem = CType(sender, MenuItem)

        ' Uncheck old selection
        miMainFormatSizeChecked.Checked = False

        ' Do Menu item action and make a new selection
        ....
        miMainFormatSizeChecked = miSmall
        ....

        ' Check the new selection
        miMainFormatSizeChecked.Checked = True
    End Sub

     
    克隆菜单
    在很多情况下,控件的上下文菜单是主菜单的子集。无法将同样的菜单项添加到多个菜单中,但是可以克隆菜单项或菜单项集合。下面的代码演示如何克隆前面创建的 Format 菜单,并将其添加到 Label 的上下文菜单中。

    C#
    //Add Format to label context menu
    //Note have to add a clone because menus can't belong to 2 parents

    label1ContextMenu.MenuItems.Add(miFormat.CloneMenu());

    VB
    'Add Format to label context menu
    'Note have to add a clone because menus can't belong to 2 parents

    label1ContextMenu.MenuItems.Add(miFormat.CloneMenu)

     
    菜单合并
    在 MDI 应用程序中,每个 MDI 子窗体的主菜单都合并到 MDI 父窗体的主菜单中。


    菜单示例

    Imports System Imports System.ComponentModel
    Imports System.Drawing
    Imports System.Drawing.Text
    Imports System.Windows.Forms

    Namespace Microsoft.Samples.WinForms.VB.Menus

        Public Class Menus
            Inherits System.Windows.Forms.Form

            Private Class FontSizes
                Public Shared Small As Single = 8F
                Public Shared Medium As Single = 12F
                Public Shared Large As Single = 24F
            End Class

            '字体外观和大小
            Private fontFace As String = "Arial"
            Private fontSize As Single = FontSizes.Medium

            '用来跟踪被选中和未被选中的菜单项
            Private mmiSansSerif As MenuItem
            Private mmiSerif As MenuItem
            Private mmiMonoSpace As MenuItem
            Private mmiSmall As MenuItem
            Private mmiMedium As MenuItem
            Private mmiLarge As MenuItem
            Private cmiSansSerif As MenuItem
            Private cmiSerif As MenuItem
            Private cmiMonoSpace As MenuItem
            Private cmiSmall As MenuItem
            Private cmiMedium As MenuItem
            Private cmiLarge As MenuItem

            Private miMainFormatFontChecked As MenuItem
            Private miMainFormatSizeChecked As MenuItem
            Private miContextFormatFontChecked As MenuItem
            Private miContextFormatSizeChecked As MenuItem

            Private currentFontFamily As FontFamily
            Private monoSpaceFontFamily As FontFamily
            Private sansSerifFontFamily As FontFamily
            Private serifFontFamily As FontFamily

            Public Sub New()

                MyBase.New()

                Menus = Me

                '此调用是 Windows 窗体设计器所必需的。
                InitializeComponent()

                monoSpaceFontFamily = new FontFamily (GenericFontFamilies.Monospace)
                sansSerifFontFamily = new FontFamily (GenericFontFamilies.SansSerif)
                serifFontFamily = new FontFamily (GenericFontFamilies.Serif)
                currentFontFamily = sansSerifFontFamily

                label1.Font = New Font(fontFace, fontSize)

                '添加“文件”菜单
                Dim miFile As MenuItem = MainMenu.MenuItems.Add("文件(&F)")
                miFile.MenuItems.Add(New MenuItem("打开(&O)...", New EventHandler(AddressOf Me.FileOpen_Clicked), Shortcut.CtrlO))
                miFile.MenuItems.Add("-")     ' 提供一个分隔符
                miFile.MenuItems.Add(New MenuItem("退出(&X)", New EventHandler(AddressOf Me.FileExit_Clicked), Shortcut.CtrlX))

                '添加“格式”菜单
                Dim miFormat As MenuItem = MainMenu.MenuItems.Add("格式(&O)")

                '“字体外观”子菜单
                mmiSansSerif = New MenuItem("1.(&1)" & sansSerifFontFamily.Name, New EventHandler(AddressOf Me.FormatFont_Clicked))
                mmiSansSerif.Checked = True
                mmiSansSerif.DefaultItem = True
                mmiSerif = New MenuItem("2. (&2)" & serifFontFamily.Name, New EventHandler(AddressOf Me.FormatFont_Clicked))
                mmiMonoSpace = New MenuItem("3.(&3)" & monoSpaceFontFamily.Name, New EventHandler(AddressOf Me.FormatFont_Clicked))

                miFormat.MenuItems.Add("字体外观(&F)" _
                                      , (New MenuItem() {mmiSansSerif, mmiSerif, mmiMonoSpace}))

                '“字体大小”子菜单
                mmiSmall = New MenuItem("小(&S)", New EventHandler(AddressOf Me.FormatSize_Clicked))
                mmiMedium = New MenuItem("中(&M)", New EventHandler(AddressOf Me.FormatSize_Clicked))
                mmiMedium.Checked = True
                mmiMedium.DefaultItem = True
                mmiLarge = New MenuItem("大(&L)", New EventHandler(AddressOf Me.FormatSize_Clicked))

                miFormat.MenuItems.Add("字体大小(&S)" _
                                      , (New MenuItem() {mmiSmall, mmiMedium, mmiLarge}))

                '向标签上下文菜单添加格式
                '注意,因为菜单不能属于两个父级,所以必须添加一个复本
                label1ContextMenu.MenuItems.Add(miFormat.CloneMenu)

                ' 设置上下文菜单项 - 使用它们选中和取消选中项
                cmiSansSerif = label1ContextMenu.MenuItems(0).MenuItems(0).MenuItems(0)
                cmiSerif = label1ContextMenu.MenuItems(0).MenuItems(0).MenuItems(1)
                cmiMonoSpace = label1ContextMenu.MenuItems(0).MenuItems(0).MenuItems(2)
                cmiSmall = label1ContextMenu.MenuItems(0).MenuItems(1).MenuItems(0)
                cmiMedium = label1ContextMenu.MenuItems(0).MenuItems(1).MenuItems(1)
                cmiLarge = label1ContextMenu.MenuItems(0).MenuItems(1).MenuItems(2)

                '用这些来跟踪被选中的菜单项
                '因为有菜单和上下文菜单,所以这会比较复杂
                miMainFormatFontChecked = mmiSansSerif
                miMainFormatSizeChecked = mmiMedium
                miContextFormatFontChecked = cmiSansSerif
                miContextFormatSizeChecked = cmiMedium

            End Sub

            '“文件”->“退出”菜单项处理程序
            Private Sub FileExit_Clicked(ByVal sender As Object, ByVal e As System.EventArgs)
                Me.Close()
            End Sub

            '“文件”->“打开”菜单项处理程序
            Private Sub FileOpen_Clicked(ByVal sender As Object, ByVal e As System.EventArgs)
                MessageBox.Show("为什么这会打开文件?")
            End Sub

            '“格式”->“字体”菜单项处理程序
            Private Sub FormatFont_Clicked(ByVal sender As Object, ByVal e As System.EventArgs)

                Dim miClicked As MenuItem = CType(sender, MenuItem)

                miMainFormatFontChecked.Checked = False
                miContextFormatFontChecked.Checked = False

                If miClicked Is mmiSansSerif Or miClicked Is cmiSansSerif Then
                    miMainFormatFontChecked = mmiSansSerif
                    miContextFormatFontChecked = cmiSansSerif
                    currentFontFamily = sansSerifFontFamily
                ElseIf miClicked Is mmiSerif Or miClicked Is cmiSerif Then
                    miMainFormatFontChecked = mmiSerif
                    miContextFormatFontChecked = cmiSerif
                    currentFontFamily = serifFontFamily
                Else
                    miMainFormatFontChecked = mmiMonoSpace
                    miContextFormatFontChecked = cmiMonoSpace
                    currentFontFamily = monoSpaceFontFamily
                End If

                miMainFormatFontChecked.Checked = True
                miContextFormatFontChecked.Checked = True

                label1.Font = New Font(currentFontFamily, fontSize)

            End Sub

            '“格式”->“大小”菜单项处理程序
            Private Sub FormatSize_Clicked(ByVal sender As Object, ByVal e As System.EventArgs)

                Dim miClicked As MenuItem = CType(sender, MenuItem)

                miMainFormatSizeChecked.Checked = False
                miContextFormatSizeChecked.Checked = False

                Dim fontSizeString As String = miClicked.Text

                If (fontSizeString = "小(&S)") Then
                    miMainFormatSizeChecked = mmiSmall
                    miContextFormatSizeChecked = cmiSmall
                    fontSize = FontSizes.Small
                ElseIf (fontSizeString = "大(&L)") Then
                    miMainFormatSizeChecked = mmiLarge
                    miContextFormatSizeChecked = cmiLarge
                    fontSize = FontSizes.Large
                Else
                    miMainFormatSizeChecked = mmiMedium
                    miContextFormatSizeChecked = cmiMedium
                    fontSize = FontSizes.Medium
                End If

                miMainFormatSizeChecked.Checked = True
                miContextFormatSizeChecked.Checked = True

                label1.Font = New Font(currentFontFamily, fontSize)
            End Sub

            '窗体重写 dispose 以清理组件列表。
            Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
                If disposing Then
                    If Not (components Is Nothing) Then
                        components.Dispose()
                    End If
                End If
                MyBase.Dispose(disposing)
            End Sub


    #Region " Windows 窗体设计器生成的代码 "

            'Windows 窗体设计器所必需的
            Private components As System.ComponentModel.Container
            Private WithEvents label1 As System.Windows.Forms.Label
            Private WithEvents mainMenu As System.Windows.Forms.MainMenu
            Private label1ContextMenu As System.Windows.Forms.ContextMenu

            Private WithEvents Menus As System.Windows.Forms.Form

            '注意:下面的过程是 Windows 窗体设计器所必需的
            '可以使用 Windows 窗体设计器修改它。
            '不要使用代码编辑器修改它。
            Private Sub InitializeComponent()
                Me.components = New System.ComponentModel.Container()
                Me.mainMenu = New System.Windows.Forms.MainMenu()
                Me.label1 = New System.Windows.Forms.Label()
                Me.label1ContextMenu = New System.Windows.Forms.ContextMenu()
                Me.Text = "Menus 'R Us"
                Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
                Me.Menu = mainMenu
                Me.ClientSize = New System.Drawing.Size(392, 117)

                label1.BackColor = System.Drawing.Color.LightSteelBlue
                label1.Location = New System.Drawing.Point(16, 24)
                label1.TabIndex = 0
                label1.Anchor = CType(13, System.Windows.Forms.AnchorStyles)
                label1.Size = New System.Drawing.Size(360, 50)
                label1.Text = "Right Click on me - I have a context menu!"
                label1.ContextMenu = label1ContextMenu

                Me.Controls.Add(label1)
            End Sub

    #End Region

            '运行应用程序
            '应用程序的主入口点
            <STAThread()> Shared Sub Main()
                System.Windows.Forms.Application.Run(New Menus())
            End Sub

        End Class

    End Namespace

    C#

    namespace Microsoft.Samples.WinForms.Cs.Menus {
        using System;
        using System.ComponentModel;
        using System.Drawing;
        using System.Drawing.Text;
        using System.Windows.Forms;

        public class Menus : System.Windows.Forms.Form {
            /// <summary>
            ///    必需的设计器变量。
            /// </summary>
            private System.ComponentModel.Container components;
            protected internal System.Windows.Forms.Label label1;
            protected internal System.Windows.Forms.MainMenu mainMenu;
            protected internal System.Windows.Forms.ContextMenu label1ContextMenu;

            // 注释此结构以在 WinForms 设计器中查看 menus.cs。
            // 记住不要在设计器中编辑菜单。这样会与
            // 手写菜单代码冲突。记住在完成此操作
            // 后取消注释此结构。
            private struct FontSizes {
                public static float Small = 8f;
                public static float Medium = 12f;
                public static float Large = 24f;
            }

            //字体外观和大小
            private float fontSize = FontSizes.Medium;

            //用来跟踪被选中和未被选中的菜单项
            private MenuItem mmiSansSerif;
            private MenuItem mmiSerif;
            private MenuItem mmiMonoSpace;

            private MenuItem mmiSmall;
            private MenuItem mmiMedium;
            private MenuItem mmiLarge;

            private MenuItem cmiSansSerif;
            private MenuItem cmiSerif;
            private MenuItem cmiMonoSpace;

            private MenuItem cmiSmall;
            private MenuItem cmiMedium;
            private MenuItem cmiLarge;

            private MenuItem miMainFormatFontChecked ;
            private MenuItem miMainFormatSizeChecked ;
            private MenuItem miContextFormatFontChecked ;
            private MenuItem miContextFormatSizeChecked ;

            private FontFamily currentFontFamily ;
            private FontFamily monoSpaceFontFamily;
            private FontFamily sansSerifFontFamily;
            private FontFamily serifFontFamily;

            public Menus() {

                //
                // Windows 窗体设计器支持所必需的
                //
                InitializeComponent();

                // 初始化字体 - 使用一般字体以避免在操作
                // 系统的不同版本之间出现问题
                monoSpaceFontFamily = new FontFamily (GenericFontFamilies.Monospace);
                sansSerifFontFamily = new FontFamily (GenericFontFamilies.SansSerif);
                serifFontFamily = new FontFamily (GenericFontFamilies.Serif);
                currentFontFamily = sansSerifFontFamily;

                //添加“文件”菜单
                MenuItem miFile = mainMenu.MenuItems.Add("文件(&F)");
                miFile.MenuItems.Add(new MenuItem("打开(&O)...", new EventHandler(this.FileOpen_Clicked), Shortcut.CtrlO));
                miFile.MenuItems.Add("-");     // 提供一个分隔符
                miFile.MenuItems.Add(new MenuItem("退出(&X)", new EventHandler(this.FileExit_Clicked), Shortcut.CtrlX));

                //添加“格式”菜单
                MenuItem miFormat = mainMenu.MenuItems.Add("格式(&O)");

                //“字体外观”子菜单
                mmiSansSerif = new MenuItem("1.(&1)" + sansSerifFontFamily.Name, new EventHandler(this.FormatFont_Clicked));
                mmiSansSerif.Checked = true ;
                mmiSansSerif.DefaultItem = true ;
                mmiSerif = new MenuItem("2. (&2)" + serifFontFamily.Name, new EventHandler(this.FormatFont_Clicked));
                mmiMonoSpace = new MenuItem("3.(&3)" + monoSpaceFontFamily.Name, new EventHandler(this.FormatFont_Clicked));

                miFormat.MenuItems.Add( "字体外观(&F)",
                                        (new MenuItem[]{ mmiSansSerif, mmiSerif, mmiMonoSpace })
                                        );

                //“字体大小”子菜单
                mmiSmall = new MenuItem("小(&S)", new EventHandler(this.FormatSize_Clicked));
                mmiMedium = new MenuItem("中(&M)", new EventHandler(this.FormatSize_Clicked));
                mmiMedium.Checked = true ;
                mmiMedium.DefaultItem = true ;
                mmiLarge = new MenuItem("大(&L)", new EventHandler(this.FormatSize_Clicked));

                miFormat.MenuItems.Add( "字体大小(&S)",
                                        (new MenuItem[]{ mmiSmall, mmiMedium, mmiLarge })
                                        );

                //向标签上下文菜单添加格式
                //注意,因为菜单不能属于两个父级,所以必须添加一个复本
                label1ContextMenu.MenuItems.Add(miFormat.CloneMenu());

                // 设置上下文菜单项 - 使用它们选中和取消选中项
                cmiSansSerif = label1ContextMenu.MenuItems[0].MenuItems[0].MenuItems[0];
                cmiSerif = label1ContextMenu.MenuItems[0].MenuItems[0].MenuItems[1];
                cmiMonoSpace = label1ContextMenu.MenuItems[0].MenuItems[0].MenuItems[2];
                cmiSmall = label1ContextMenu.MenuItems[0].MenuItems[1].MenuItems[0];
                cmiMedium = label1ContextMenu.MenuItems[0].MenuItems[1].MenuItems[1];
                cmiLarge = label1ContextMenu.MenuItems[0].MenuItems[1].MenuItems[2];

                //用这些来跟踪被选中的菜单项
                //因为有菜单和上下文菜单,所以这会比较复杂
                miMainFormatFontChecked = mmiSansSerif;
                miMainFormatSizeChecked = mmiMedium;
                miContextFormatFontChecked = cmiSansSerif;
                miContextFormatSizeChecked = cmiMedium;

            }

            //“文件”->“退出”菜单项处理程序
            private void FileExit_Clicked(object sender, System.EventArgs e) {
                this.Close();
            }

            //“文件”->“打开”菜单项处理程序
            private void FileOpen_Clicked(object sender, System.EventArgs e) {
                MessageBox.Show("为什么这会打开文件?");
            }

            //“格式”->“字体”菜单项处理程序
            private void FormatFont_Clicked(object sender, System.EventArgs e) {
                MenuItem miClicked = (MenuItem)sender;

                miMainFormatFontChecked.Checked = false;
                miContextFormatFontChecked.Checked = false;

                if ( miClicked == mmiSansSerif || miClicked == cmiSansSerif ) {
                    miMainFormatFontChecked = mmiSansSerif;
                    miContextFormatFontChecked = cmiSansSerif;
                    currentFontFamily = sansSerifFontFamily;
                } else if (miClicked == mmiSerif || miClicked == cmiSerif) {
                    miMainFormatFontChecked = mmiSerif;
                    miContextFormatFontChecked = cmiSerif;
                    currentFontFamily = serifFontFamily;
                } else {
                    miMainFormatFontChecked = mmiMonoSpace;
                    miContextFormatFontChecked = cmiMonoSpace;
                    currentFontFamily = monoSpaceFontFamily;
                }

                miMainFormatFontChecked.Checked = true;
                miContextFormatFontChecked.Checked = true;

                label1.Font = new Font(currentFontFamily, fontSize);
            }

            //“格式”->“大小”菜单项处理程序
            private void FormatSize_Clicked(object sender, System.EventArgs e) {

                MenuItem miClicked = (MenuItem)sender;

                miMainFormatSizeChecked.Checked = false;
                miContextFormatSizeChecked.Checked = false;

                string fontSizeString = ((MenuItem)sender).Text;

                if (fontSizeString == "小(&S)") {
                    miMainFormatSizeChecked = mmiSmall;
                    miContextFormatSizeChecked = cmiSmall;
                    fontSize = FontSizes.Small ;
                } else if (fontSizeString == "大(&L)") {
                    miMainFormatSizeChecked = mmiLarge;
                    miContextFormatSizeChecked = cmiLarge;
                    fontSize = FontSizes.Large ;
                } else {
                    miMainFormatSizeChecked = mmiMedium;
                    miContextFormatSizeChecked = cmiMedium;
                    fontSize = FontSizes.Medium ;
                }

                miMainFormatSizeChecked.Checked = true;
                miContextFormatSizeChecked.Checked = true;

                label1.Font = new Font(currentFontFamily, fontSize);
            }

            /// <summary>
            ///    清理正在使用的所有资源。
            /// </summary>
            protected override void Dispose(bool disposing)
            {
               if (disposing) {
                    if (components != null) {
                        components.Dispose();
                    }
               }
               base.Dispose(disposing);
            }

            /// <summary>
            ///    设计器支持所必需的方法,不要使用
            ///    代码编辑器修改此方法的内容。
            /// </summary>
            private void InitializeComponent()
            {
                this.components = new System.ComponentModel.Container ();
                this.mainMenu = new System.Windows.Forms.MainMenu ();
                this.label1 = new System.Windows.Forms.Label ();
                this.label1ContextMenu = new System.Windows.Forms.ContextMenu ();
                this.Text = "Menus 'R Us";
                this.AutoScaleBaseSize = new System.Drawing.Size (5, 13);
                this.Menu = this.mainMenu;
                this.ClientSize = new System.Drawing.Size (392, 117);
                label1.BackColor = System.Drawing.Color.LightSteelBlue;
                label1.Location = new System.Drawing.Point (16, 24);
                label1.TabIndex = 0;
                label1.Anchor = (System.Windows.Forms.AnchorStyles) 13;
                label1.Size = new System.Drawing.Size (360, 50);
                label1.Text = "Right Click on me - I have a context menu!";
                label1.ContextMenu = this.label1ContextMenu;
                this.Controls.Add (this.label1);
            }

            /// <summary>
            /// 应用程序的主入口点。
            /// </summary>
            [STAThread]
            public static void Main(string[] args) {
                Application.Run(new Menus());
            }

        }
    }



    展开全文
  • 内容索引:VB源码,界面编程,菜单 VB窗体中动态添加、移除一个菜单项,适时操作,很简单的示例程序,VB爱好者可以参考一下,程序如上图示。
  • VB菜单添加小图标

    2018-11-05 09:45:50
    VB菜单添加小图标VB菜单添加小图标VB菜单添加小图标
  • VB动态添加菜单项内容,在VB6.0的窗口中,动态添加菜单项子项内容,支持创建菜单父类和子类,或创建分级的菜单,程序小巧,在VB6下直接编译即可。
  • VB向菜单添加图标的实例
  • excel vba 不规则窗体美化 极酷窗体效果 窗体添加菜单 快捷键大全
  • [img=https://img-bbs.csdn.net/upload/201601/09/1452303146_645301.jpg][/img] 如图所示,VB6中创建的Form2.0窗体不能用菜单编辑器创建菜单,怎么样可以方便快捷的为此窗体创建菜单
  • VB 开发的 ComboBox 窗体下拉框菜单选择源码,与普通的ComboBox其实差别并不大,个性之处就在于在下拉框的选项中,增加了一个图形标识,也就是加了一个图标,这样看上去似乎更形象、美观了一点,虽然就加了这么一个...
  • 怎样实现MDI窗体菜单上的“窗口菜单下面的功能如下图所示:要实现“窗口菜单下面的“1、窗口1”“2、窗口2”……这几个功能 这几个菜单都是动态添加的,一旦新建窗口,就会自动添加显示,关掉相关窗口后,又会...
    怎样实现MDI窗体菜单上的“窗口”菜单下面的功能
    
    如下图所示:要实现“窗口”菜单下面的“1、窗口1”“2、窗口2”……这几个功能
    这几个菜单都是动态添加的,一旦新建窗口,就会自动添加显示,关掉相关窗口后,又会消失
    在.net新建项中添加MDI父窗体,就会自动有这个功能,但是我找不到代码在哪里,晕死了!
    总共就那么点代码,就是找不到!,请高手给找找
    __________________________________________________________________________
    MenuStrip.mdiwindowslistitem属性设置为你要显的菜单下就可以了vb2005
    __________________________________________________________________________
    在MDIParent1.Designer.vb里面的''MenuStrip设置里面


    ''MenuStrip
    ''
    Me.MenuStrip.Items.AddRange(New System.Windows.Forms.ToolStripItem() {Me.FileMenu, Me.EditMenu, Me.ViewMenu, Me.ToolsMenu, Me.WindowsMenu, Me.HelpMenu})
    Me.MenuStrip.Location = New System.Drawing.Point(0, 0)
    [b]Me.MenuStrip.MdiWindowListItem = Me.WindowsMenu[/b] Me.MenuStrip.Name = "MenuStrip"
    Me.MenuStrip.Size = New System.Drawing.Size(632, 24)
    Me.MenuStrip.TabIndex = 5
    Me.MenuStrip.Text = "MenuStrip"


    MDIParent1.Designer.vb在解决方案资源管理器里面,选择显示详细信息就能看见
    __________________________________________________________________________
    在MDIParent1.Designer.vb里面的大约87行左右


    ''MenuStrip
    ''
    Me.MenuStrip.Items.AddRange(New System.Windows.Forms.ToolStripItem() {Me.FileMenu, Me.EditMenu, Me.ViewMenu, Me.ToolsMenu, Me.WindowsMenu, Me.HelpMenu})
    Me.MenuStrip.Location = New System.Drawing.Point(0, 0)
    [color=#008000][u]Me.MenuStrip.MdiWindowListItem = Me.WindowsMenu[/u][/color]
    Me.MenuStrip.Name = "MenuStrip"
    Me.MenuStrip.Size = New System.Drawing.Size(632, 24)
    Me.MenuStrip.TabIndex = 5
    Me.MenuStrip.Text = "MenuStrip"


    MDIParent1.Designer.vb在解决方案资源管理器里面,选择[size=14px]显示所有文件[/size]就能看见
    __________________________________________________________________________
    展开全文
  • 如题,给VB窗体上鼠标右键菜单上添加菜单图标,应该如何操作,请指点,谢谢。 如图: [img=https://img-bbs.csdn.net/upload/201406/28/1403923803_964368.jpg][/img] 如果一定需要源码,请跟贴回复,我再...
  • VB托盘菜单实例,使用自己的窗体制作系统托盘右键菜单,从及实现气泡提示功能,程序提供了在系统托盘添加图标,气泡弹窗,在托盘弹出右键菜单窗体置前,拦截系统消息等功能。而且用自制的窗体充当右键菜单,可以...
  • VB.net 动态添加菜单响应单击事件

    千次阅读 2007-06-13 23:10:00
    以前 VB 6.0 我要调一堆 API 感觉好过瘾 呵呵 现在找不到这种感觉了 哈!记得vb 6 的 时候生气用Win32写了个Clock 掉了上百...动态添加菜单在 .net 中很容易的。首先建立一个XML 文件用来存储 菜单信息 (本例使用 Data

    以前 VB 6.0 我要调一堆 API 感觉好过瘾 呵呵 现在找不到这种感觉了 哈!记得vb 6 的 时候生气用Win32写了个Clock  掉了上百个 API 连VB 6 的窗体都没用, 那个叫过瘾 一切都在控制中的感觉真好。看来我还是适合用 MASM Win32 的。哈哈 废话 到此结束。动态添加菜单在 .net 中很容易的。

    首先建立一个XML 文件用来存储 菜单信息 (本例使用 DataHelper.xml)
     
    <?xml version="1.0" encoding="utf-8" ?>
    <DataHelper>
      <Menu>
        <IsSeparator>0</IsSeparator>
        <IsTopMenu>0</IsTopMenu>
        <TopMenuItemIndex>2</TopMenuItemIndex>
        <SubMenuItemIndex></SubMenuItemIndex>
        <MenuID>1</MenuID>
        <ParentID>0</ParentID> 
        <Text>基本数据库(&amp;B)...</Text>
        <Image></Image>
        <ToolTipText></ToolTipText>
        <Visible>True</Visible>
        <Name>DataConvert</Name>
        <ShortcutKeys></ShortcutKeys>
      </Menu>
      <Menu>
        <IsSeparator>1</IsSeparator>
        <IsTopMenu>0</IsTopMenu>
        <TopMenuItemIndex>2</TopMenuItemIndex>
        <SubMenuItemIndex></SubMenuItemIndex>
        <MenuID>2</MenuID>
        <ParentID>0</ParentID>
      </Menu>

      <Menu>
        <IsSeparator>0</IsSeparator>
        <IsTopMenu>0</IsTopMenu>
        <TopMenuItemIndex>2</TopMenuItemIndex>
        <SubMenuItemIndex></SubMenuItemIndex>
        <MenuID>2</MenuID>
        <ParentID>0</ParentID>
        <Text>计生系统(&amp;J)...</Text>
        <Image></Image>
        <ToolTipText></ToolTipText>
        <Visible></Visible>
        <Name>DataConvert</Name>
        <ShortcutKeys>131140</ShortcutKeys>
      </Menu>

      <Menu>
        <IsSeparator>1</IsSeparator>
        <IsTopMenu>0</IsTopMenu>
        <TopMenuItemIndex>2</TopMenuItemIndex>
        <SubMenuItemIndex></SubMenuItemIndex>
        <MenuID>2</MenuID>
        <ParentID>0</ParentID>
      </Menu>

      <Menu>
        <IsSeparator>0</IsSeparator>
        <IsTopMenu>0</IsTopMenu>
        <TopMenuItemIndex>2</TopMenuItemIndex>
        <SubMenuItemIndex></SubMenuItemIndex>
        <MenuID>3</MenuID>
        <ParentID>0</ParentID>
        <Text>党员系统(&amp;D)...</Text>
        <Image></Image>
        <ToolTipText></ToolTipText>
        <Visible></Visible>
        <Name>DataConvert</Name>
        <ShortcutKeys></ShortcutKeys>
      </Menu>

      <Menu>
        <IsSeparator>1</IsSeparator>
        <IsTopMenu>0</IsTopMenu>
        <TopMenuItemIndex>2</TopMenuItemIndex>
        <SubMenuItemIndex></SubMenuItemIndex>
        <MenuID>2</MenuID>
        <ParentID>0</ParentID>
      </Menu>

      <Menu>
        <IsSeparator>0</IsSeparator>
        <IsTopMenu>0</IsTopMenu>
        <TopMenuItemIndex>2</TopMenuItemIndex>
        <SubMenuItemIndex></SubMenuItemIndex>
        <MenuID>4</MenuID>
        <ParentID>0</ParentID>
        <Text>民政系统(&amp;M)...</Text>
        <Image></Image>
        <ToolTipText></ToolTipText>
        <Visible></Visible>
        <Name>DataConvert</Name>
        <ShortcutKeys></ShortcutKeys>
      </Menu>
    </DataHelper>

    好了第一步开始,读取XML文件 我们使用 DataSet 来干这活,不过我是在 DataHerlper.Common 工程来干这事的。哇 分层阿!
    这个是个结构 我用的 看取那个Table

     

    Imports  System.Drawing
    Imports  System.Windows.Forms

    Namespace DataHelper.Common

        
    Public Enum DataHelperConfigTableName
            DropDownMenu 
    = 0
            Plugins 
    = 1
        
    End Enum


    End Namespace


    Imports  System.Data
    Imports  DataHelper.Common.Caching
    Imports  System.Windows.Forms

    Namespace DataHelper.Common
        
    Public Class SharedClass

            
    Public Shared Function GetDataHelperConfig(Optional ByVal TableName As DataHelperConfigTableName = DataHelperConfigTableName.DropDownMenu) As DataTable
                
    Dim strDataHelperConfigFile As String
                
    Dim ds As New DataSet
                
    Dim dt As New DataTable

                strDataHelperConfigFile 
    = Application.StartupPath & "ConfigDataHelper.xml" 'Application.StartupPath

                ds 
    = DataCache.GetCache("DataHelperConfig")
                
    If ds Is Nothing Then
                    
    Dim xmlReader As XmlTextReader = New XmlTextReader(strDataHelperConfigFile)
                    ds 
    = New DataSet
                    ds.ReadXml(xmlReader)
                    dt 
    = ds.Tables(TableName)
                    DataCache.Insert(
    "DataHelperConfig", ds, DataCache.CreateFileCacheDependency(strDataHelperConfigFile))
                
    Else
                    dt 
    = ds.Tables(TableName)
                
    End If
                
    Return dt
            
    End Function


        
    End Class

    End Namespace


    我们得到菜单信息了接下来就要动态添加菜单了 这又是一个工程(DataHelper.UserInterface) 分层吗
    ' **********************************************************************************************************
    '
     Programmer By Shadow (QQ:33512603)
    '
     User Interface MainMenu  Class
    '
     The MainMenu class create menu for localappliction 
    '
     Corpright(C) 2006 Jiang Jian
    '
    **********************************************************************************************************
    '
     程序设计:江建
    '
    **********************************************************************************************************

    Imports  System.IO
    Imports  System.Windows.Forms
    Imports  DataHelper.Common
    Imports  DataHelper.PluginsInterface

    Namespace DataHelper.UserInterface
        
    Public Class MainMenu
            
    Private MenuClick As New MainMenuClick

    Public Methods

    Private Methods

        
    End Class

    End Namespace



    ' **********************************************************************************************************
    '
     Programmer By Shadow (QQ:33512603)
    '
     User Interface MainMenuClick  Class
    '
     The MainMenuClick class response menu click event
    '
     Corpright(C) 2006 Jiang Jian
    '
    **********************************************************************************************************
    '
     程序设计:江建
    '
    **********************************************************************************************************

    Imports  System.Diagnostics
    Imports  System.Windows.Forms


    Namespace DataHelper.UserInterface

        
    Public Class MainMenuClick
            
    Private Declare Function SetParent Lib "user32" Alias "SetParent" (ByVal hWndChild As IntegerByVal hWndNewParent As IntegerAs Integer
            
    Private Declare Function SetWindowPos Lib "user32" (ByRef hwnd As IntegerByRef hWndInsertAfter As IntegerByVal x As IntegerByVal y As IntegerByVal cx As Integer

    ByVal cy As IntegerByVal wFlags As IntegerAs Integer

            
    Private Const SWP_NOSIZE = &H1
            
    Private Const SWP_NOMOVE = &H2

            
    Private DataConvert As New Process

    Public Methods

    Overrides Methods


        
    End Class

    End Namespace


    最后是测试 哈哈 终于完了 在你的主窗体下 加入一个 MenuScript 控件 名称为 MainMenu 这次测试未添加 顶级菜单 如果要添加顶级菜单 请修改XML文件 IsTopMenu 为1

       Private   Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            
    Dim test As New MainMenu
            test.CreateMenu(
    Me.MainMenu)
      
    End Sub

     

     

    展开全文
  • 内容索引:VB源码,界面编程,窗体,标题栏,菜单 程序说明:  系统菜单是点击窗体右上角图标时所弹出的菜单,这个菜单的选项一般是固定的,而本例通过API函数的调用改变了系统菜单的选项,在其中加入了新的菜单项。...
  • VB菜单中出现图标,在窗口菜单前面加入小图标,本例演示了怎样在菜单中加入图标的方法,例中使用了三个WINAPI函数,利用这三个函数,我们可以对菜单进行定义。例子的一个缺陷是没有使用两张不同的图标来表示复选的...
  • 版权声明:本文为博主原创文章,转载请在显著位置标明本文出处...双击添加MenuStrip,它会显示设计窗体的下面,此时可以进入菜单项的编辑: 当建好一个菜单项后,就可以在下面或者后面继续添加新的菜单项: ...
  • 系统菜单是点击窗体左上角图标时所弹出的菜单,这个菜单的选项一般是固定的,而本例通过API函数的调用改变了系统菜单的选项,在其中加入了新的菜单项。程序实现有两个关键: 1、通过AppendMenu和GetSystemMenu两个...
  • VB演示树控菜单各个Tree节点的添加和拖动删除功能,测试时,请根据右侧的选项操作,可从数据库中载入TreeView、增加一个节点,清空TreeView、删除节点等功能,在实际应用中,这个技巧相当实用。
  • # **VB.NET DesignSurface 右键菜单如何添加 (ContextMenuStrip)** [百度知道——窗体设计器添加右键菜单](https://zhidao.baidu.com/question/1305612275502458939.html "") ![图片说明]...
  • VB2005编了一个菜单窗口程序,想在这个窗口菜单添加一个帮助菜单,点击这个帮助菜单会弹出帮助窗口显示帮助内容或打开帮助文件.请教各位高手,怎样实现?多谢.
  • VB.Net程序设计:动态添加右键菜单和子菜单 具体分5步 1:定义一个ContextMenuStrip对象。 2:用withevents关键字声明一个ContextMenu对象Private WithEvents ContextMenu1 As System.Windows.Forms.ContextMenu...
  • VB添加IE右键菜单

    2007-10-02 15:05:00
    /--------------------------------------------------***************************************IE工具栏按钮和IE右键菜单(VB6)Autor:wgscd mail: wgscd@126.comDate:2007/09*************************************...
  • 版权声明:本文为博主原创文章,转载请在显著位置标明本文出处以及作者网名,未经作者允许不得用于商业目的。 ToolStrip工具条(有些地方称为i工具栏,个人认为不...可以看到控件自动设置为了靠窗体顶部,宽度和窗体...
  • VB添加menu控件/菜单栏/工具条

    千次阅读 2019-12-17 14:56:39
    一个意思。 自己很久以前写的工程,今天要...切换到对象窗口,单击工具-菜单编辑器。 完事儿。 另外要查看属性的话,上面那个步骤也可以,另外就是在右边属性栏选择下拉框, 就可以更改属性了。 完事儿。 ...
  • VB窗体标题栏中添加自定义按钮,类似最大化,最小化按钮一样,美化一下,看不出是用户添加的按钮,当然,本例是只是简单的进行了美化。
  • 合并菜单及工具栏不是新技术也不是新话题,我只是把网上流行的C#代码用VB改写了下,分享给习惯了VB的朋友。 代码不只适用于2010,以前的版本也可以跑,标题如此说只因为我提供的源码下载是VS2010的。 合并子窗体...
  • 版权声明:本文为博主原创文章,转载请在显著位置标明本文出处以及作者网名,...在vs编辑器中添加了StatusStrip后,它也显示在窗体的底部: 按下小箭头,我们可以选择在状态栏中加入4种控件: ToolStripStatu...
  • 版权声明:本文为博主原创...本节将讲到的ToolStripContainer相似于一个容器,可以包含菜单和工具条、状态栏(当然状态栏一般不会放在上面)。 在设计窗体中放入一个ToolStripContainer: 通常情况下,会设置D...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 15,290
精华内容 6,116
关键字:

如何向窗体添加菜单vb