How to automatically resize custom button.CData image with maximize and restore down clicks on guide?
4 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Geoff Hayes
2019-8-13
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Migrate GUIDE Apps 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!