How to automatically resize custom button.CData image with maximize and restore down clicks on guide?
조회 수: 3 (최근 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!