matlab无法右键,GUI右键菜单不能在鼠标右键的位置显示

罗老师,你好。我按照您书中第六章例子6.3.3编写了一个可以读取图片,读取图片后对图片做放大和移动的gui。我看到您使用get得到鼠标位置后就可以设置GUI右键菜单的位置,从而实现点击鼠标右键就可以在鼠标位置显示设计好的右键菜单。但是,我按照您的写了程序,右键菜单就是不能在鼠标右键的位置显示,这是为什么呢?是因为有其他的句柄等等需要设置吗?

以下是我的代码:

function varargout = test1(varargin)

% TEST1 MATLAB code for test1.fig

%      TEST1, by itself, creates a new TEST1 or raises the existing

%      singleton*.

%

%      H = TEST1 returns the handle to a new TEST1 or the handle to

%      the existing singleton*.

%

%      TEST1('CALLBACK',hObject,eventData,handles,...) calls the local

%      function named CALLBACK in TEST1.M with the given input arguments.

%

%      TEST1('Property','Value',...) creates a new TEST1 or raises the

%      existing singleton*.  Starting from the left, property value pairs are

%      applied to the GUI before test1_OpeningFcn gets called.  An

%      unrecognized property name or invalid value makes property application

%      stop.  All inputs are passed to test1_OpeningFcn via varargin.

%

%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one

%      instance to run (singleton)".

%

% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help test1

% Last Modified by GUIDE v2.5 04-Aug-2016 19:33:11

% Begin initialization code - DO NOT EDIT

gui_Singleton = 1;

gui_State = struct('gui_Name',       mfilename, ...

'gui_Singleton',  gui_Singleton, ...

'gui_OpeningFcn', @test1_OpeningFcn, ...

'gui_OutputFcn',  @test1_OutputFcn, ...

'gui_LayoutFcn',  [] , ...

'gui_Callback',   []);

if nargin && ischar(varargin{1})

gui_State.gui_Callback = str2func(varargin{1});

end

if nargout

[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});

else

gui_mainfcn(gui_State, varargin{:});

end

% End initialization code - DO NOT EDIT

% --- Executes just before test1 is made visible.

function test1_OpeningFcn(hObject, eventdata, handles, varargin)

% This function has no output args, see OutputFcn.

% hObject    handle to figure

% eventdata  reserved - to be defined in a future version of MATLAB

% handles    structure with handles and user data (see GUIDATA)

% varargin   command line arguments to test1 (see VARARGIN)

% Choose default command line output for test1

handles.output = hObject;

% Update handles structure

guidata(hObject, handles);

% UIWAIT makes test1 wait for user response (see UIRESUME)

% uiwait(handles.figure1);

% --- Outputs from this function are returned to the command line.

function varargout = test1_OutputFcn(hObject, eventdata, handles)

% varargout  cell array for returning output args (see VARARGOUT);

% hObject    handle to figure

% eventdata  reserved - to be defined in a future version of MATLAB

% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure

varargout{1} = handles.output;

function pushbutton1_Callback(hObject, eventdata, handles)

% hObject    handle to pushbutton1 (see GCBO)

% eventdata  reserved - to be defined in a future version of MATLAB

% handles    structure with handles and user data (see GUIDATA)

[filename, pathname]=...

uigetfile({'*.png';'*.jpg';'*.bmp';'*.gif'},'open file');

str=[pathname filename];

a1=imread(str);

axes(handles.axes1);

imshow(a1);

function figure1_WindowButtonMotionFcn(hObject, eventdata, handles)

% hObject    handle to figure1 (see GCBO)

% eventdata  reserved - to be defined in a future version of MATLAB

% handles    structure with handles and user data (see GUIDATA)

pos=get(handles.axes1,'currentpoint');

xLim=get(handles.axes1,'xlim');

yLim=get(handles.axes1,'ylim');

if(pos(1,1)>=xLim(1)&&pos(1,1)<=xLim(2)&&pos(1,2)>=yLim(1)&&pos(1,2)<=yLim(2))

set(gcf,'Pointer','cross');

else

set(gcf,'Pointer','arrow');

end

% --- Executes on mouse press over figure background, over a disabled or

% --- inactive control, or over an axes background.

function figure1_WindowButtonDownFcn(hObject, eventdata, handles)

if strcmp(get(gcf,'Pointer'),'cross')

if strcmp(get(gcf,'SelectionType'), 'alt')

pos = get(gcf, 'currentpoint');

set(handles.change, 'position',[pos(1,1) pos(1,2)], 'visible', 'on')

end

end

%右键菜单显示的代码就是这里了

% --------------------------------------------------------------------

function Untitled_6_Callback(hObject, eventdata, handles)

% hObject    handle to Untitled_6 (see GCBO)

% eventdata  reserved - to be defined in a future version of MATLAB

% handles    structure with handles and user data (see GUIDATA)

toolsmenufcn ZoomIn

% --------------------------------------------------------------------

function Untitled_7_Callback(hObject, eventdata, handles)

% hObject    handle to Untitled_7 (see GCBO)

% eventdata  reserved - to be defined in a future version of MATLAB

% handles    structure with handles and user data (see GUIDATA)

pan on;

% --------------------------------------------------------------------

function change_Callback(hObject, eventdata, handles)

% hObject    handle to change (see GCBO)

% eventdata  reserved - to be defined in a future version of MATLAB

% handles    structure with handles and user data (see GUIDATA)

期待您的回答,谢谢。

ab1c7ee18da4dfd6bd0e53ac6dfdfdba.gif

2016-8-4 20:49 上传

点击文件名下载附件

6.92 KB, 下载次数: 4

GUI的文件

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值