poster
2019-12-10, 20:48
我有一个GUI,其中一些值显示在可编辑的文本框中 (http://www.mathworks.com/help/techdoc/creating_guis/f15-998661.html#f15-1002118) 。由于某些原因,我无法使用鼠标复制这些值。我可以选择文本,但是当我右键单击所选文本时,不会出现下拉菜单。我一直到处找。我想念什么?
回答:
的确,当您右键单击时,默认情况下,可编辑文本框不会弹出上下文菜单,但是如果要将文本复制到剪贴板,有几种解决方法:
正如Mikhail (https://stackoverflow.com/users/136967/mikhail)在评论中提到的那样,您仍然可以突出显示文本,然后按Ctrl + C将其复制到剪贴板。
正如Itamar在回答中提到的那样 (https://stackoverflow.com/questions/4427873/why-cant-i-copy-values-from-an-edit-field/4428753#4428753) ,您可以使用UICONTEXTMENU (http://www.mathworks.com/help/techdoc/ref/uicontextmenu.html)和UIMENU (http://www.mathworks.com/help/techdoc/ref/uimenu.html)函数为可编辑文本框创建自己的上下文菜单。这是使用功能CLIPBOARD (http://www.mathworks.com/help/techdoc/ref/clipboard.html)将可编辑文本字符串添加到剪贴板的示例实现:
hFigure = figure; %# Create a figure hEdit = uicontrol(hFigure,'Style','edit',... %# Create an editable text box 'String','Enter your name here',... 'Position',[30 50 130 20]); hCMenu = uicontextmenu; %# Create a context menu uimenu(hCMenu,'Label','Copy',... %# Create a menu item 'Callback',@(hObject,eventData) clipboard('copy',get(hEdit,'String'))); set(hEdit,'UIContextMenu',hCMenu); %# Add context menu to control 现在,您可以右键单击该控件以打开一个带有以下选项的菜单:“复制”。请注意,通过选择此菜单项,它将可编辑的文本字符串复制到剪贴板,而不必先突出显示文本。
您可以为可编辑的文本框设置'ButtonDownFcn'属性 (http://www.mathworks.com/help/techdoc/ref/uicontrol_props.html#bqxoije) ,以便在控件上单击鼠标右键将自动将文本字符串复制到剪贴板,而无需突出显示文本或选择菜单项。首先,您必须将此m文件功能保存到路径:
function right_click_copy(hObject,eventData) hFigure = get(hObject,'Parent'); %# Get the parent object while ~strcmp(get(hFigure,'Type'),'figure') %# Loop until it is a figure hFigure = get(hFigure,'Parent'); %# Keep getting the parents end if strcmp(get(hFigure,'SelectionType'),'alt') %# Check for a right click clipboard('copy',get(hObject,'String')); %# Copy the object string to %# the clipboard end end 此函数使用父图形的'SelectionType'属性 (http://www.mathworks.com/help/techdoc/ref/figure_props.html#SelectionType)来检查按下了哪个鼠标按钮,并使用CLIPBOARD (http://www.mathworks.com/help/techdoc/ref/clipboard.html)函数将对象字符串复制到剪贴板。现在,您可以按如下所示创建可编辑的文本控件:
hFigure = figure; %# Create a figure hEdit = uicontrol(hFigure,'Style','edit',... %# Create an editable text box 'String','Enter your name here',... 'Position',[30 50 130 20],... 'ButtonDownFcn',@right_click_copy); 这是三个方法中最快,最简单的选项,因为它只需要单击一次鼠标即可将可编辑的文本字符串复制到剪贴板。
更多&回答... (https://stackoverflow.com/questions/4427873)
回答:
的确,当您右键单击时,默认情况下,可编辑文本框不会弹出上下文菜单,但是如果要将文本复制到剪贴板,有几种解决方法:
正如Mikhail (https://stackoverflow.com/users/136967/mikhail)在评论中提到的那样,您仍然可以突出显示文本,然后按Ctrl + C将其复制到剪贴板。
正如Itamar在回答中提到的那样 (https://stackoverflow.com/questions/4427873/why-cant-i-copy-values-from-an-edit-field/4428753#4428753) ,您可以使用UICONTEXTMENU (http://www.mathworks.com/help/techdoc/ref/uicontextmenu.html)和UIMENU (http://www.mathworks.com/help/techdoc/ref/uimenu.html)函数为可编辑文本框创建自己的上下文菜单。这是使用功能CLIPBOARD (http://www.mathworks.com/help/techdoc/ref/clipboard.html)将可编辑文本字符串添加到剪贴板的示例实现:
hFigure = figure; %# Create a figure hEdit = uicontrol(hFigure,'Style','edit',... %# Create an editable text box 'String','Enter your name here',... 'Position',[30 50 130 20]); hCMenu = uicontextmenu; %# Create a context menu uimenu(hCMenu,'Label','Copy',... %# Create a menu item 'Callback',@(hObject,eventData) clipboard('copy',get(hEdit,'String'))); set(hEdit,'UIContextMenu',hCMenu); %# Add context menu to control 现在,您可以右键单击该控件以打开一个带有以下选项的菜单:“复制”。请注意,通过选择此菜单项,它将可编辑的文本字符串复制到剪贴板,而不必先突出显示文本。
您可以为可编辑的文本框设置'ButtonDownFcn'属性 (http://www.mathworks.com/help/techdoc/ref/uicontrol_props.html#bqxoije) ,以便在控件上单击鼠标右键将自动将文本字符串复制到剪贴板,而无需突出显示文本或选择菜单项。首先,您必须将此m文件功能保存到路径:
function right_click_copy(hObject,eventData) hFigure = get(hObject,'Parent'); %# Get the parent object while ~strcmp(get(hFigure,'Type'),'figure') %# Loop until it is a figure hFigure = get(hFigure,'Parent'); %# Keep getting the parents end if strcmp(get(hFigure,'SelectionType'),'alt') %# Check for a right click clipboard('copy',get(hObject,'String')); %# Copy the object string to %# the clipboard end end 此函数使用父图形的'SelectionType'属性 (http://www.mathworks.com/help/techdoc/ref/figure_props.html#SelectionType)来检查按下了哪个鼠标按钮,并使用CLIPBOARD (http://www.mathworks.com/help/techdoc/ref/clipboard.html)函数将对象字符串复制到剪贴板。现在,您可以按如下所示创建可编辑的文本控件:
hFigure = figure; %# Create a figure hEdit = uicontrol(hFigure,'Style','edit',... %# Create an editable text box 'String','Enter your name here',... 'Position',[30 50 130 20],... 'ButtonDownFcn',@right_click_copy); 这是三个方法中最快,最简单的选项,因为它只需要单击一次鼠标即可将可编辑的文本字符串复制到剪贴板。
更多&回答... (https://stackoverflow.com/questions/4427873)