实例步骤:
须要设置UIContextMenu,否则点击右键不显示。
右键点击第一个菜单之后:(在菜单中加入对应的回调函数)
function r1_Callback(hObject, eventdata, handles)% hObject handle to r1 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)uicontrol(gcf,'style','text','String','右键菜单');结果:点击右键弹出菜单,点击第一个菜单项
使用底层代码加入右键菜单:
figure('MenuBar','none');%将原来的菜单取消掉h=uicontextmenu;%定义新的右键菜单uimenu(h,'label','右键菜单1');%定义子菜单uimenu(h,'label','右键菜单2');%定义子菜单set(gcf,'uicontextmenu',h);%将右键菜单显示出来