다음을 통해 공유


연습: 확장명이 편집기 바로 가기 키 사용

확장 편집기의 바로 가기 키에 응답할 수 있습니다. 다음 연습에서는 바로 가기 키를 사용 하 여 보기 장식 텍스트 보기에 추가 하는 방법을 보여 줍니다. 이 연습에서는 뷰포트 장식 편집기 서식 파일을 기반으로 하는 장식 사용 하 여 추가할 수 있습니다에 + 문자.

사전 요구 사항

이 연습을 완료 하려면 설치 해야 해당 Visual Studio 2010 SDK.

참고

Visual Studio SDK에 대 한 자세한 내용은 참조 하십시오. Visual Studio 개요를 확장합니다..Visual Studio SDK를 다운로드 하는 방법를 참조 하십시오. Visual Studio 확장성 개발자 센터 MSDN 웹 사이트에서.

관리 되는 확장성 프레임 워크 (MEF) 프로젝트 만들기

MEF 프로젝트를 만들려면

  1. 뷰포트 장식 편집기 프로젝트를 만듭니다. 솔루션의 이름을 KeyBindingTest.

  2. VSIX 매니페스트 편집기에서 source.extension.vsixmanifest 파일을 엽니다.

  3. Content MEF 구성 요소 콘텐츠 형식 및 해당 제목 포함은 Path Keybindingtest.dll으로 설정 됩니다.

  4. 저장 하 고 source.extension.vsixmanifest를 닫습니다.

  5. 다음 참조를 추가 하 고 설정 CopyLocal 에 false:

    Microsoft.VisualStudio.Editor

    Microsoft.VisualStudio.OLE.Interop

    Microsoft.VisualStudio.Shell

    Microsoft.VisualStudio.TextManager.Interop

  6. KeyBindingTestFactory 클래스 파일을 삭제 합니다.

KeyBindingTest 클래스 파일의 클래스 이름을 Purplecornerbox로 변경 합니다. 생성자를 변경 합니다. 생성자 내부에서는 줄 변경:

_adornmentLayer = view.GetAdornmentLayer("KeyBindingTest")
_adornmentLayer = view.GetAdornmentLayer("KeyBindingTest");

를 다음으로 변경:

_adornmentLayer = view.GetAdornmentLayer("PurpleCornerBox")
_adornmentLayer = view.GetAdornmentLayer("PurpleCornerBox");

명령 필터를 정의합니다.

명령 필터를 구현 하는 것 IOleCommandTarget, 장식을 인스턴스화하여 명령을 처리 합니다.

명령 필터를 정의 하려면

  1. 클래스 파일을 추가 하 고 이름을 KeyBindingCommandFilter.

  2. 다음 추가 문을 사용 합니다.

    Imports System
    Imports System.Runtime.InteropServices
    Imports Microsoft.VisualStudio.OLE.Interop
    Imports Microsoft.VisualStudio
    Imports Microsoft.VisualStudio.Text.Editor
    
    using System;
    using System.Runtime.InteropServices;
    using Microsoft.VisualStudio.OLE.Interop;
    using Microsoft.VisualStudio;
    using Microsoft.VisualStudio.Text.Editor;
    
  3. KeyBindingCommandFilter 라는 클래스에서 상속 해야 합니다 IOleCommandTarget.

    Friend Class KeyBindingCommandFilter
        Implements IOleCommandTarget
    
    internal class KeyBindingCommandFilter : IOleCommandTarget
    
  4. 텍스트 보기 전용 필드, 다음 명령을 명령 체인 및 명령 필터를 이미 추가 되어 있는지 여부를 나타내는 플래그를 추가 합니다.

    Private m_textView As IWpfTextView
    Friend m_nextTarget As IOleCommandTarget
    Friend m_added As Boolean 
    Friend m_adorned As Boolean
    
    private IWpfTextView m_textView;
    internal IOleCommandTarget m_nextTarget;
    internal bool m_added;
    internal bool m_adorned;
    
  5. 텍스트 뷰를 설정 하는 생성자를 추가 합니다.

    Public Sub New(ByVal textView As IWpfTextView)
        m_textView = textView
        m_adorned = False 
    End Sub
    
    public KeyBindingCommandFilter(IWpfTextView textView)
    {
        m_textView = textView;
        m_adorned = false;
    }
    
  6. 구현에서 QueryStatus() 방법으로 다음과 같습니다.

    Private Function QueryStatus(ByRef pguidCmdGroup As Guid, ByVal cCmds As UInteger, ByVal prgCmds() As OLECMD, ByVal pCmdText As IntPtr) As Integer Implements IOleCommandTarget.QueryStatus
        Return m_nextTarget.QueryStatus(pguidCmdGroup, cCmds, prgCmds, pCmdText)
    End Function
    
    int IOleCommandTarget.QueryStatus(ref Guid pguidCmdGroup, uint cCmds, OLECMD[] prgCmds, IntPtr pCmdText)
    {
        return m_nextTarget.QueryStatus(ref pguidCmdGroup, cCmds, prgCmds, pCmdText);
    }
    
  7. 구현은 Exec() 경우 보라색 상자 보기에이 추가 하도록 메서드는 + 문자 수를 입력 합니다.

    Private Function Exec(ByRef pguidCmdGroup As Guid, ByVal nCmdID As UInteger, ByVal nCmdexecopt As UInteger, ByVal pvaIn As IntPtr, ByVal pvaOut As IntPtr) As Integer Implements IOleCommandTarget.Exec
        If m_adorned = False Then 
            Dim typedChar As Char = Char.MinValue
    
            If pguidCmdGroup = VSConstants.VSStd2K AndAlso nCmdID = CUInt(VSConstants.VSStd2KCmdID.TYPECHAR) Then
                typedChar = CChar(ChrW(Marshal.GetObjectForNativeVariant(pvaIn)))
                If typedChar.Equals("+"c) Then 
                    Dim TempPurpleCornerBox As PurpleCornerBox = New PurpleCornerBox(m_textView)
                    m_adorned = True 
                End If 
            End If 
        End If 
        Return m_nextTarget.Exec(pguidCmdGroup, nCmdID, nCmdexecopt, pvaIn, pvaOut)
    End Function
    
    int IOleCommandTarget.Exec(ref Guid pguidCmdGroup, uint nCmdID, uint nCmdexecopt, IntPtr pvaIn, IntPtr pvaOut)
    {
        if (m_adorned == false)
        {
            char typedChar = char.MinValue;
    
            if (pguidCmdGroup == VSConstants.VSStd2K && nCmdID == (uint)VSConstants.VSStd2KCmdID.TYPECHAR)
            {
                typedChar = (char)(ushort)Marshal.GetObjectForNativeVariant(pvaIn);
                if (typedChar.Equals('+'))
                {
                    new PurpleCornerBox(m_textView);
                    m_adorned = true;
                }
            }
        }
        return m_nextTarget.Exec(ref pguidCmdGroup, nCmdID, nCmdexecopt, pvaIn, pvaOut);
    }
    

명령 필터 공급자 추가

장식 공급자 명령 필터 텍스트 보기에 추가 해야 합니다. 이 예제에서는 공급자를 구현 합니다. IVsTextViewCreationListener 텍스트 보기 만들기 이벤트를 수신 하도록 합니다. 이 장식 공급자도의 장식의 Z 순서 정의 장식 레이어를 내보냅니다.

명령 필터 공급자를 추가 하려면

  1. 클래스 파일을 추가 하 고 이름을 KeyBindingFilterProvider.

  2. 다음 추가 문을 사용 합니다.

    Imports System
    Imports System.Collections.Generic
    Imports System.ComponentModel.Composition
    Imports Microsoft.VisualStudio
    Imports Microsoft.VisualStudio.OLE.Interop
    Imports Microsoft.VisualStudio.Utilities
    Imports Microsoft.VisualStudio.Editor
    Imports Microsoft.VisualStudio.Text.Editor
    Imports Microsoft.VisualStudio.TextManager.Interop
    
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.Composition;
    using Microsoft.VisualStudio;
    using Microsoft.VisualStudio.OLE.Interop;
    using Microsoft.VisualStudio.Utilities;
    using Microsoft.VisualStudio.Editor;
    using Microsoft.VisualStudio.Text.Editor;
    using Microsoft.VisualStudio.TextManager.Interop;
    
  3. 라는 클래스를 추가 KeyBindingFilterProvider 에서 상속 되 IVsTextViewCreationListener, 내보낼 콘텐츠 형식을 "텍스트"와 a TextViewRoleAttributeEditable.

    <Export(GetType(IVsTextViewCreationListener)), ContentType("text"), TextViewRole(PredefinedTextViewRoles.Editable)>
    Friend Class KeyBindingCommandFilterProvider
        Implements IVsTextViewCreationListener
    
    [Export(typeof(IVsTextViewCreationListener))]
    [ContentType("text")]
    [TextViewRole(PredefinedTextViewRoles.Editable)]
    internal class KeyBindingCommandFilterProvider : IVsTextViewCreationListener
    
  4. 장식 계층 정의 추가 합니다.

    <Export(GetType(AdornmentLayerDefinition)), Name("PurpleCornerBox"), Order(), TextViewRole(PredefinedTextViewRoles.Editable)>
    Friend keybindingAdornmentLayer As AdornmentLayerDefinition
    
    [Export(typeof(AdornmentLayerDefinition))]
    [Name("PurpleCornerBox")]
    [Order()]
    [TextViewRole(PredefinedTextViewRoles.Editable)]
    internal AdornmentLayerDefinition keybindingAdornmentLayer;
    
  5. 텍스트 뷰 어댑터를 얻으려면 하면 가져와야 합니다는 IVsEditorAdaptersFactoryService.

    <Import(GetType(IVsEditorAdaptersFactoryService))>
    Friend editorFactory As IVsEditorAdaptersFactoryService = Nothing
    
    [Import(typeof(IVsEditorAdaptersFactoryService))]
    internal IVsEditorAdaptersFactoryService editorFactory = null;
    
  6. 구현에서 VsTextViewCreated 하 여 추가 하도록 메서드는 KeyBindingCommandFilter.

    Public Sub VsTextViewCreated(ByVal textViewAdapter As IVsTextView) Implements IVsTextViewCreationListener.VsTextViewCreated
        Dim textView As IWpfTextView = editorFactory.GetWpfTextView(textViewAdapter)
        If textView Is Nothing Then 
            Return 
        End If
    
        AddCommandFilter(textViewAdapter, New KeyBindingCommandFilter(textView))
    End Sub
    
    public void VsTextViewCreated(IVsTextView textViewAdapter)
    {
        IWpfTextView textView = editorFactory.GetWpfTextView(textViewAdapter);
        if (textView == null)
            return;
    
        AddCommandFilter(textViewAdapter, new KeyBindingCommandFilter(textView));
    }
    
  7. AddCommandFilter 처리기에 텍스트 뷰 어댑터를 가져옵니다 및 명령 필터를 추가 합니다.

    Private Sub AddCommandFilter(ByVal viewAdapter As IVsTextView, ByVal commandFilter As KeyBindingCommandFilter)
        If commandFilter.m_added = False Then 
            'get the view adapter from the editor factory 
            Dim [next] As IOleCommandTarget
            Dim hr As Integer = viewAdapter.AddCommandFilter(commandFilter, [next])
    
            If hr = VSConstants.S_OK Then
                commandFilter.m_added = True 
                'you'll need the next target for Exec and QueryStatus 
                If [next] IsNot Nothing Then
                    commandFilter.m_nextTarget = [next]
                End If 
            End If 
        End If 
    End Sub
    
    void AddCommandFilter(IVsTextView viewAdapter, KeyBindingCommandFilter commandFilter)
    {
        if (commandFilter.m_added == false)
        {
            //get the view adapter from the editor factory
            IOleCommandTarget next;
            int hr = viewAdapter.AddCommandFilter(commandFilter, out next);
    
            if (hr == VSConstants.S_OK)
            {
                commandFilter.m_added = true;
                //you'll need the next target for Exec and QueryStatus 
                if (next != null)
                    commandFilter.m_nextTarget = next;
            }
        }
    }
    

빌드 및 코드 테스트

이 코드를 테스트 하려면 KeyBindingTest 솔루션을 빌드하고 실험 인스턴스를 실행 합니다.

빌드 및 KeyBindingTest 솔루션을 테스트 하려면

  1. 솔루션을 빌드합니다.

  2. 이 프로젝트를 디버거에서 실행할 때 Visual Studio 두 번째 인스턴스를 인스턴스화합니다.

  3. 만들기 또는 텍스트 파일을 열고 텍스트 보기에서 아무 곳 이나 클릭 합니다.

  4. 형식 +.

    텍스트 보기의 크기를 조정 합니다.

    그 테두리가 자주색 사각형 텍스트 보기의 오른쪽 위 모서리에 표시 됩니다.

참고 항목

작업

연습: 콘텐츠 형식의 파일 이름 확장명에 연결