2014-07-17 1 views
7

도구 모음에 도구 모음과 몇 가지 명령이 포함 된 VSPackage가 있다고 가정 해 보겠습니다. 툴바의 명령 중 하나를 프로그래밍 방식으로 표시하거나 숨길 수 있습니까? 사용자는 도구 모음을 사용자 정의하여이를 수행 할 수 있습니다. 따라서 코드에서이 작업을 수행 할 수있는 방법이 있어야한다는 강한 인상을 가지고 있습니다.도구 모음에 동적으로 표시되는 Visual Studio VSPackage 명령

AddIn을 개발하지 않으므로 DTE.Commands.AddNamedCommand(AddInInstance, Name, ButtonText, ToolTip, MSOButton)을 사용할 수 없습니다.

우리는 여전히 비주얼 스튜디오 명령 표 (.vsct) 형식을 사용하여 우리의 명령을 정의 할 수 있으며, 그있는 VSPackages에 대한 제안 된 방법이기 때문에 좋아,

: 나중에 C# 코드에

<?xml version="1.0" encoding="utf-8"?> 
<CommandTable xmlns="http://schemas.microsoft.com/VisualStudio/2005-10-18/CommandTable" xmlns:xs="http://www.w3.org/2001/XMLSchema"> 

    <Extern href="vsshlids.h" /> 

    <Commands package="testPackage"> 

    <Menus> <!-- Define the menus, toolbars, etc. --> 
     <Menu guid="commands" id="toolbar" type="Toolbar"> 
     <Parent guid="guidSHLMainMenu" id="IDG_VS_BUILD_SOLUTION" /> 
     <CommandFlag>DefaultDocked</CommandFlag> 
     <Strings> 
      <ButtonText>TestToolbar</ButtonText> 
     </Strings> 
     </Menu> 
    </Menus> 

    <Groups> <!-- Define the groups for commands --> 
     <Group guid="commands" id="toolbarGroup" priority="0x0001"> 
     <Parent guid="commands" id="toolbar" /> 
     </Group> 
    </Groups> 

    <Buttons> <!-- Define the commands as buttons --> 
     <Button guid="commands" id="button0" type="Button"> 
     <Parent guid="commands" id="toolbarGroup" /> 
     <CommandFlag>DynamicVisibility</CommandFlag> 
     <Strings> 
      <ButtonText>TestButton0</ButtonText> 
     </Strings> 
     </Button> 
     <Button guid="commands" id="button1" type="Button"> 
     <Parent guid="commands" id="toolbarGroup" /> 
     <CommandFlag>DynamicVisibility</CommandFlag> 
     <Strings> 
      <ButtonText>TestButton1</ButtonText> 
     </Strings> 
     </Button> 
    </Buttons> 

    </Commands> 

    <Symbols> 
    <GuidSymbol name="testPackage" value="{FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF}" /> 

    <GuidSymbol name="commands" value="{EEEEEEEE-EEEE-EEEE-EEEE-EEEEEEEEEEEE}"> 
     <IDSymbol name="toolbar" value="0x0100" /> 
     <IDSymbol name="toolbarGroup" value="0x0010" /> 
     <IDSymbol name="button0" value="0x0000" /> 
     <IDSymbol name="button1" value="0x0001" /> 
    </GuidSymbol> 
    </Symbols> 

</CommandTable> 

당신은 우리가 위에서 정의한 MenuCommand의 다른 속성을 설정할 수 있습니다 : 메뉴 명령을 도구 모음에 배치되어있는 경우, 다음 버튼을 숨길 것 Visible 속성이 false 만드는 것으로,

System.ComponentModel.Design.MenuCommand menuCommand = <Acquire your menu command>; 
menuCommand.Enabled = <enabled>; 
menuCommand.Visible = <visible>; 
menuCommand.Supported = <supported>; 

문제는, J ust가 회색으로 표시됩니다. (다른 메뉴에서는 숨겨져 있습니다.) 이것은 Visual Studio의 기능으로 버그가 아닙니다.

그러나 내가 필요한 것은 정확하게 다음과 같습니다. 숨기기 Button0. (Button0은 몇 가지 특수 조건이 적용될 때만 표시됩니다 (예 : X MB보다 하드 드라이브 공간이 적거나 다른 도구가 설치되어 있거나 여기에 자신의 상태를 구성하는 경우)

EnvDTE.Command button0 = DTE.Commands.Item(commandsGuid, button0CommandId); // Both are the same as in the .vsct file 
if (button0 != null) 
    button0.Delete(); 

Command0가 발견하지만, 삭제할 때, 나는 예외있어 : 지정되지 않은 오류를 (

하나가 필요하지 않은 경우, 버튼을 삭제하려면 추가 기능 시간에서 다음과 같은 방법을 사용할 수 있습니다 HRESULT 예외 : 0x80004005 (E_FAIL)) 결국, 다소 이해가됩니다. 프로그래밍 방식이 아닌 .vsct 메커니즘을 통해 만들어졌습니다.

아이디어가 부족합니다. 런타임에 프로그래밍 방식으로 도구 모음 단추를 숨기거나 표시하거나 추가/제거하는 방법을 알려주십시오. VSPackage 명령을 정의하는 다른 방법이 있지만 .vsct 파일이 있습니까?

도움을 주시면 감사하겠습니다.

당신이 vsct 파일에 버튼을 DynamicVisibility 플래그를 설정하는 모두의
+0

같은 문제가 발생했습니다. 또한 vsct 파일의 섹션을 사용하려고했지만 성공하지는 못했습니다. 이것으로 어떤 진전을 보였습니까? – Przemaas

+0

@Przemaas 우리는 비활성화 된 (회색으로 표시) 아이콘으로 붙어 있습니다.우선 순위가 더 높은 작업이 있었으므로 사라지게 만드는 방법을 결코 알지 못했습니다. – Shakaron

+0

https://stackoverflow.com/questions/14572226/hiding-a-button-from-a-plugin-toolbar/ –

답변

7

첫째 :

<Button guid="commands" id="button0" priority="0x1001" type="Button"> 
    <Parent guid="commands" id="toolbarGroup" /> 
    <CommandFlag>DefaultInvisible</CommandFlag> 
    <CommandFlag>DynamicVisibility</CommandFlag> 
    <Strings> 
     <ButtonText>TestButton1</ButtonText> 
    </Strings> 
    </Button> 

다음, 오버라이드 (override) Package.Initialize 대신 MenuCommand의 OleMenuCommand 클래스를 사용하여 명령 처리기를 작성하고이 같은 BeforeQueryStatus 이벤트에 가입 :

OleMenuCommandService mcs = GetService(typeof(IMenuCommandService)) as OleMenuCommandService; 
if (null != mcs) 
{ 
    // Create the command for the menu item. 
    CommandID menuCommandID = new CommandID(GuidList.guidToolbarCmdSet, (int)PkgCmdIDList.cmdidButton0); 
    var menuItem = new OleMenuCommand(MenuItemCallback, menuCommandID); 
    menuItem.BeforeQueryStatus += BeforeQueryStatusCallback; 
    mcs.AddCommand(menuItem); 
} 

지금 BeforeQueryStatusCallback에 표시하거나 버튼을 당신에게

private void BeforeQueryStatusCallback(object sender, EventArgs e) 
{ 
    var cmd = (OleMenuCommand)sender 
    cmd.Visible = true; 
} 
를 숨길 수 있습니다
+0

버튼이 ** 툴바에없는 경우 ** 사실입니다. 하지만 제 경우에는 숨겨진 것이 아니라 원래 게시물에서 지적한 것처럼 회색으로 표시됩니다. 그렇습니다. 항목에 DynamicVisiblility 플래그를 포함해야합니다. 나는 그것을 고쳤다. – Shakaron

+0

Typo - 줄 끝 부분에 세미콜론이 없습니다. var cmd = (OleMenuCommand) sender –