📄 vb0105.htm
字号:
<HTML>
<head><title>洪恩在线 - 使用右键菜单</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<META HTTP-EQUIV="Pragma" CONTENT="no_cache">
<link rel="stylesheet" href="/pub/css/home.css">
<link rel="stylesheet" href="/pc/program/program.css">
<meta name="keywords" content="程序设计,趣味编程,源码,源程序,源码讲解,使用右键菜单,菜单,popmenu,VB,教程,电脑,教育,电脑乐园,交互">
<script language="javascript">
<!--
function change(myform)
{
var url="www.hongen.com/pc/program/index.htm";
url=myform.select.options[myform.select.options.selectedIndex].value;
top.location.href=url;
}
//-->
</script></head>
<body bgcolor="white" text="black" topmargin="10" leftmargin="0" marginwidth="0" marginheight="10">
<!--顶部开始--><script language=javascript src=/pub/js/head.js></script><!--顶部结束-->
<table width="760" height="20" border=0 cellspacing="0" cellpadding="0" align="center">
<tr valign="middle" align="left">
<td height=20 class="p1" width="297" bgcolor="#D3E7FF" align=center><div align="left"> <a href="/pc/program/index.htm" class=under>程序设计栏目首页</a></div></td>
<td align=left class="p1" height="20" width="58" valign="top"><img src="/pc/program/img/leftnew-0.gif" width="52" height="20"></td>
<td align="right" class="p1" height="20" width="405"><font color="#330099">当前位置:<a href="/default.htm" class="under">洪恩在线</a>
-> <a href="/pc/index.htm" class="under">电脑乐园</a> -> 使用右键菜单的例子</font><img name="image2" src="/pc/img/blank.gif" width="8" height="8">
</td>
</tr>
</table>
<table width=760 border=0 align="center" cellspacing="0" cellpadding="0">
<tr>
<td height=15><img src="/pc/img/blank.gif" width="1" height="1" border=0></td>
</tr>
</table>
<table width="760" border="0" cellspacing="0" cellpadding="0" align="center" height="909">
<tr>
<th width="150" bgcolor="6082CF" align="center" height="910" valign="top"> <!-- 左边条开始 -->
<script language="Javascript" src="/pc/program/js/proleft.js"></script>
<!-- 左边条结束 --><p align="center" class=p1> <a href=/pc/bin/msg.pl?file_id=k_tutor_vb target="_blank"><img src="/pc/img/message.gif" width="64" height="54" alt="请您留言" border="0"><br> 谈谈您的看法</a>
<br>
<script language="javascript">
speak_num="za"
</script>
<script language="javascript" src=/pc/var/pc_k_tutor_vb.js>
</script>
<script language="javascript">
var num=0
if (!isNaN(speak_num)){
num=speak_num;
}
else{
num=0
}
document.writeln ("已有")
document.writeln (num)
document.writeln ("条发言")
</script>
</p>
<br>
</th>
<td bgcolor="#000000" width="1" height="910"><img src="img/blank.gif" width="1" height="1"></td>
<td valign="top" align="center" height="910"> <br>
<table width="95%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td> </td>
<td width="13" valign="middle"><img src="/pc/program/img/dot.gif" width="13" height="13"></td>
<td class="p2" width="150" align="center" valign="middle"><font color="B85959"><b>编
程 入 门</b></font></td>
<td width="179"><img src="/pc/program/img/logo2.gif" width="179" height="52"></td>
</tr>
</table>
<table width="95%" border="0" cellspacing="0" cellpadding="0" class="p2">
<tr>
<td height="15" valign="bottom" colspan="3" class="p2">--看实例学VB6.0--<b>使用右键菜单的例子--</b></td>
<td height="15" valign="bottom" class="p2" width="23%">
<div align="right" class="p1">下一节</div>
</td>
</tr>
<tr>
<td bgcolor="FF9700" height="1" width="4%"><img src="img/blank.gif" width="1" height="2"></td>
<td bgcolor="FF9700" height="1" width="4%"><img src="img/blank.gif" width="1" height="2"></td>
<td bgcolor="FF9700" height="1" width="69%"><img src="img/blank.gif" width="1" height="2"></td>
<td bgcolor="FF9700" height="1" width="23%"><img src="img/blank.gif" width="1" height="2"></td>
</tr>
</table>
<table width="95%" border="0" cellspacing="0" cellpadding="0" class="p2" background="../../../img/line.gif">
<tr align="center">
<td class="p2" height="120" colspan="4">
<p align="left"><span class="p2" style="line-height:17pt"> 右键菜单能让软件的使用者快捷的完成操作,那么如何把右键菜单用在自己写的程序中呢?在这个小程序中我们能看到编辑并使用右键菜单的方法。</span></p>
<p align="left"><span class="p2" style="line-height:17pt"> 为了学习方便,提供的源码已经作了详细的中文注释,看看源码框中的代码:</span></p>
<div align="center">
<p>
<textarea name="textfield" cols="80" rows="7" onMouseOver="this.focus()" onFocus="this.select()" readonly>'--------------------------------------
' 弹出鼠标右键菜单
'--------------------------------------
' 洪恩在线 求知无限
'--------------------------------------
'------名称-------------作用------------
' 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
</textarea>
<br>
(<a href="/pc/program/tutors/vb/zip/vb0104.zip">源程序下载</a>)</p>
<p align="left"><span class="p2" style="line-height:17pt"> 我们先打开菜单编辑器(在工具菜单中),然后添加一个一级菜单Popmenu,将它的Visible属性设为“False”,这代表菜单在程序运行时是看不到的,由于一级菜单是二级菜单的上级菜单,所以二级菜单也是看不到的。然后我们来添加几个二级菜单,注意每个菜单都有Caption属性和Name属性,这两个属性是必须写的,Caption属性是显示在菜单项上的内容,而Name属性则是我们要在程序中引用菜单项的代号,类似与其它控件的Name属性。而菜单中分隔线是通过把Caption属性设为“-”来实现的。</span></p>
<p align="center"><img src="img/edit.gif" width="366" height="366" alt="编辑菜单"></p>
<p align="left"><span class="p2" style="line-height:17pt"> 菜单编辑好后,就能在程序中引用并控制它,在本例中我们希望在文本框上点击鼠标右键时弹出这个右键菜单,则需要在文本框的MouseDown事件中加入控制语句:</span></p>
<p align="left"><span class="p1" style="line-height:17pt"> Private
Sub RichTextBox1_MouseDown(Button As Integer,Shift As Integer,X
As Single,Y As Single) <br>
If Button = 2 Then '如果是鼠标右键按下时<br>
Form1.PopupMenu mnufile, 0, X, Y '弹出右键菜单<br>
End If<br>
End Sub</span></p>
<p align="left"><span class="p2" style="line-height:17pt"> 其中MouseDown事件各个参数意义如下:</span></p>
<p align="left"><span class="p1" style="line-height:17pt"> button返回一个整数,用来标识该事件的产生是按下哪个按键,其中左键为0,右键为2,中间键为4
<br>
shift 返回一个整数,标示是否同时有Shift,Ctrl,Alt键按下<br>
x, y 返回一个指定鼠标指针当前位置的数</span></p>
<p align="left"><span class="p2" style="line-height:17pt"> Popupmenu是一个方法:</span></p>
<p align="left"><span class="p1" style="line-height:17pt"> PopupMenu方法用来弹出一个菜单,语法是:object.PopupMenu
menuname, flags, X, Y<br>
mnufile是我们在菜单编辑器中设计好的菜单的名称<br>
X,Y是弹出菜单的位置,可以为数字,如果直接写为X,Y则是在当前鼠标位置弹出菜单</span></p>
<p align="left"><span class="p2" style="line-height:17pt"> 这样就能调出右键菜单,然后我们根据自己的需要,为每个菜单项的Click事件编写代码,完成一些特定的操作,右键菜单就可以使用了。最后效果如下图所示:</span></p>
<p align="center"><img src="img/yanshi.gif" width="324" height="153" alt="效果演示"></p>
</div>
<div align="center"></div>
</td>
</tr>
<tr>
<td bgcolor="FF9700" height="2" width="5%"><img src="img/blank.gif" width="1" height="2"></td>
<td bgcolor="FF9700" height="2" width="5%"><img src="img/blank.gif" width="1" height="2"></td>
<td bgcolor="FF9700" height="2" width="29%"><img src="img/blank.gif" width="1" height="2"></td>
<td bgcolor="FF9700" height="2" width="61%"><img src="img/blank.gif" width="1" height="2"></td>
</tr>
</table>
<table width="95%" align="center" class="p1" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="16%">
<div align="center"><a href="/pc/program/index.htm">程序设计首页</a></div>
</td>
<td width="16%">
<div align="center"><a href="/pc/program/tutors/index.htm" color="#009999">编程入门首页</a></div>
</td>
<td width="41%">
<div align="left">相关链接: <a href="/pc/program/codtutor/index.htm">VB趣味程序讲解</a></div>
</td>
<td width="16%">
<div align="right"><a href="/pc/program/tutors/vb/vb0104.htm">上一节</a> 下一节</div>
</td>
</tr>
</table>
</td>
</tr>
</table>
<!--底部开始-->
<script language="Javascript" src="/pub/js/foot.js"></script>
<!--底部结束--></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -