none
VB如何制作菜单(不是MenuStrip) RRS feed

  • 问题

  •  

    首先要解决的问题:

    有什么控件是完全透明的么?比如我想让在程序的某个位置内点击鼠标的时候触发事件,但是这块区域由很多控件而且还有窗体的空白部分组成。并且这些控件只是需要显示而已,没有互动的作用。所以想找到一个透明的控件覆盖在这个地方。这样就只用编写一个控件的代码了。

     

    然后是主要问题:

    如何在这个控件(上面说的那个)上点击鼠标右键时让他弹出一个菜单,有运行,设置等选项呢?怎么做啊?

     

    我使用的是VB2008。能给2008版的代码最好。没有也可以,发上来再说。

     

    还有,最后问个小问题,VB2008 和 .NET有什么区别?为什么好像.net代码就是VB2008的代码?比如网上找来的代码中出现hwnd这个东西。放在2008里不能用,网上找到hwnd在.net里叫做handle,换掉就可以在2008里面用了。为什么?

     

     

    上面两个问题很急啊。如果这两个问题不能解决那么我整个软件的开发计划都要延迟了。这款软件是拿去参加比赛的啊。2月15日前要交作品。。。大家快帮帮我。另外,哪位高手愿意随时辅导我的加QQ:437509294.到时候填报名表的时候会填上软件中受他人帮助的地方和帮助人的联系方式的。而且在软件的制作团队中也将显示您的姓名或QQ或邮箱以示感谢

    2009年1月25日 5:13

答案

  •  

    首先要解决的问题:

    有什么控件是完全透明的么?比如我想让在程序的某个位置内点击鼠标的时候触发事件,但是这块区域由很多控件而且还有窗体的空白部分组成。并且这些控件只是需要显示而已,没有互动的作用。所以想找到一个透明的控件覆盖在这个地方。这样就只用编写一个控件的代码了。

     

     

    这个很可惜   标准控件没有  但是你可以把这些控件放在一个 panel中  然后用一个  for 循环给每个子控件增加事件监听

     

     

     

     

    如何在这个控件(上面说的那个)上点击鼠标右键时让他弹出一个菜单,有运行,设置等选项呢?怎么做啊

     

    ContextMenuStrip 这个控件 使用方法  在

     

    ms-help://MS.VSCC.v90/MS.MSDNQTR.v90.chs/dv_fxmclictl/html/9787cdb3-88f1-4198-972f-eefd9524ce39.htm

    ms-help://MS.VSCC.v90/MS.MSDNQTR.v90.chs/fxref_system.windows.forms/html/2594d669-e7b5-d326-693b-da988734cc93.htm

     

     

     

    ' This code example demonstrates how to handle the Opening event.
    ' It also demonstrates dynamic item addition and dynamic
    ' SourceControl determination with reuse.
    Class Form3
        Inherits Form

       ' Declare the ContextMenuStrip control.
       Private fruitContextMenuStrip As ContextMenuStrip


       Public Sub New()
          ' Create a new ContextMenuStrip control.
          fruitContextMenuStrip = New ContextMenuStrip()

          ' Attach an event handler for the
          ' ContextMenuStrip control's Opening event.
          AddHandler fruitContextMenuStrip.Opening, AddressOf cms_Opening

          ' Create a new ToolStrip control.
          Dim ts As New ToolStrip()

          ' Create a ToolStripDropDownButton control and add it
          ' to the ToolStrip control's Items collections.
          Dim fruitToolStripDropDownButton As New ToolStripDropDownButton("Fruit", Nothing, Nothing, "Fruit")
          ts.Items.Add(fruitToolStripDropDownButton)

          ' Dock the ToolStrip control to the top of the form.
          ts.Dock = DockStyle.Top

          ' Assign the ContextMenuStrip control as the
          ' ToolStripDropDownButton control's DropDown menu.
          fruitToolStripDropDownButton.DropDown = fruitContextMenuStrip

          ' Create a new MenuStrip control and add a ToolStripMenuItem.
          Dim ms As New MenuStrip()
          Dim fruitToolStripMenuItem As New ToolStripMenuItem("Fruit", Nothing, Nothing, "Fruit")
          ms.Items.Add(fruitToolStripMenuItem)

          ' Dock the MenuStrip control to the top of the form.
          ms.Dock = DockStyle.Top

          ' Assign the MenuStrip control as the
          ' ToolStripMenuItem's DropDown menu.
          fruitToolStripMenuItem.DropDown = fruitContextMenuStrip

          ' Assign the ContextMenuStrip to the form's
          ' ContextMenuStrip property.
          Me.ContextMenuStrip = fruitContextMenuStrip

          ' Add the ToolStrip control to the Controls collection.
            Me.Controls.Add(ts)

            'Add a button to the form and assign its ContextMenuStrip.
            Dim b As New Button()
            b.Location = New System.Drawing.Point(60, 60)
            Me.Controls.Add(b)
            b.ContextMenuStrip = fruitContextMenuStrip

          ' Add the MenuStrip control last.
          ' This is important for correct placement in the z-order.
          Me.Controls.Add(ms)
        End Sub

       ' This event handler is invoked when the ContextMenuStrip
       ' control's Opening event is raised. It demonstrates
       ' dynamic item addition and dynamic SourceControl
       ' determination with reuse.
        Sub cms_Opening(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs)

            ' Acquire references to the owning control and item.
            Dim c As Control = fruitContextMenuStrip.SourceControl
            Dim tsi As ToolStripDropDownItem = fruitContextMenuStrip.OwnerItem

            ' Clear the ContextMenuStrip control's
            ' Items collection.
            fruitContextMenuStrip.Items.Clear()

            ' Check the source control first.
            If (c IsNot Nothing) Then
                ' Add custom item (Form)
                fruitContextMenuStrip.Items.Add(("Source: " + c.GetType().ToString()))
            ElseIf (tsi IsNot Nothing) Then
                ' Add custom item (ToolStripDropDownButton or ToolStripMenuItem)
                fruitContextMenuStrip.Items.Add(("Source: " + tsi.GetType().ToString()))
            End If

            ' Populate the ContextMenuStrip control with its default items.
            fruitContextMenuStrip.Items.Add("-")
            fruitContextMenuStrip.Items.Add("Apples")
            fruitContextMenuStrip.Items.Add("Oranges")
            fruitContextMenuStrip.Items.Add("Pears")

            ' Set Cancel to false.
            ' It is optimized to true based on empty entry.
            e.Cancel = False
        End Sub
    End Class

     

     

     

     

    2009年1月27日 9:38

全部回复

  • VB6不支持.Net。2002年以后的VB只支持.Net。你找到的代码可能是VB6代码。
    你要找的可能是.Net的ContextMenuStrip属性。
    2009年1月25日 15:04
    版主
  •  

    首先要解决的问题:

    有什么控件是完全透明的么?比如我想让在程序的某个位置内点击鼠标的时候触发事件,但是这块区域由很多控件而且还有窗体的空白部分组成。并且这些控件只是需要显示而已,没有互动的作用。所以想找到一个透明的控件覆盖在这个地方。这样就只用编写一个控件的代码了。

     

     

    这个很可惜   标准控件没有  但是你可以把这些控件放在一个 panel中  然后用一个  for 循环给每个子控件增加事件监听

     

     

     

     

    如何在这个控件(上面说的那个)上点击鼠标右键时让他弹出一个菜单,有运行,设置等选项呢?怎么做啊

     

    ContextMenuStrip 这个控件 使用方法  在

     

    ms-help://MS.VSCC.v90/MS.MSDNQTR.v90.chs/dv_fxmclictl/html/9787cdb3-88f1-4198-972f-eefd9524ce39.htm

    ms-help://MS.VSCC.v90/MS.MSDNQTR.v90.chs/fxref_system.windows.forms/html/2594d669-e7b5-d326-693b-da988734cc93.htm

     

     

     

    ' This code example demonstrates how to handle the Opening event.
    ' It also demonstrates dynamic item addition and dynamic
    ' SourceControl determination with reuse.
    Class Form3
        Inherits Form

       ' Declare the ContextMenuStrip control.
       Private fruitContextMenuStrip As ContextMenuStrip


       Public Sub New()
          ' Create a new ContextMenuStrip control.
          fruitContextMenuStrip = New ContextMenuStrip()

          ' Attach an event handler for the
          ' ContextMenuStrip control's Opening event.
          AddHandler fruitContextMenuStrip.Opening, AddressOf cms_Opening

          ' Create a new ToolStrip control.
          Dim ts As New ToolStrip()

          ' Create a ToolStripDropDownButton control and add it
          ' to the ToolStrip control's Items collections.
          Dim fruitToolStripDropDownButton As New ToolStripDropDownButton("Fruit", Nothing, Nothing, "Fruit")
          ts.Items.Add(fruitToolStripDropDownButton)

          ' Dock the ToolStrip control to the top of the form.
          ts.Dock = DockStyle.Top

          ' Assign the ContextMenuStrip control as the
          ' ToolStripDropDownButton control's DropDown menu.
          fruitToolStripDropDownButton.DropDown = fruitContextMenuStrip

          ' Create a new MenuStrip control and add a ToolStripMenuItem.
          Dim ms As New MenuStrip()
          Dim fruitToolStripMenuItem As New ToolStripMenuItem("Fruit", Nothing, Nothing, "Fruit")
          ms.Items.Add(fruitToolStripMenuItem)

          ' Dock the MenuStrip control to the top of the form.
          ms.Dock = DockStyle.Top

          ' Assign the MenuStrip control as the
          ' ToolStripMenuItem's DropDown menu.
          fruitToolStripMenuItem.DropDown = fruitContextMenuStrip

          ' Assign the ContextMenuStrip to the form's
          ' ContextMenuStrip property.
          Me.ContextMenuStrip = fruitContextMenuStrip

          ' Add the ToolStrip control to the Controls collection.
            Me.Controls.Add(ts)

            'Add a button to the form and assign its ContextMenuStrip.
            Dim b As New Button()
            b.Location = New System.Drawing.Point(60, 60)
            Me.Controls.Add(b)
            b.ContextMenuStrip = fruitContextMenuStrip

          ' Add the MenuStrip control last.
          ' This is important for correct placement in the z-order.
          Me.Controls.Add(ms)
        End Sub

       ' This event handler is invoked when the ContextMenuStrip
       ' control's Opening event is raised. It demonstrates
       ' dynamic item addition and dynamic SourceControl
       ' determination with reuse.
        Sub cms_Opening(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs)

            ' Acquire references to the owning control and item.
            Dim c As Control = fruitContextMenuStrip.SourceControl
            Dim tsi As ToolStripDropDownItem = fruitContextMenuStrip.OwnerItem

            ' Clear the ContextMenuStrip control's
            ' Items collection.
            fruitContextMenuStrip.Items.Clear()

            ' Check the source control first.
            If (c IsNot Nothing) Then
                ' Add custom item (Form)
                fruitContextMenuStrip.Items.Add(("Source: " + c.GetType().ToString()))
            ElseIf (tsi IsNot Nothing) Then
                ' Add custom item (ToolStripDropDownButton or ToolStripMenuItem)
                fruitContextMenuStrip.Items.Add(("Source: " + tsi.GetType().ToString()))
            End If

            ' Populate the ContextMenuStrip control with its default items.
            fruitContextMenuStrip.Items.Add("-")
            fruitContextMenuStrip.Items.Add("Apples")
            fruitContextMenuStrip.Items.Add("Oranges")
            fruitContextMenuStrip.Items.Add("Pears")

            ' Set Cancel to false.
            ' It is optimized to true based on empty entry.
            e.Cancel = False
        End Sub
    End Class

     

     

     

     

    2009年1月27日 9:38
  •  

    谢谢,解决啦
    2009年1月28日 4:24
  •  完全透明的控件还是有的呀,例如Label,可以把Text设为"",把Autosize设为False.

    然后调整大小,放到所有控件之前,当然这也将挡住用户与后面控件产生交互的事件,只是能看到内容.

    (当然panel控件也是可以.)

    然后可以针对这个Label写事件.

     

    另外,若要写对多个控件的事件的处理函数,可以在Handles语句后面添加控件事件名称,例如:

     
    <P>Private Sub Text_KeyDown(ByVal sender As Object, ByVal e As   
    System.Windows.Forms.KeyEventArgs) Handles ComboBox1.KeyDown, ComboBox2.KeyDown,   
    ComboBox3.KeyDown, NumericUpDown4.KeyDown, NumericUpDown5.KeyDown,   
    ComboBox4.KeyDown, ComboBox5.KeyDown,&nbsp;TextBox1.KeyDown, TextBox2.KeyDown,   
    Button1.KeyDown, Button2.KeyDown</P> 

     

    也可以通过AddHandler在程序运行过程中动态关联事件

    • 已建议为答案 abcjackson 2009年1月30日 3:17
    2009年1月30日 3:17
  •  你的这个所谓透明

     

    namespace WindowsFormsApplication1
    {
        partial class Form1
        {
            /// <summary>
            /// 必需的设计器变量。
            /// </summary>
            private System.ComponentModel.IContainer components = null;

            /// <summary>
            /// 清理所有正在使用的资源。
            /// </summary>
            /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }

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

            /// <summary>
            /// 设计器支持所需的方法 - 不要
            /// 使用代码编辑器修改此方法的内容。
            /// </summary>
            private void InitializeComponent()
            {
                this.label1 = new System.Windows.Forms.Label();
                this.button1 = new System.Windows.Forms.Button();
                this.SuspendLayout();
                //
                // label1
                //
                this.label1.BackColor = System.Drawing.Color.Transparent;
                this.label1.Location = new System.Drawing.Point(57, 64);
                this.label1.Name = "label1";
                this.label1.Size = new System.Drawing.Size(100, 23);
                this.label1.TabIndex = 0;
                this.label1.Text = "label1";
                //
                // button1
                //
                this.button1.Location = new System.Drawing.Point(120, 59);
                this.button1.Name = "button1";
                this.button1.Size = new System.Drawing.Size(75, 23);
                this.button1.TabIndex = 1;
                this.button1.Text = "button1";
                this.button1.UseVisualStyleBackColor = true;
                //
                // Form1
                //
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(292, 273);
                this.Controls.Add(this.label1);
                this.Controls.Add(this.button1);
                this.Name = "Form1";
                this.Text = "Form1";
                this.ResumeLayout(false);

            }

            #endregion

            private System.Windows.Forms.Label label1;
            private System.Windows.Forms.Button button1;
        }
    }



     

     

     

    完全不能正确运行


    卑鄙是卑鄙者的通行证 技术需要通行证
    2009年1月30日 3:23
  • 韦恩卑鄙 说:

     你的这个所谓透明

     

    namespace WindowsFormsApplication1
    {
        partial class Form1
        {
            /// <summary>
            /// 必需的设计器变量。
            /// </summary>
            private System.ComponentModel.IContainer components = null;

            /// <summary>
            /// 清理所有正在使用的资源。
            /// </summary>
            /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }

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

            /// <summary>
            /// 设计器支持所需的方法 - 不要
            /// 使用代码编辑器修改此方法的内容。
            /// </summary>
            private void InitializeComponent()
            {
                this.label1 = new System.Windows.Forms.Label();
                this.button1 = new System.Windows.Forms.Button();
                this.SuspendLayout();
                //
                // label1
                //
                this.label1.BackColor = System.Drawing.Color.Transparent;
                this.label1.Location = new System.Drawing.Point(57, 64);
                this.label1.Name = "label1";
                this.label1.Size = new System.Drawing.Size(100, 23);
                this.label1.TabIndex = 0;
                this.label1.Text = "label1";
                //
                // button1
                //
                this.button1.Location = new System.Drawing.Point(120, 59);
                this.button1.Name = "button1";
                this.button1.Size = new System.Drawing.Size(75, 23);
                this.button1.TabIndex = 1;
                this.button1.Text = "button1";
                this.button1.UseVisualStyleBackColor = true;
                //
                // Form1
                //
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(292, 273);
                this.Controls.Add(this.label1);
                this.Controls.Add(this.button1);
                this.Name = "Form1";
                this.Text = "Form1";
                this.ResumeLayout(false);

            }

            #endregion

            private System.Windows.Forms.Label label1;
            private System.Windows.Forms.Button button1;
        }
    }



     

     

     

    完全不能正确运行


    卑鄙是卑鄙者的通行证 技术需要通行证



    :)非常抱歉,关于透明控件,VB6下面Label、GroupBox对这招是管用的,想不到VB2008不行了,一直没有留意,真是非常抱歉。

    不知道有什么办法可以让控件实现透明效果呢?我知道VB6时可以使用API实现的,VB2008就要请教各位了。

    2009年1月30日 5:17