How to automatically resize custom button.CData image with maximize and restore down clicks on guide?
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Gopichandh Danala
el 13 de Ag. de 2019
Editada: Gopichandh Danala
el 14 de Ag. de 2019
Hi,
I am trying to use cData feature to change the look of each button to some image.
I was able to acheive this but when I try to use maximize and restore down clicks on guide, the image size is not being updated to fit the normalized size of the button.
Can we make the button.CData resize the image when we do change the size of GUI using maximize or downclick or drag features on the guide?
My code:
I put the image fit to button size in OpeningFcn like this:
% --- Executes just before tempGUI is made visible.
function tempGUI_OpeningFcn(hObject, eventdata, handles, varargin)
handles = guidata(hObject);
[button_img,~]=imread('peppers.png');
handles.openPeppers.Units = 'pixels';
button_img =imresize(button_img,fliplr(handles.openPeppers.Position(1,3:4)));
handles.openPeppers.CData = button_img;
handles.openPeppers.Units ='normalized';
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
Then, if I change the size of guide manually , I need to dump the same code into button again to fit into button size.
% --- Executes on button press in openPeppers.
function openPeppers_Callback(hObject, eventdata, handles)
% hObject handle to openPeppers (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
handles = guidata(hObject);
button_img = handles.openPeppers.CData;
handles.openPeppers.Units = 'pixels';
button_img =imresize(button_img,fliplr(handles.openPeppers.Position(1,3:4)));
handles.openPeppers.CData = button_img;
handles.openPeppers.Units ='normalized';
% Update handles structure
guidata(hObject, handles);
Sample Images:
After OpeningFcn looks fine:
When I resize the guide manually, it doesn't fit and need to press button:
Can I do this without button click option?
Thanks,
Gopi
0 comentarios
Respuesta aceptada
Geoff Hayes
el 13 de Ag. de 2019
Gopichandh - try using the ResizeFcn callback. You can add this callback from the GUIDE editor by right-clicking on the figure and selecting View Callbacks --> ResizeFcn. This should create a function similar to
% --- Executes when figure1 is resized.
function figure1_ResizeFcn(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)
in your m file. You can then add the code to update the button image. I suggest that you create a helper function (that updates the button) which can be called from both your OpeningFcn and ResizeFcn so that you avoid code duplication.
1 comentario
Más respuestas (0)
Ver también
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!