网摘》PopupMenu

VB PopupMenu方法
 

PopupMenu方法用来显示弹出菜单,语法格式为:

object.PopupMenu menuname,flags,x, y,boldcommand

其中:

Object(对象)——窗体名。

Menuname(菜单名)——指在菜单编辑器中定义的主菜单项名。

X、Y——弹出式菜单在窗体上的显示位置的X、Y坐标(与Flags参数配合使用)。

Boldcommand——指定弹出式菜中的弹出式菜单控件的名字,用以显示为黑体正文标题。

Flags——该参数是一个数值或符号常量,指定弹出式菜单的位置和行为,其取值分为两组,一组用来指定菜单位置,另一组用来定义特殊的菜单行为,如下表:

指定菜单位置

定位常量作用
VbPopupMenuLeftAlign0X坐标指定弹出式菜单的左边界位置
VbPopupMenuCenterAlign4X坐标指定弹出式菜单的中间位置
VbPopupMenuRightAlign8X坐标指定弹出式菜单的右边界位置

          定义菜单行为

定位常量作用
VbPopupMenuLeftButton0通过单击鼠标左键选择菜单命令
VbPopupMenuRightButton8通过单击鼠标右键选择菜单命令

说明:

VB PopupMenu方法 - za147852 - za147852的博客 PopupMenu方法的6个参数中,除“菜单名”外,其余参数都是可选的。当省略了“对象”时,弹出式菜单只能在当前窗体中显示。如果需要在其它窗体中显示弹出菜单,则必须加上窗体名。

VB PopupMenu方法 - za147852 - za147852的博客 Flags的两组参数可以单独使用,也可以联合使用。当联合使用时,每组中取一个值,两个值相加;如果使用符号常量,则两个值用Or连接。

VB PopupMenu方法 - za147852 - za147852的博客 X、Y分别用来指定弹出式菜单显示位置的横、纵坐标,如果省略,则弹出菜单在鼠标光标的当前位置显示。

VB PopupMenu方法 - za147852 - za147852的博客 弹出式菜单的“位置”由X、Y、Flags参数共同指定。如果省略这几个参数,则在单击鼠标右键弹出菜单时,鼠标光标所在位置为弹出式菜单左上角的坐标。在默认情况下,以窗体的左上角为坐标原点。如果只省略Flags参数,不省略X、Y参数,则X、Y为弹出式菜单左上角的坐标;如果同时使用X、Y及Flags参数,则弹出菜单的位置分为一下几种情况:

Flags=0        X、Y为弹出式菜单左上角的坐标

Flags=4        X、Y为弹出式菜单顶边中间的坐标

Flags=8        X、Y为弹出式菜单右上角的坐标

VB PopupMenu方法 - za147852 - za147852的博客 为了显示弹出式菜单,通常把PopupMenu方法放在MouseDown事件中,该事件响应所有的鼠标单击操作。按照惯例,一般通过单击鼠标右键显示弹出菜单,这可以用Button参数来实现。对于两个键的鼠标来说,左键的Button参数值为1,右键的Button参数值为2。因此可以强制使用右键来响应MouseDown事件而显示弹出菜单:

   If Button=2 Then PopupMenu 菜单名

 

Popup Menu Example(弹出菜单实例)

This example shows you how to display a popup menu...以下是演示如何显示一个弹出菜单

First, you need to add a number of controls:首先,你要添加若干控件

TypeNameCaption
LabellblPopupRight click for popup menu右键出弹出菜单
LabellblDefaultPopupRight click for popup menu with default item
LabellblShowPopupAtXRight click here to display popup at X
LabellblXX
MenumnuTestTest Menu测试菜单
MenumnuTestItem1Test Item 1
MenumnuTestItem2Test Item 2

Make sure that mnuTestItem 1 and 2 are sub menus of mnuTest. Now, add the code below, run your project, and try right clicking on each of the labels.

Private Sub lblDefaultPopup_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If Button = vbRightButton Then '// is right button?
        PopupMenu mnuTest, , , , mnuTestItem1 '// display popup menu
    End If
End Sub
Private Sub lblPopUp_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If Button = vbRightButton Then '// is right button?
        PopupMenu mnuTest '// show popup
    End If
End Sub
Private Sub cmdHidePopup_Click()
    mnuTest.Visible = False '// hide the popup menu from the main menu bar
End Sub
Private Sub lblShowPopupAtX_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If Button = vbRightButton Then
        '// get the coordinates of the X label (x and y)
        PopupMenu mnuTest, , lblX.Left, lblX.Top '// display menu
    End If
End Sub

 

VB实现窗口的弹出式菜单

《A》

在Windows98或Windows2000的风格中,有按动鼠标器右键弹出下拉菜单的操作,在VB执行环境下,有一些控件本身具有弹下拉菜单的功能,如TexTbox控件等,但大多数编辑类控件以及窗体本身却没有此功能,要在窗口中任意位置实现PopUpMenu(弹出式菜单),可借助VB的菜单工具来实现。
首先,打开VB的“工具”菜单条,利用“菜单编辑器”为窗体生成一个菜单:
标题       (Caption)        菜单条名   (Name)
编辑       menuEdit
复制       mnuCopy
剪切       munCut
其次,将生成的menuEdit菜单设置为不可视。
Private Sub Form_Load()
Me.menuEdit.Visibe=False 
‘menuEdit菜单设置为不可视
End Sub
然后,利用MouseDown事件实现任意位置弹出PopUpMenu,如下例:
本例以RichTexBox控件为例,实现复制、剪切功能。
1.复制功能的实现。
Private Sub MnuCopy_Click()
Clipboar.Clear′将剪贴板清空
′将RichTexBox控件上选择上的内容复制到剪贴板
Clipboard.SetTextRichTexBox1.SelText
End Sub
2.剪切功能的实现。
Private Sub MnuCut_Click()
Clipboard.Clear ′将剪贴板清空
′将RichTextBox控件上选择了的内容复制到剪贴板
Clipboard.SetTextRichBox1.SelText
′将RichTexBox控件上选择了的内容删除
SendKeys″{DELETE}″,True
End Sub
3.在RichTexBox控件的MouseDown事件中实现任意位置弹出PopUPMenu。
OPrivate Sub RichTexBox1_MouseDown(Button As Integer, _
Shift As Integer,x As Single,Y As Single)
Dim MnuFile AS Menu                  ′声明一个菜单类型的变量
Set munFile=Me.MenuEdit           ′将MenuEdit赋给菜单变量
if Button=2 Then ′判断是否按动鼠标器右键
If Len(RichTexBox.Text)=0 Or Len(RichTexBox.SelText)=0 Then
mnuCopy.Enabled=Flase    ′复制菜单条不可操作。
nmuCut.Enabled=False      ′剪切菜单条不可操作。
nmuCopy.Enabled=True     ′复制菜单条可操作。
nmuCut.Enabled=True       ′剪切菜单条可操作。
End If
PopupMenu mnuFile         ′弹出PopUpMenu。
End If
End Sub
这样,在VB执行环境中,操作窗体上的RichTexBox控件,按动鼠标器右键就可弹出下拉菜单,实现复制、剪切功能。

 《B》

 在VB中通用的方法如下:

分两个步骤:

1. 利用VB的选单编辑器(Menu Editor)编辑你希望弹出的选单及子选单(PoPMenu),注意,要将选单的Visible属性设置为:False。
2. 在窗体(Form1)的MouseDown事件中编写程序,来激发编辑好的选单,假设选单名为PopMenu,程序源码如下:

Private Sub Form-MouseDown  (Button As Integer, Shift As Integer,  X As Single, Y As Single
     If Button = vbRightButton Then 
          PopMenu.Visible = True 
          PopupMenu PopMenu 
      End If 
End Sub

上述方法是针对窗体的,我们也可以针对任意控件,用鼠标右键点击控件时,也弹出一个快捷选单。方法也很简单,只要把上述代码放到相应控件的MouseDown事件中,就可以了。

  右键菜单能让软件的使用者快捷的完成操作,那么如何把右键菜单用在自己写的程序中呢?在这个小程序中我们能看到编辑并使用右键菜单的方法。

'--------------------------------------
'           弹出鼠标右键菜单
'--------------------------------------
'           洪恩在线 求知无限
'--------------------------------------
'------名称-------------作用------------
'       CmdCancel       退出按钮
'       mnublue         “兰色”菜单项
'       mnured          “红色”菜单项
'       RichTextBox1    文本框
'       PopupFrm        主窗体
'       mnufile         右键菜单的名字
'--------------------------------------

Private Sub CmdCancel_Click()

Unload Me

End Sub


'当弹出式菜单的“红色”项被点击时
Private Sub mnured_Click()
'把RichTextBox框中的背景色设置为红色
RichTextBox1.BackColor = vbRed

End Sub

'当弹出式菜单的“兰色”项被点击时
Private Sub mnublue_Click()
'把RichTextBox框中的背景色设置为兰色
RichTextBox1.BackColor = vbBlue

End Sub

'当文本框上出现鼠标按下的事件时
Private Sub RichTextBox1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
'MouseDown事件各种语法包含下列部分:
'button  返回一个整数,用来标识该事件的产生是按下哪个按钮
'其中 左按钮(位 0),右按钮(位 2),以及中间按钮(位 4)
'shift   返回一个整数,标示是否同时有Shift,Ctrl,Alt键按下
'x, y    返回一个指定鼠标指针当前位置的数
'Button = 2 表示右键按下
If Button = 2 Then
'PopupMenu方法用来弹出一个菜单
'语法是 object.PopupMenu menuname, flags, X, Y
'mnufile是我们在菜单编辑器中设计好的菜单
'X,Y是弹出菜单的位置,可以为数字,如果直接写为X,Y则是在当前鼠标位置弹出菜单
PopupFrm.PopupMenu mnufile, 0, X, Y
End If

End Sub


  我们先打开菜单编辑器(在工具菜单中),然后添加一个一级菜单Popmenu,将它的Visible属性设为“False”,这代表菜单在程序运行时是看不到的,由于一级菜单是二级菜单的上级菜单,所以二级菜单也是看不到的。然后我们来添加几个二级菜单,注意每个菜单都有Caption属性和Name属性,这两个属性是必须写的,Caption属性是显示在菜单项上的内容,而Name属性则是我们要在程序中引用菜单项的代号,类似与其它控件的Name属性。而菜单中分隔线是通过把Caption属性设为“-”来实现的。

  菜单编辑好后,就能在程序中引用并控制它,在本例中我们希望在文本框上点击鼠标右键时弹出这个右键菜单,则需要在文本框的MouseDown事件中加入控制语句:

 Private Sub RichTextBox1_MouseDown(Button As Integer,Shift As Integer,X As Single,Y As Single)
 If Button = 2 Then          '如果是鼠标右键按下时
  Form1.PopupMenu mnufile, 0, X, Y  '弹出右键菜单
 End If
 End Sub

  其中MouseDown事件各个参数意义如下:

 button返回一个整数,用来标识该事件的产生是按下哪个按键,其中左键为0,右键为2,中间键为4
 shift 返回一个整数,标示是否同时有Shift,Ctrl,Alt键按下
 x, y 返回一个指定鼠标指针当前位置的数

  Popupmenu是一个方法:

 PopupMenu方法用来弹出一个菜单,语法是:object.PopupMenu menuname, flags, X, Y
 mnufile是我们在菜单编辑器中设计好的菜单的名称
 X,Y是弹出菜单的位置,可以为数字,如果直接写为X,Y则是在当前鼠标位置弹出菜单

  这样就能调出右键菜单,然后我们根据自己的需要,为每个菜单项的Click事件编写代码,完成一些特定的操作,右键菜单就可以使用了。

 

 

treeview 单击右键功能,怎么实现

工具-菜单编辑器
添加菜单mnu1并取消其可见的复选
给mnu1添加子菜单
Private Sub TreeView1_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)
    If Button = 2 Then PopupMenu mnu1
End Sub

Private Sub TreeView1_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)
   If Button = vbRightButton Then
      PopupMenu mnuYourMenu
   End If
End Sub

 

Visual Basic 6.0 TreeView控件及右击弹出菜单Demo

Visual Basic 6.0 TreeView控件及右击弹出菜单Demo

1.       新建一exe工程

2.       Ctrl+T打开组件对话框,将Microsoft Windows Common Controls 6.0(SP 6)添加进项目工程

3.       在窗体中添加一TreeView 将其命名为tvOrg

4.       右击窗体,选择编辑菜单,打开菜单编辑器。添加打开(mOpen),删除(mDelete) ,退出(mExit)菜单项。

5.       添加如下Code

Option Explicit

Dim sKey As String

Dim sValue As String

Private Sub Form_Load()

 

    Dim Node As Node

   

    With tvOrg

        .Nodes.Add , , "K10", "China"

       

        .Nodes.Add "K10", tvwChild, "K1001", "AnHui"

        .Nodes.Add "K1001", tvwChild, "K100101", "HeFei"

       

        .Nodes.Add "K10", tvwChild, "K1002", "JiangSu"

        .Nodes.Add "K1002", tvwChild, "K100201", "NanJing"

 

        .Nodes("K10").Expanded = True

 

    End With

   

End Sub

 

Private Sub mDelete_Click()

    MsgBox "Key=" & sKey & " Value=" & sValue, vbInformation + vbOKOnly

End Sub

Private Sub mExit_Click()

    MsgBox "Key=" & sKey & " Value=" & sValue, vbInformation + vbOKOnly

End Sub

 

Private Sub tvOrg_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)

    Dim nodeSelected As Node

    If Button = vbRightButton Then

        Set nodeSelected = tvOrg.HitTest(x, y)

       

        If nodeSelected Is Nothing Then Exit Sub

       

        nodeSelected.Selected = True

       

        sKey = nodeSelected.Key

        sValue = nodeSelected.Text

       

        Me.PopupMenu mOpen

    End If

End Sub

【注意】

1.  TreeView中添加结点时,结点的Key不能为数值类型,需要将数值类型转化一下再存入。

2.  TreeViewMouseDown事件中判断哪一个结点被选中是通过TreeViewHitTest(x, y)方法返回的结点与TreeView选中的结点加以比较,如果一样表示为右击事件。

 3.     在添加结点(父、子、兄弟,根结点可直接加入)内容时,可以构建邻接矩阵(关系矩阵),以此简化编程

 

 

一段英文对话,关于popumeun与treeview

Calling A Click On A Treeview...

I am trying to call the sub that takes place when a user clicks on an item. This one:

Code:

Private Sub TreeView_NodeClick(ByVal Node As MSComctlLib.Node)

‘**I do not know how to give it the "ByVal Node" it requires.

‘**I have tried this:

TreeView.Nodes.Item(1).Selected = True

TreeView.SetFocus

call TreeView_NodeClick()

‘**But it does not actually call the code that is in the NodeClick sub?

‘**Any ideas how i can pass the info it needs?

Cheers.

Right-Click TreeView Node!

A TreeView looks like this:

Code:

Asia

|____China

|____India

|____Japan

Europe

|____France

|____Germany

|____Sweden

South America

|____Argentina

|____Brazil

|____Paraguay

Australia

|___No countries

If any parent node (i.e. the continents) is right-clicked & the parent node is in a collapsed state, a menu pops-up with 2 items - Delete & Expand. On the other hand, if the parent node is in the expanded state, the popup menu shows 2 items - Delete & Collapse.

Users can even delete the nodes - be it a parent node or a child node. If a parent node doesn't have any children (i.e. any country), expanding that parent node shows only 1 child node whose text is No countries.

What I want is if a parent node has no children i.e. it doesn't have any country name as a child & consequently has only the No countries node, then the Delete menu item when the No countries node is right-clicked should be disabled but the Delete menu item should remain enabled when the parent node (i.e. Australia in the TreeView shown above) is right-clicked. This is what I tried:

VB Code:

Private Sub TreeView1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) 

   Dim objNode As Node

     Set objNode = TreeView1.HitTest(X, Y)

If (Button = vbRightButton) Then

        If Not (objNode Is Nothing) Then

            If (objNode.Child.Text = "No countries") Then

                mnuDelete.Enabled = False

            End If

        End If

    End IfEnd

 Sub

When the No countries node is right-clicked, VB generates the Object variable or With block variable not set error. Even the Delete menu item remains disabled when Australia is right-clicked whereas I want it to be enabled.

What am I missing in the above code?

Click TreeView Node!

In a WebBrowser project, a TreeView is used to list the History of the URLs a user has visited. Other than left-clicking any node in the TreeView to navigate to the URLs, users can also navigate by dragging & dropping a node in the WebBrowser. This is how I am implementing it:

VB Code:

Private Sub TreeView1_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)

If (Button = vbLeftButton) Then

        TreeView1.SelectedItem = TreeView1.HitTest(x, y)

        TreeView1.Drag vbBeginDrag

    End If

End Sub

Private Sub wWeb_DragDrop(Source As Control, x As Single, y As Single)

If Not (Source.SelectedItem Is Nothing) Then

        wWeb.Navigate2 Source.SelectedItem.Tag

    End If

End Sub

The above code implements the drag-drop feature exactly as I want it but strangely, the WebBrowser doesn't navigate to the Tag of the node (which is the URL) when a node is just left-clicked.

What am I missing here?

TREEVIEW <double Click>

How can I program in the Treeview Function to perform the following ?

-> to run "program A" when I double click on the "parent"

-> to run "program B" when I double click on the "child"

Treeview Child Click & Key

I have the following code which populates my treeview. The issue now is when I click on a child node, I want the key from that record to populate a series of text boxes – which is simple enough.

To test this I have it displaying a Msgox when I click the child and I am getting a combination of two values now, the key and the value of fld_Board_1.

e.g. – "5Accessibility" – I just want the key – fld_Ref_Num

I could simply grab the first character, but what happens when the key is 2 or 3 digits long...

While Not adoPrimaryRS.EOF

If dteCurrent <> adoPrimaryRS.Fields("fld_Date") Then

'Add Parent Node

Set DateNode = tvwNominations.Nodes.Add(, adoPrimaryRS.Fields("fld_Date"), adoPrimaryRS.Fields("fld_Date"), adoPrimaryRS.Fields("fld_Date"), 1)

dteCurrent = adoPrimaryRS.Fields("fld_Date")

End If

 

'Add Child Node

tvwNominations.Nodes.Add DateNode, tvwChild, adoPrimaryRS.Fields("fld_RefNum") & adoPrimaryRS.Fields("fld_Board_1"), adoPrimaryRS.Fields("fld_Board_1"), 2

Right Click Select In Treeview

I want the user to be able to right click select a node (then a popup menu shows)

right now I have to left click to select it then right click for the popup

Dbl-Click On Treeview Children

How can I tell if when I dbl-click on a treeviews child? I need to trap this so I can execute another set of code.

Treeview Click Command

how can I made a label contain what the user clicks on in the treeview control

Treeview And Right Click Menu

In the MouseUp event of the treeview, conduct a HitTest to see if you are over a node, and if you are, then show the menu. eg:

[code]

Set lItem = Treeview1.HitTest( X, Y)

If Not lItem is Nothing then

PopUpMenu MenuName1, , X, Y

End If

[code]

I didn't plug that through VB, so I may have got my parameteres in the wrong order, but you get the drift

Cheers

To Specified A Node In Treeview On Click

Loop through all the computers and add them to the treeview

If ServerList.Init Then

 

Dim rt As Node

Dim ch As Node

Dim strServer As String

Dim t

Set rt = TreeView1.Nodes.Add(, , , "Sistem Kawalan Makmal")

For intIDX = 1 To UBound(ServerList.List)

strServer = ServerList.List(intIDX).ServerName

t = strServer & intIDX

Set ch = TreeView1.Nodes.Add(rt, tvwChild, t, strServer)

rt.Expanded = True

Next

End If

The above is my treeview to display computers connected to the network. Now i would like to find out the code to enable me to specified the name of the particular computer when i click on its name in the treeview. Please help me this is very urgent and important to me.. Thanks a lot

A Bug In TreeView's ItemClick Event

There seems to be a bug with TreeView control. Look at the attached project.

If there is a message box in the ItemClick event the item you clicked "sticks" to the mouse and it continues acting like you are still holding the mouse button, dragging it.

The list on the left (in the attached project) shows the bug, and the list on the right (without message box) show that the same list (with same properties) works ok.

Has anybody expirienced this before, and maybe found a workaround for this problem?

MouseDown Event On TreeView

Avatar, please be warned that Access has some weird ideas about Win32 Mouse events (and focus events too).

I'm afraid I don't have many answers for you this time, because I gave up on Access's VBA interface ages ago

TreeView MouseDown Event

The following syntax, used with a TreeView control, doesn't work with Access 97 :

Private Sub MyTreeView_MouseDown(button As Integer, shift As Integer, x As Single, Y As Single)

...'Code

End Sub

Does somebody know why

Using The Treeview NodeClick Event

I am getting the error

"object variable or with block variable not set."

when I click on a node in my treeview.

Code:

Private NodeClick AS Node

Private Sub TreeView_NodeClick(ByVal Node As Node)

On Error GoTo NodeClickError

nodeClick.Key = Node.Key

nodeClick.Parent = Node.Parent

nodeClick.Text = Node.Text

RaiseEvent Click

Exit Sub

NodeClickError:

MsgBox "Err in NodeClick. Err: " & Err.Description, vbOKOnly, "You Got Error"

Exit Sub

End Sub

The point here is to be able to get the clicked Node property passed up to the application using this control.

Thanks,

Treeview Before Expand Event

I am stuck with a problem,please help me.
I have a requirement in which i have some nodes in a treeview and When the treeview is populated the first time I do not populate the child nodes.I only add dummy nodes in the begining. Only when the user clicks on the "+" sign i delete the dummy node and add the actual child nodes. While doing this,as soon as the user clicks on the parent node a messagebox is fired asking the user whether to continue. The coding for the messagebox is writen in the expand event of the treeview .The problem is when the user clicks on the "+" sign the messagebox is fired but at the same time the dummy node which was added as the child node is also visible(which is not favourable). So please suggest a solution where I can fire the message box before the child node(Dummy Node) is visible. Is there anything through which i can access the before expand event of a treeview node.

Thanks in Advance

Regards

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值