EditPoint 使用方式範例
在下列範例中,會建立 TextDocument 物件、在物件中加入一些文字,然後示範 EditPoint 物件的部分功能。
範例
描述
這些函式是以一起始的字元位移 (Offset),而 IVsTextView 和類似的介面則是以零起始的。 由於 EditPoint 是為了支援 VB Automation 而設計的,所以是以一為起始。
若要執行此範例,請建立 Visual Studio 增益集專案,並且在精靈中指定要建立 [工具] 功能表項目。 當您建置和執行增益集時,按一下 [工具] 功能表上相關的命令,就可以執行程式碼。
程式碼
[Visual Basic]
Public Sub Exec(ByVal commandName As String, ByVal executeOption As vsCommandExecOption, ByRef varIn As Object, ByRef varOut As Object, ByRef handled As Boolean) Implements IDTCommandTarget.Exec
handled = False
If executeOption = vsCommandExecOption.vsCommandExecOptionDoDefault Then
If commandName = "EditPoint.Connect.EditPoint" Then
EditPointExample1(CType(_applicationObject, DTE))
handled = True
Exit Sub
End If
End If
End Sub
Sub EditPointExample1(ByVal dte As DTE)
Dim objTextDoc As TextDocument
Dim objEditPt As EditPoint, iCtr As Integer
' Create a new text file.
applicationObject.ItemOperations.NewFile("General\Text File")
' Get a handle to the new document and create an EditPoint.
objTextDoc = applicationObject.ActiveDocument.Object("TextDocument")
objEditPt = objTextDoc.StartPoint.CreateEditPoint
' Insert ten lines of text.
For iCtr = 1 To 10
objEditPt.Insert("This is a test." & Chr(13))
Next iCtr
' Replace the eighth word on the third line with a new word.
objEditPt.StartOfDocument()
objEditPt.LineDown(3)
objEditPt.WordRight(3)
objEditPt.Delete(4)
objEditPt.Insert("raid")
' Indent a line.
objEditPt.LineDown(2)
objEditPt.StartOfLine()
objEditPt.Indent()
' Set some bookmarks.
objEditPt.LineDown(1)
' Set the first bookmark.
objEditPt.SetBookmark()
' Set the second bookmark on the next line.
objEditPt.LineDown(1)
objEditPt.SetBookmark()
' Go back to the first bookmark.
objEditPt.PreviousBookmark()
' Add some text there.
objEditPt.Insert("BOOKMARK 1: ")
' Do the same thing at the next bookmark.
objEditPt.NextBookmark()
objEditPt.Insert("BOOKMARK 2: ")
' Print the current line and character offset in the buffer.
MsgBox("Line and offset: " & objEditPt.Line, _
objEditPt.LineCharOffset())
' Capitalize the third word of the second line.
objEditPt.StartOfDocument()
objEditPt.LineDown(2)
objEditPt.WordRight(3)
objEditPt.ChangeCase(4, vsCaseOptions.vsCaseOptionsCapitalize)
' Remove the spaces between the words.
objEditPt.DeleteWhitespace(vsWhitespaceOptions. _
vsWhitespaceOptionsHorizontal)
End Sub
Using System.Windows.Forms;
public void EditPointExample1( DTE dte )
{
TextDocument objTextDoc = null;
EditPoint objEditPt = null; int iCtr = 0;
// Create a new text file.
applicationObject.ItemOperations.NewFile( @"General\Text File",
"", "{00000000-0000-0000-0000-000000000000}" );
// Get a handle to the new document and create an EditPoint.
objTextDoc = ( ( EnvDTE.TextDocument )(
applicationObject.ActiveDocument.Object( "TextDocument" ) ) );
objEditPt = objTextDoc.StartPoint.CreateEditPoint();
// Insert ten lines of text.
for ( iCtr=1; iCtr<=10; iCtr++ )
{
objEditPt.Insert( "This is a test." + "\n");
}
// Replace the eighth word on the third line with a new word.
objEditPt.StartOfDocument();
objEditPt.LineDown( 3 );
objEditPt.WordRight( 3 );
objEditPt.Delete( 4 );
objEditPt.Insert( "raid" );
// Indent a line.
objEditPt.LineDown( 2 );
objEditPt.StartOfLine();
objEditPt.Indent( null, 1 );
// Set some bookmarks.
objEditPt.LineDown( 1 );
// Set the first bookmark.
objEditPt.SetBookmark();
// Set the second bookmark on the next line.
objEditPt.LineDown( 1 );
objEditPt.SetBookmark();
// Go back to the first bookmark.
objEditPt.PreviousBookmark();
// Add some text there.
objEditPt.Insert( "BOOKMARK 1: " );
// Do the same thing at the next bookmark.
objEditPt.NextBookmark();
objEditPt.Insert( "BOOKMARK 2: " );
// Print the current line and character offset in the buffer.
MessageBox.Show( "Line and offset: " + objEditPt.Line);
// Capitalize the third word of the second line.
objEditPt.StartOfDocument();
objEditPt.LineDown( 2 );
objEditPt.WordRight( 3 );
objEditPt.ChangeCase( 4, vsCaseOptions.vsCaseOptionsCapitalize );
// Remove the spaces between the words.
objEditPt.DeleteWhitespace( vsWhitespaceOptions.vsWhitespaceOptionsHorizontal );
}