Linking multiple GUI's when moving the Main GUI
    5 views (last 30 days)
  
       Show older comments
    
    Sebastian Ciuban
 on 14 Mar 2015
  
    
    
    
    
    Commented: Sebastian Ciuban
 on 15 Mar 2015
            Lately, I've asked a lot of questions regarding GUI's in matlab. And on this way I want to thank the people who answered them. I googled this problem but I didn't found any clear solution.
So the issue is: I have 1 Main Gui (the vertical one with 3 menus) and 3 "sub gui's". If I want to move the Main GUI the other 3 should "follow" the Main one on the screen being in the exact same position having as reference the Main GUI. What code I should approach in order to fulfill this?

0 Comments
Accepted Answer
  Geoff Hayes
      
      
 on 15 Mar 2015
        Ciuban - you can try adding a listener (see learn to use events and listeners) to capture the movement of the main GUI so that when its position changes, the other three GUIs position changes as well. For example, suppose that we have two GUIs, the main one and a smaller one that is situated to the right of the main one. The main GUI (named/tagged as Gui1) launches the second GUI (named/tagged Gui2). Within the OpeningFcn of the first we do
 function Gui1_OpeningFcn(hObject, eventdata, handles, varargin)
 % Choose default command line output for Gui1
 handles.output = hObject;
 % place Gui1 in centre of screen
 movegui(hObject,'center');
 % launch Gui2 and move to right of Gui1
 handles.hGui2 = Gui2('Visible','off');
 gui1Pos = get(hObject,'Position');
 gui2Pos = get(handles.hGui2,'Position');
 gui2Pos = [gui1Pos(1)+gui1Pos(3)+5 gui1Pos(2) gui2Pos(3) gui2Pos(4)];
 set(handles.hGui2,'Position',gui2Pos,'Visible','on');
 % save the current GUI positions
 handles.gui1Pos = gui1Pos;
 handles.gui2Pos = gui2Pos;
 % add a listener to respond to position changes of Gui1
 handles.gui1PosListener = ...
    addlistener(hObject, 'Position', 'PostSet', @(src,event)onGui1PosChange(src,event,hObject));
 % Update handles structure
 guidata(hObject, handles);
In the above, we centre the main GUI and then move the other GUI to the right of the first one. The listener is created to respond to changes in position of the main GUI. We define the callback for this listener as
 function onGui1PosChange(~,~,hGui)
 % get the handles for Gui1
 handles = guidata(hGui);
 % determine the change in position for Gui1
 newGui1Pos        = get(hGui,'Position');
 gui1PosDelta      = handles.gui1Pos - newGui1Pos;
 gui1PosDelta(3:4) = 0; % don't want any change to width and height
 % determine the new position for Gui2
 newGui2Pos = handles.gui2Pos - gui1PosDelta;
 set(handles.hGui2,'Position',newGui2Pos);
 % save the new positions
 handles.gui1Pos = newGui1Pos;
 handles.gui2Pos = newGui2Pos;
 guidata(hGui,handles);
Here, we determine the change in position of the first GUI (with respect to its x and y position) and then apply that change to the second GUI.
The attached code uses the above to keep the two GUIs positioned together. From the Command Line, just type
 Gui1
to test this out. Note that the second GUI only moves after the first GUIs position has been finalized (i.e. after you release the mouse button). Try it out and see what happens!
More Answers (0)
See Also
Categories
				Find more on Interactive Control and Callbacks in Help Center and File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
