GUI: function to change pushbutton color on mouseover

21 visualizzazioni (ultimi 30 giorni)
Magda
Magda il 21 Ago 2016
Risposto: David Barry il 25 Ago 2016
Hi, I'm trying to create a simple GUI. I want to make the uicontrol pushbuttons to change a specific color when mouse over. I've been trying to find the answer on the forum but with no success. Please help me as without this I can't move on with my work (it's one of the requirements of the project). Here is the code I have so far:
function fig = mainmenu()
col = [0.0 0.2 0.4];
h0 = figure(...
'Color',[0.95 0.95 0.95], ...
'PaperPosition',[18 180 576 432], ...
'PaperUnits','points', ...
'Units','normalized',...
'Position',[0.72 0.55 0.21 0.4], ...
'NumberTitle','off',...
'Name','Software X', ...
'MenuBar','none', ...
'Resize','off',...
'ToolBar','none');
hp = uipanel(...
'Title','Analysis',...
'TitlePosition', 'centertop',...
'Position',[.05 .05 .9 .9],...
'FontUnits','normalized',...
'ForegroundColor',[0.0 0.2 0.4],...
'FontSize',0.065);
h1 = uicontrol('Parent',hp, ...
'Units','norm', ...
'Position',[0.15 0.65 0.7 0.11], ...
'FontUnits','normalized',...
'Fontsize',0.4,...
'BackgroundColor',col, ...
'ForegroundColor',[1 1 1],...
'Style', 'pushbutton',...
'String','Link 1', ...
'CallBack','link to another menu;');
h2 = uicontrol('Parent',hp, ...
'Units','norm', ...
'Position',[0.15 0.5 0.7 0.11], ...
'FontUnits','normalized',...
'Fontsize',0.4,...
'BackgroundColor',col, ...
'ForegroundColor',[1 1 1],...
'String','Link 2', ...
'Style','pushbutton',...
'CallBack','link to another menu;');
h3 = uicontrol('Parent',hp, ...
'Units','norm', ...
'Position',[0.15 0.35 0.7 0.11], ...
'FontUnits','normalized',...
'Fontsize',0.4,....
'BackgroundColor',col, ...
'ForegroundColor',[1 1 1],...
'String','Close',...
'CallBack','delete(gcf)');
if nargout > 0, fig = h0; end

Risposte (1)

David Barry
David Barry il 25 Ago 2016
You should take a look at the WindowButtonMotionFcn callback property on the Figure. You could use this to define your own custom callback function which gets the current mouse position, checks if it coincides with the position of your button and responds accordingly.

Categorie

Scopri di più su Interactive Control and Callbacks in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by