当我使用drawpo​lygon交互绘制多​边形后,如何针对顶点​设置一个右键菜单?

6 visualizaciones (últimos 30 días)
华明
华明 el 29 de Mzo. de 2025
Editada: Harsh el 11 de Jun. de 2025
function createPolygon
fig = figure('Name','高精度地质建模工具', 'NumberTitle','off',...
'Position',[200 200 1200 700], 'MenuBar','none',...
'CloseRequestFcn',@closeApp);
% 绘图区域
ax = axes('Parent', fig, ...
'Position',[0.1, 0.1, 0.6, 0.8],...
'Box','on', 'XGrid','on', 'YGrid','on',...
'YDir','reverse' ... % 添加纵轴倒转
);
hPoly = drawpolygon(ax, 'Color', rand(1,3), 'LineWidth',1.5,...
'InteractionsAllowed','all',...
'Tag','Polygon');
function closeApp(~,~)
selection = questdlg('确认退出?', '关闭程序',...
'保存并退出', '直接退出', '取消', '保存并退出');
switch selection
case '保存并退出'
exportData();
delete(fig);
case '直接退出'
delete(fig);
otherwise
return
end
end
end
这是我的基础代码。
这三张图片显示了,绘制多边形后,分别是多边形的面、线和顶点处右键的菜单。我现在发现在面和线的右键菜单可以重新自定义,然而在顶点处的菜单无法找到自定义位置。
修改菜单的代码如下
function createPolygon
fig = figure('Name','高精度地质建模工具', 'NumberTitle','off',...
'Position',[200 200 1200 700], 'MenuBar','none',...
'CloseRequestFcn',@closeApp);
% 绘图区域
ax = axes('Parent', fig, ...
'Position',[0.1, 0.1, 0.6, 0.8],...
'Box','on', 'XGrid','on', 'YGrid','on',...
'YDir','reverse' ... % 添加纵轴倒转
);
hPoly = drawpolygon(ax, 'Color', rand(1,3), 'LineWidth',1.5,...
'InteractionsAllowed','all',...
'Tag','Polygon');
menuItems = uicontextmenu(fig);
uimenu(menuItems, 'Text', '重命名', ...
'MenuSelectedFcn',@(src,evt)renamePolygon(hPoly),...
'Separator','on');
uimenu(menuItems, 'Text', '多边形属性', ...
'MenuSelectedFcn',@(~,~)editPolygonProperties(hPoly), ...
'Separator','on');
uimenu(menuItems, 'Text','删除多边形', ...
'MenuSelectedFcn',@(src,evt)deletePolygon(hPoly), ...
'Separator','on');
uimenu(menuItems, 'Text','编辑顶点', ...
'MenuSelectedFcn',@(src,evt)deletePolygon(hPoly), ...
'Separator','on');
uimenu(menuItems, 'Text','删除顶点', ...
'MenuSelectedFcn',@(src,evt)deletePolygon(hPoly), ...
'Separator','on');
hPoly.ContextMenu = menuItems;
function closeApp(~,~)
selection = questdlg('确认退出?', '关闭程序',...
'保存并退出', '直接退出', '取消', '保存并退出');
switch selection
case '保存并退出'
exportData();
delete(fig);
case '直接退出'
delete(fig);
otherwise
return
end
end
end
结果如下:
在顶点的菜单没有被覆盖。
我现在希望找到方法可以覆盖原始的顶点菜单,进行自定义。

Respuestas (1)

Harsh
Harsh el 11 de Jun. de 2025
Editada: Harsh el 11 de Jun. de 2025
You are right in observing that assigning a custom uicontextmenu to a polygon object applies it to the polygon’s face and edges, but not to its vertices, where the default context menu remains active.
To achieve your goal of having a custom context menu at each vertex, you can overlay images.roi.Point objects on top of each polygon vertex. This allows you to assign individual context menus to those points, effectively customizing the interaction at each vertex.
The process is as follows:
  1. After a user draws the hPoly polygon, get the array of vertex locations from its Position property.
  2. Loop through these positions. At each coordinate, create and display an images.roi.Point object.
  3. Create your desired custom vertex menu using uicontextmenu.
  4. Assign this custom menu to the ContextMenu property of each images.roi.Point object you created.
This approach effectively places an invisible, clickable point with your custom menu on each vertex. To ensure that the points and the polygon vertices move together seamlessly when either is edited, you will also need to use event listeners (addlistener) to create a two-way link between the polygon's Position and the position of each point.
This method gives you full control over the right-click behavior at the vertices while preserving the underlying polygon's functionality.
For further details on the objects involved, please see the following documentation:
Using MATLAB co-pilot in MATLAB R2025a, I was able to write the attached script, feel free to use it for your reference.
I hope this helps, thanks!

Categorías

Más información sobre Migrate GUIDE Apps en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by