How to join macro in your own program

zhaozj2021-02-16  53

How to join macro in your own program

Implementation: To add the function of the macro statement in the program, like Microsoft Word, you can run macros in it, and use macros to implement some special features.

Use technology: Automotion

Program implementation: 1. New dialog program, join the edit box, select its multiline property.

2. Associate the variable to the edit box: CEDIT M_EDIT1;

3. Add new category MyScriptObject, base class is ccmdtarget, and select the automotion below.

4. Add the method to the new class: Open ClassWizard, select the Automotion tab, select MyScriptObject in Class Name, click Add Methods, enter the add to add, in the EXTERNAL NAME, select Long, the parameter list is added as follows: NA Na long.

5. Change the Add method to: Return Na NB in ​​the MyScriptObject class;

6. Open MYScriptObject.h and change the designer function to public.

7, intercept according to the bn_clicked message of IDOK, add the following code and remove cdialog :: onok ();:

g_iactivescriptsite.m_punkscriptobject =

M_MyscriptObject.getInterface (& IID_IUNKNOWN);

HRVERIFY (COCREATEINSTANCE (CLSID_VBScript, Null, Clsctx_inproc_server,

IID_IACTIScript, (void **) & m_iactivescript,

"CoCreateInstance () for clsid_vbscript");

HrVerify (m_iactivescript-> queryinterface (IID_IACTIVEScriptParse,

(void **) & m_iactivescriptpival,

"Queryinterface () for iid_iactivescriptpival");

Hrverify (m_iactivescript-> setscriptsite (& g_iactivescriptsite),

"IACTIVEScript :: setscriptsite ()");

HrVerify (m_iactivescriptpivalse-> initnew (),

"IactivescriptParse :: initnew ()");

HrVerify (m_iactivescript-> addnameditem (l "myObject",

ScriptItem_isvisible | scriptItem_issource,

"IACTIScript :: addnamedItem ()");

CSTRING CSScriptText;

m_edit1.GetwindowText (CSScriptText);

EXCEPINFO EI;

BSTR PPARSETEXT = CSScriptText.Allocsystring ();

M_IACTIVESCRIPTPARS-> ParsescriptText (PParstext, L "MyObject", NULL,

NULL, 0, 0,

0L

, NULL, & EI);

m_iactivescript-> setscriptState (scriptstate_connected); m_iactivescriptpival-> release ();

M_IACTIVEScript-> release ();

8. PUBLIC in CMYDIALOG.H is added as follows:

CMYScriptObject M_MyscriptObject;

IACTIVEScript * m_iactivescript;

IactivescriptParse * m_iactivescriptpival;

9, add header files in CMYDIALOG.H:

#include

#include "myscriptobject.h"

10. Add the following code before Onok ():

Class MyActivescriptsite: Public IACTIVESCRIPTSITE {

Private:

Ulong m_dwref; // Reference Count

PUBLIC:

IUNKNOWN * M_PUNKScriptObject; // Pointer to Your Object That IS Exposed

// to the script engine in getteminfo ().

Myactivescriptsite :: myactivescriptsite () {m_dwref = 1;

Myactivescriptsite :: ~ myactivescriptsite () {}

Virtual HRESULT _STDCALL QUERYINTERFACE (Refiid Riid, Void ** PPVObject) {

* pPVObject = NULL;

RETURN E_NOTIMPL;

}

Virtual ulong _stdcall addref (void) {

Return m_dwref;

}

Virtual ulong _stdcall release (void) {

IF (- m_dwref == 0) Return 0;

Return m_dwref;

}

Virtual HRESULT _STDCALL GETLCID (LCID * PLCID) {

Return S_OK;

}

Virtual HRESULT _STDCALL GETITEMINFO (LPCOLESTR PSTRNAME,

DWORD DWRETURNMASK, IUNKNOWN ** PPUNKITEM, ITYPEINFO ** PPTI) {

IF (ppti) {

* PPTI = NULL;

IF (DWRETURNMASK & Scriptinfo_itypeInfo)

RETURN TYPE_E_E_EMENTNOTFOUND;

}

IF (PPUnkitem) {

* PPunkItem = NULL;

IF (dwreturnmask & scriptinfo_iunknown) {

IF (! _wcsicmp (l "myObject", pstrname) {

* PPunkItem = m_PunkscriptObject;

M_PunkscriptObject-> addRef ();

}

}

}

Return S_OK;

}

Virtual HRESULT __STDCALL GETDOCVERSIONSTRING (BSTR * PBSTRVERSION) {

Return S_OK;

}

Virtual HRESULT __STDCALL OONScriptterMinate (Const Variant * PvarResult,

Const excepinfo * pexcepinfo) {

Return S_OK;

Virtual HRESULT __STDCALL OnStateChange (ScriptState SsscriptState) {

Return S_OK;

}

Virtual HRESULT __STDCALL OnScripTERROR (

IactivescripterRor * pscripterror) {

Static BSTR PWCERRORTEXT;

PscripterRor-> getSourceLinetext (& PWCERRORTEXT);

AfxMessageBox

CString ("Iactivescriptsite :: OnScripTerror () / N")

CSTRING ("Line:")

CSTRING (PWCerrorText),

MB_SETFOREGROUND);

:: sysfreestring (pWcerrorText);

Return S_OK;

}

Virtual HRESULT __STDCALL Onenterscript (void) {

Return S_OK;

}

Virtual HRESULT __STDCALL ONLEVEScript (void) {

Return S_OK;

}

}

Myactivescriptsite g_iactivescriptsite;

#include

Define_guid (CLSID_VBScript, 0xB

54F

3741, 0x5b07, 0x11cf, 0xa4, 0xb0, 0x0,

0xAA, 0x0, 0X

4A

, 0x55, 0xE8);

Define_guid (CLSID_JScript, 0xF

414C

260, 0X

6ac

0, 0x11cf, 0xb6, 0xd1, 0x00,

0xAA, 0x00, 0xBb, 0xBb, 0x58);

Class oleinitclass {

PUBLIC:

Oleinitclass () {

Oleinitialize (NULL);

}

~ Oleinitclass () {

OleunInitialize ();

}

}

Oleinitclass g_oleinitclass;

Void HRVERIFY (HRESULT HR, CHAR * MSG)

{

IF (Failed (HR)) {

CString Str;

Str.Format ("Error: 0x% 08LX (% s)", HR, MSG);

AfxMessageBox (STR, 0x10000);

_exit (0);

}

}

11, run. Find a broken macro code to write Edit and you can do it. Ah, I haven't forgotten a little, remember to initialize the COM interface! J

转载请注明原文地址:https://www.9cbs.com/read-24299.html

New Post(0)