2016-04-14 4 views
1

私が持っているもの:Matlabの-GUIでたUicontextmenuが見えるかアクティブであるかどうかを確認する方法

私はプロット(=軸)に接続されたUicontextmenuを持っています。私がマウスクリック(右ボタン)でこれを「アクティブ化」すると、通常の「コールバック」を使ってプロットを強調表示するなどの操作を行うことができます。ユーザーがメニューのuimenu要素の1つを選択すると、このuimenu要素のCallbackを使用して強調表示をリセットすることができます。 しかし、ユーザーが要素を選択しなければ問題があります。コンテキストメニューが表示されなくなり、これが発生した場合、私は見つけ出す方法を見つけることができません。私の例では、強調表示されたプロットは強調表示されたままです。私がこれまで試したどのような

ドキュメントを読んだだけでなく、私はuimenuの要素、例えばの一部にプロパティにリスナーを追加:

addlistener(mymenu_element, 'Visible', 'PostSet', @mytest); 

しかし、このプロパティだけでなく他の人のように、いつでも変更または触れることがないようだ - 私は少しサプライズ何:

oをそこで質問です:

uicontextmenuが実行された後に関数を実行する方法はありますか(コンテキストメニューが表示されなくなったときに呼び出す方法)?つまり、ユーザーがコンテキストメニューの要素を選択しなかった場合、これをどのように識別できますか?

答えて

1

あなたが別の方法であなたたUicontextmenuを作成し、管理することによって、この問題を回避することができます(私はいくつかのテストを実行し、同じ結論に来ている)、これらの項目に耳を傾けるカント以来:

function yourFunction 
    % create a figure 
    hFig = figure; 
    % add a listener to the mouse being pressed 
    addlistener (hFig, 'WindowMousePress', @(a,b)mouseDown(hFig)); 
end 
function mouseDown(hFig) 
    % react depening on the mouse selection type: 
    switch hFig.SelectionType 
    case 'alt' % right click 
     % create a uicontext menu and store in figure data 
     hFig.UserData.uic = uicontextmenu ('parent', hFig); 
     % create the menu items for the uicontextmenu 
     uimenu ('parent', hFig.UserData.uic, 'Label', 'do this', 'Callback', @(a,b)DoThis(hFig)) 
     % assign to the figure 
     hFig.UIContextMenu = hFig.UserData.uic; 
     % turn visible on and set position 
     hFig.UserData.uic.Visible = 'on'; 
     hFig.UserData.uic.Position = hFig.CurrentPoint; 
     % uicontext menu will appear as desired 
     % the next mouse action is then to either select an item or 
     % we will capture it below 
    otherwise 
     % if the uic is stored in the userdata we need to run the clean up 
     % code since the user has not clicked on one of the items 
     if isfield (hFig.UserData, 'uic') 
     DoThis(hFig); 
     end 
    end 
end 
function DoThis(hFig) 
    % Your code 
    disp ('your code'); 
    % Clean up 
    CleanUp(hFig); 
end 
function CleanUp(hFig) 
    % delete the uicontextmenu and remove the reference to it 
    delete(hFig.UserData.uic) 
    hFig.UserData = rmfield (hFig.UserData, 'uic'); 
end 
+0

確かにこの非常に素晴らしく巧妙な回避策です!どうもありがとう!しかし、私のGUIは本当に複雑でパフォーマンスも重要なので、私はいつも全員にリスナーを置いてはいけません。関連するコールバックがトリガーされた時点でリスナーを軸に置き、図のリスナーを置くと、これはうまくいくと思います。クリーンアップ内で、図のリスナーを削除することができます。 –

関連する問題