Microsoft Word Syntax Highlight Addin (v1.2)

Source code can be downloaded. [updated:May 17, 2006]

Chinese Version


1. Introduction

    Syntax highlight addin is a COM addin for Microsoft Word. Through a COM addin, we can add a menu item or toolbar button onto Word, implementing special functions which  are needed by my ourselves.

    The addin on this page, can change the color or background color of selected text. How to change is not fixed. Anyone can define a new rule of changing color, by implementing the functions which are declared in the interface header file. This addin provided an open method to operate the text in Word. If you are interested in parsing text, this addin can help you attend to parsing itself.

    This software consists of a main module (sntxaddn.dll) and a set of highlight rules.


2. Usage of the Addin

2.1 Use "Setup Program" for Popular Use

    You can download a setup program. Highlight rule of 'HTML' are built in this setup program:

[demo_nt.msi] - 324kb (for Windows NT/2K/XP)

[demo_9x.msi] - 324kb (for Windows 9x)


2.2 Download from 'download.com'

Get it from CNET Download.com!


3. Comments on Interface Header File

3.1 Introduction of interface header file

    The filename of the header file: "sntxmod.h". There are 6 functions declared. A new button can be added after these 6 functions are implemented:

// Caption
const char * GetCaption();

// Tooltip ( Optional )
const char * GetTooltip();

// Button Face ( Optinal )
HBITMAP GetBtnFace();

// Parse
void Parse(wchar_t * text, int len);

// Nextpos
bool GetNextPos(int & pos, int & len);

// Color
bool GetColor(int & color);

// Bgcolor ( Optional )
bool GetBgColor(int & bgcolor);

Explanation:

    GetCaption() will be called at first time Word is opened after addin's installation. The return string will be used as caption text on the new button.

    GetTooltip() will be called at first time Word is opened after addin's installation. The return string will be used as tool tip text on the new button. This text will be shown as tool tip when mouse hovers on the button.

    GetBtnFace() will be called at first time Word is opened after addin's installation. It should return a handle of 16x16 bitmap. The HBITMAP returned is used to be the face of the button. In the bitmap, fuchsia color ( 0xFF00FF ) will stands for transparent color.

    Parse() will be called each time when the corresponding button is clicked. The 'text' passed in is the selected text, the 'len' passed in is the length of the selected text. Also, 'text' ends with '\0' too.

    After Parse() is called, GetNextPos() will called many times until it returns false. If GetNextPos() returns true, it means that some part of text should be highlighted. At the same time, it should tell Word the pos and length of the part of text through referenced arguments 'pos' and 'len'. Please pay attention to that: the 'pos' relative to the beginning of the text which is passed to Parser(), not relative to the whole article in the Word. After all highlight operations completed, GetNextPos() must return false.

    GetColor() will be called each time after GetNextPos() returns true. It should tell Word whether the color of the part of text should be changed or not, through return vale. If true, the color should be passed to Word through ref argument 'color'.

    GetBgColor() will be called each time after GetNextPos() returns true. It should tell Word whether the background color of the text should be changed or not, through return value. If true, the color should be passed to Word through ref argument 'bgcolor'.


3.2 Develop Demo

Step 1: Create 'Win32 Dynamic-Link Library' Project Type:

Step 2: Select 'A simple DLL project':

Step 3: Implement the 6 functions in 'sntxmod.h':

Step 4: Copy the compiled dll file to installed directory, reopen Word, select some text, then click:

3.3 Demo Project Download

    The interface header file of highlight rule is 'sntxmod.h', and there are 6 functions declared in it. To implement these functions in a dll project, a new highlight rule can be created. Copy the compile result, one dll file, into the directory of 'sntxaddn.dll', a new button can be seen after Word is reopened.

    "Main module (sntxaddn.dll)", "Demo Rule", and "Interface (sntxmod.h)" download:

[demo_nt.zip] - 33kb (sntxaddn.dll is UNICODE version)

[demo_9x.zip] - 33kb (sntxaddn.dll is ANSI version)


4. Source Code of the Main Module

4.1 Explanation

    For intercommunion, the source code of the main module is open. The source code can be consultable for those friends who are interested in Office development.


4.2 Freeware Copyright Announcement

This software is free to use/redistribute. The origin must be declared if redistributed:
http://www.regexlab.com/highlight/

The source code is open and free to use. It is allowed to develop any other freeware or shareware basing on this source code.


4.3 Download

Source code download address:

[Syntax3.src.zip] - 30kb


4.4 Discussion on 'codeproject.com'

A Word add-in to syntax highlight selected text


5. Relative Sponsored Links

 

 

 

RegExLab.com © 2005 - All Rights Reserved