65.9K
CodeProject 正在变化。 阅读更多。
Home

JesCopyRightWriter - ITextDocument, ITextSelection 示例

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.67/5 (4投票s)

2009年3月11日

CPOL

1分钟阅读

viewsIcon

21672

downloadIcon

260

一个简单的 DevStudio 插件,用于演示文本对象模型 (TOM)

引言

JesCopyRightWriter 是一个简单的 DevStudio 插件,用于演示 ITextDocumentITextSelection 文本对象模型 (TOM) 对象。此插件在 VisualStudio 6.0 (SP5) 中打开的文件中插入一个简单的版权信息文件头。

背景

当我决定向 CodeProject 提交一些文章时,我想到如果有一个我自己的插件,可以插入带有版权信息的文件的文件头会很好。当我搜索互联网上的帮助时,我发现有很多用 Visual Basic、C# 等编写的文章,但用 VC++ 的文章却不多。但是,我从 TOM 及其接口中获得了一些想法(也感谢 Derek Lakin 的文章 WinDiff Visual Studio Add-in)。经过不断的尝试,我终于写出了一些可用的代码。

使用工具/代码

要配置插件,请按照以下步骤操作

  1. 下载并将插件保存到本地驱动器。
  2. 打开 Visual Studio。
  3. 选择“工具”->“自定义菜单”。
  4. 选择“加载项和宏文件”选项卡。
  5. 浏览并选择插件 DLL,然后单击“关闭”按钮。
  6. 单击新出现的工具栏按钮。这将向当前打开的文件添加文件头。

下面显示了检索当前打开的文档名称并将新行添加到该文件的代码片段

STDMETHODIMP CCommands::JesCopyRightWriterCommandMethod()
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());

	// Jes
    VERIFY_OK( m_pApplication->EnableModeless( VARIANT_FALSE ));
    // Retrieve active source file
    CComPtr<IDispatch> pIActiveDocDisp;
    m_pApplication->get_ActiveDocument( &pIActiveDocDisp );
    CComQIPtr<ITextDocument, &IID_ITextDocument> pActiveSrc( pIActiveDocDisp );
    if( pActiveSrc )
    {
        CComPtr<IDispatch> pITextSelDisp;
        if( SUCCEEDED( pActiveSrc->get_Selection( &pITextSelDisp )))
        {
            // Retrieve date information
            SYSTEMTIME stSysTime;
            GetSystemTime( &stSysTime );

            // Declare Copy-right information lines
            const int NUM_COPY_RIGHT_INFO_LINES = 7;
            CString csCopyRightInfoLines[ NUM_COPY_RIGHT_INFO_LINES ];
            // Line 1
            csCopyRightInfoLines[ 0 ] = _T( "/*\n" );
            // Line 2
            csCopyRightInfoLines[ 1 ].Format
		( _T( " * Copy-Right(c) %d Jellow T. K., Refer CPOL Clauses.\n" ),
                 	stSysTime.wYear );
            // Line 3
            csCopyRightInfoLines[ 2 ] = _T( " *\n" );
            // Line 4
            // Retrieve file name
            CComBSTR bstrSrcFile;
            pActiveSrc->get_Name( &bstrSrcFile );
            CString csSrcFileName( bstrSrcFile );
            csCopyRightInfoLines[ 3 ].Format( _T( " * %s - TODO : 
					Add file description here.\n" ),
                                              csSrcFileName );
            // Line 5
            csCopyRightInfoLines[ 4 ] = _T( " *\n" );
            // Line 6
            csCopyRightInfoLines[ 5 ].Format( _T( " * @version:    1.0            
				Date:  %2d-%2d-%4d\n" ),
                                   	stSysTime.wDay, stSysTime.wMonth, stSysTime.wYear );
            // Line 7
            csCopyRightInfoLines[ 6 ] = _T( " */\n" );

            // Update file with Copy-Right information
            CComQIPtr<ITextSelection, &IID_ITextSelection> pITextSel( pITextSelDisp );
            pITextSel->StartOfDocument( CComVariant( 0 ));
            pITextSel->NewLine( CComVariant( 0 ));
            pITextSel->NewLine( CComVariant( 0 ));
            pITextSel->StartOfDocument( CComVariant( 0 ));

            for( int nIdx = 0; nIdx < NUM_COPY_RIGHT_INFO_LINES; ++nIdx )
            {
                CComBSTR bstrCopyRightLineInfo = csCopyRightInfoLines[ nIdx ];
                pITextSel->put_Text( bstrCopyRightLineInfo );
            }
        }
    }
    else
    {
        AfxMessageBox( _T( "Could not find open source file..." ));
    }

    VERIFY_OK( m_pApplication->EnableModeless( VARIANT_TRUE ));

    // Jes End
	return S_OK;
}

关注点

当我检查 MSDN 中 ITextDocumentITextSelection 的帮助时,我发现需要 tom.h 进行编译。但是当我包含此头文件时,我得到了多个或重定义编译错误。这些接口定义已经存在于 VC98 标准头文件中的 \ObjModel\TextAuto.h 中。但我无法在此头文件中 (\ObjModel\TextAuto.h) 找到一些记录的方法。我不知道确切的原因。

历史

  • 版本 1.0 - 2009 年 3 月 11 日
© . All rights reserved.