open
说明
示例
在指定位置打开上下文菜单
创建一个 UI 图窗。创建一个包含两个子菜单的上下文菜单,并将其分配给该 UI 图窗。
fig = uifigure; cm = uicontextmenu(fig); m1 = uimenu(cm,'Text','Import Data'); m2 = uimenu(cm,'Text','Export Data'); fig.ContextMenu = cm;
然后,在位置 (250,250)
上打开上下文菜单。
open(cm,250,250)
打开未分配的上下文菜单
当您右键点击作为父容器的 UI 图窗的空白区域或支持 ButtonDownFcn
属性的图形对象时,将打开一个未分配的上下文菜单。
首先,创建一个名为 openCtxtMenu.m
的程序文件。在该程序文件中:
在一个 UI 图窗中创建 UI 坐标区并在坐标区中绘制数据。
在 UI 图窗中创建具有一个子菜单的上下文菜单。
将
WindowButtonDownFcn
属性设置为名为onButtonDown
的回调函数。创建一个名为
onButtonDown
的回调函数。在其中,通过查询 UI 图窗的SelectionType
属性来确定选择是否为右键点击。在右键点击时,从CurrentPoint
属性获取鼠标指针的 x 和 y 坐标。x 和 y 坐标是它返回的向量的第一个和第二个元素。然后,在这些坐标处打开上下文菜单。当出现其他选择类型时,在命令行窗口中显示一条消息。
function openCtxtMenu fig = uifigure; ax = uiaxes(fig); plot(ax,magic(5)); cm = uicontextmenu(fig); m = uimenu(cm,'Text','Menu1'); fig.WindowButtonDownFcn = @onButtonDown; function onButtonDown(src,event) clickType = src.SelectionType; switch clickType case 'alt' x = src.CurrentPoint(1); y = src.CurrentPoint(2); open(cm,x,y) otherwise disp('Right-click to view context menu') end end end
运行程序文件,然后右键点击 UI 坐标区或 UI 图窗中的空白区域可打开上下文菜单。
openContextMenu
输入参数
cm
— 上下文菜单对象
ContextMenu
对象
使用 uicontextmenu
函数创建的上下文菜单对象。
x
— x 坐标
整数
x 坐标,即距 UI 图窗左边缘的距离,指定为一个整数(以像素为单位)。如果指定的值超过图窗的宽度,则上下文菜单将不可见。
y
— y 坐标
整数
y 坐标,即距图窗下边缘的距离,指定为一个整数(以像素为单位)。如果指定的值超过图窗的高度,则上下文菜单将不可见。
coord
— 像素坐标
由整数组成的行向量
像素坐标,指定为由整数值组成的二元素行向量。
示例: [100 150]
指定像素坐标 (100,150)
。
提示
通过按键或在上下文菜单外点击鼠标来关闭上下文菜单。您无法以编程方式关闭上下文菜单。
算法
当您调用 open
函数时,ContextMenuOpeningFcn
回调函数不执行。回调函数仅由用户交互触发。
版本历史记录
在 R2020a 中推出
另请参阅
MATLAB 命令
您点击的链接对应于以下 MATLAB 命令:
请在 MATLAB 命令行窗口中直接输入以执行命令。Web 浏览器不支持 MATLAB 命令。
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)