Creating Graphical User Interfaces |
The Address Book Resize Function
The address book defines its own resize function. To use this resize function, you must set the Application Options dialog Resize behavior to User-specified
, which in turn sets the figure's ResizeFcn
property to:
Whenever the user resizes the figure, MATLAB calls the ResizeFcn
subfunction in the address book M-file (address_book.m
)
Behavior of the Resize Function
The resize
function allows users to make the figure wider, to accommodate long names and numbers, but does not allow the figure to be made narrower than its original width. Also, users cannot change the height. These restrictions do not limit the usefulness of the GUI and simplify the resize
function, which must maintain the proper proportions between the figure size and the components in the GUI.
When the user resizes the figure and releases the mouse, the resize function executes. At that point, the resized figure's dimensions are saved. The following sections describe how the resize
function handles the various possibilities.
Changing the Width
If the new width is greater than the original width, set the figure to the new width.
The size of the Contact Name text box changes in proportion to the new figure width. This is accomplished by:
Units
of the text box to normalized
.
Units
to characters
.
If the new width is less than the original width, use the original width.
Changing the Height
If the user attempts to change the height, use the original height. However, because the resize function is triggered when the user releases the mouse button after changing the size, the resize function cannot always determine the original position of the GUI on screen. Therefore, the resize function applies a compensation to the vertical position (second element in the figure Position
vector) as follows:
When the figure is resized from the bottom, it stays in the same position. When resized from the top, the figure moves to the location where the mouse button is released.
Ensuring the Resized Figure Is On Screen
The resize
function calls movegui
to ensure that the resized figure is on screen regardless of where the user releases the mouse.
When the GUI is first run, it is displayed at the size and location specified by the figure Position
property. You can set this property with the Property Inspector when you create the GUI.
Code Listing
function ResizeFcn(hObject, eventdata, handles)% Get the figure size and position
Figure_Size = get(hObject, 'Position');% Set the figure's original size in character units
Original_Size = [ 0 0 94 19.230769230769234];% If the resized figure is smaller than the
% original figure size then compensate
if (Figure_Size(3)<Original_Size(3)) | (Figure_Size(4) ~= Original_Size(4)) if Figure_Size(3) < Original_Size(3)% If the width is too small then reset to origianl width
set(hObject, 'Position',... [Figure_Size(1) Figure_Size(2) Original_Size(3) Original_Size(4)]) Figure_Size = get(hObject, 'Position'); end if Figure_Size(4) ~= Original_Size(4)% Do not allow the height to change
set(hObject, 'Position',... [Figure_Size(1), Figure_Size(2)+Figure_Size(4)-Original_Size(4),... Figure_Size(3), Original_Size(4)]) end end% Adjust the size of the Contact Name text box
% Set the units of the Contact Name field to 'Normalized'
set(handles.Contact_Name,'units','normalized')% Get its Position
C_N_pos = get(handles.Contact_Name,'Position');% Reset it so that it's width remains normalized relative to figure
set(handles.Contact_Name,'Position',... [C_N_pos(1) C_N_pos(2) 0.789 C_N_pos(4)])% Return the units to 'Characters'
set(handles.Contact_Name,'units','characters')% Reposition GUI on screen
movegui(hObject, 'onscreen')
Saving Changes to the Address Book from the Menu |