Use events in VBS

zhaozj2021-02-11  196

Sender: roachcock (chen3feng), letter area: Programming Title: Using events in VBS: BBS Shuimu Tsinghua Station (THU Jun 13 18:55:43 2002) VBS is a powerful language, this from The flood of various macro viruses can be seen, and the VBS function is powerful is not the language itself, but it has the ability to call the automated COM component, and in the VBS, you can use the event VBS to provide a getRef function, the VBS manual saying is Returns a function pointer, the result of my debugging is to return a IDispatch interface pointer, which only one DISP member is the method of adding the idispatch * type in the COM component, and is required to receive the pointer. When calling, it is also the driver script of a WinformCom object: Dim frmmain set frmmain = createObject ("winform.form") frmmain.onload = getRef ("form_load") frmmain.onclose = getRef ("form_close" frmmain .OnMouseMove = GetRef ( "Form_MouseMove") frmMain.ShowModal Sub Form_Load () frmMain.Caption = "Hello, world!" MsgBox frmMain.Caption End Sub Sub Form_Close (ByRef Cancel) Cancel = true End Sub Sub Form_MouseMove (Button, x, y) frmmain.drawtext 0,0, "x =" & x & vbcrlf & "y =" & y End Sub Magic? Now VBS can be used like VB.

// Script of Java version

var frmMain; frmMain = WScript.CreateObject ( "WinForm.Form"); frmMain.OnLoad = Form_Load; frmMain.OnClose = Form_Close frmMain.OnMouseMove = Form_MouseMove frmMain.OnPaint = Form_Paint frmMain.ShowModal () function Form_Load () {frmMain.Caption = "Hello, World!";} Function form_close (ca Zan, x, y) {frmmain.drawtext (0, 0, "x =" x "y =" Y);} function form_paint (canvas) {canvas.moveto (0, 0); Canvas.Lineto (200, 200);}

Winform's partial code looks closely, I can use connection point, :) // Winform.idl: idl source for winform.dll /// this file will be proced by the mid o o / / p d t l 百 百 百货 商城. .tlb) and marshalling code import "oaidl.idl"; import "ocidl.idl"; [object, uuid (209FA034-4472-4F90-B220-23CA290598D9), dual, helpstring ( "IForm Interface"), pointer_default (unique )] Interface iForm: IDispatch {[Propget, ID (1)] HRESULT CAPTION ([OUT, RET VAL] BSTR * PVAL); [PropPut, ID (1), Helpstring ("Property CAPTION) ")] HRESULT CAPTION ([in] bstr newval); [Propget, ID (2), Helpstring (" Property Visable ")] HRESULT Visible ([OUT, RET VAL] BOOL * PVAL); [PropPut, ID (2) HELPSTRING ("Property Visable")] HRESULT Visible ([In] Bool NewVal); [Propget, ID (3), Helpstring ("Property Enabled")] HRESULT Enabled ([OUT, RET VAL] BOOL * PVAL); PropPut, ID (3), Helpstring ("Property Enabled")] HRESULT Enabled ([IN] Bool NewVal; [ID (4), Helpstring ("Method Create")] HRESULT CREATE (); [i D (5), Helpstring ("Method ShowModal")] HRESULT ShowModal ([OUT, RETVAL] LON G * PRESULT); [ID (6), Helpstring ("Method Show"] HRESULT SHOW (); [Propget, ID (7) HRESULT OnLOAD ([OUT, RETVA L] LPDISPATCH * PVAL); [PropPut, ID (7), Helpstring ("Property OnLoad")] HRESULT ONLOAD ([in] LPDISP ATCH Newval); [Propget, ID (8), HELPSTRING ("Property OnClick"] HRESULT OnClick ([in] long nbutton, [out, retval] lpdispatch * pval); [PropPut, ID (8), Helpstring ("Property OnClick ")] HRESULT OnClick ([in] long nbutton, [in] lpdispatch newval; [Propget, ID (9), Helpstring (" Property OnClose "

)] HRESULT OnCLOSE ([OUT, RET VAL] LPDISPATCH * PVAL); [PropPut, ID (9), Helpstring ("Property OnClose")] HRESULT OnClose ([In] LPDI Spatch NewVal); [Propget, ID (10) , HELPSTRING ("Property Onunload")] HRESULT ONNUNLOAD ([OUT, RETVAL] iDispatch * * pval); [PropPut, ID (10), Helpstring ("Property Onunload")] HRESULT ONNLOAD ([In] I Dispatch * NewVal) ; [propget, id (11), helpstring ( "property OnMouseMove")] HRESULT OnMouseMove ([out, retval] IDispatch * * pVal); [propput, id (11), helpstring ( "property OnMouseMove")] HRESULT OnMouseMove ( [Idispatch * newVal); [ID (12), Helpstring ("Method DrawText")] HRESULT DRAWTEXT ([in] int long x, [in] long y, [in] bstr bstrtext);}; [UUID 1C755BCA-5105-4D0B-A80E-8B37B22C8011), Version (1.0), Helpstring ("Winform 1.0 Type Library")] library winformlib {importlib ("stdole32.tlb"); importlib ("stdole2.tlb"); [UUID 6430A312-37A5-46EA-A7AD-4796CADFB77A), Helpstring ("form class")] coclass form {[default] interface iform;};}; // form.h: DEC laration of the CForm #ifndef __FORM_H_ #define __FORM_H_ #include "resource.h" // main symbols / // CForm class ATL_NO_VTABLE CForm: public CComObjectRootEx , public CComCoClass , public CDialogImpl , public IdispatchImpl {public: enum {IDD = IDD_MAINDIALOG};

CForm () {} DECLARE_REGISTRY_RESOURCEID (IDR_FORM) DECLARE_PROTECT_FINAL_CONSTRUCT () BEGIN_COM_MAP (CForm) COM_INTERFACE_ENTRY (IForm) COM_INTERFACE_ENTRY (IDispatch) END_COM_MAP () BEGIN_MSG_MAP (CForm) MESSAGE_HANDLER (WM_CLOSE, OnClose) MESSAGE_HANDLER (WM_CREATE, OnCreate) MESSAGE_HANDLER (WM_INITDIALOG, OnInitDialog) MESSAGE_HANDLER (WM_LBUTTONUP, OnLButtonUP) MESSAGE_HANDLER (WM_MBUTTONUP, OnMButtonUp) MESSAGE_HANDLER (WM_RBUTTONUP, OnRButtonUp) MESSAGE_HANDLER (WM_MOUSEMOVE, OnMouseMove) MESSAGE_HANDLER (WM_DESTROY, OnDestroy) END_MSG_MAP () // IForm public: STDMETHOD (Show) (); STDMETHOD (ShowModal) (/ * [out, retval] * / long * presult); stdmethod (); stdmethod (/ * [out, retval); stdmethod (PUT_ENABLED) (/ * [in] * / Bool newval); stdmethod (/ * [out, retval] * / bool * pVAL); stdmethod (/ * [in] * / bool newval); stdmethod (/ * [out, Retval] * / bstr * pVAL); stdmethod (/ * [in] * / bstr newval); public: stdmethod (DrawText (/ * [in] * / long y, / * [in] * / bstr bstrtext); stdmethod (get_onmousemove) (/ * [out, return) * / idispatch * * PVAL); stdmethod (put_onmousemove) (/ * [in] * / idispatch * newval); stdmethod (/ * [out, retval); stdMeth * * pVAL); stdmethod (PUT_ONUNLOAD) (/ * [in] * / Idispatch * newval); stdmethod (/ * [out, retval); stdmethod (put_onclose) (/ * [in] * / lpdispatch newval); stdmethod (Get_onclick) (/ * [ IN] * / long nbutton, / * [out, retval] * / lpdispatch * pval); stdmethod (/ * [in] * / long nbutton, / * [in] * / lpdispatch newval); stdmethod (Get_onLoad) (/ * [out, retval] * / lpdispatch * pval); stdmethod (/ * [in] * / lpdispatch newval);

LRESULT OnClose (UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL & bHandled) {// TODO: Add Code for message handler Call DefWindowProc if necessary _variant_t varCancel = false; m_OnClose.Invoke1 ((int) 0x0, & varCancel); if (.. ! (bool) varCancel) EndDialog (0); return 0;} LRESULT OnCreate (UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL & bHandled) {// TODO:.. Add Code for message handler Call DefWindowProc if necessary VARIANT varResult; VariantClear (& varResult); DISPPARAMS disp = {NULL, NULL, 0, 0}; m_OnLoad-> Invoke (0x1, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_METHOD, & dis p, & varResult, NULL, NULL); return 0;} LRESULT OnInitDialog (UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL & bHandle d) {// TODO:.. Add Code for message handler Call DefWindowProc if necessary if (m_OnLoad) m_OnLoad.Invoke0 ((int) 0x0); return 0;} LRESULT OnClick (UINT uButton, Uint nshift) {// Todo: add code for message handler. Call defWindowProc if Necessary. Return 0; LRESULT OnLButtonUP (UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL & bHandled) {// TODO:.. Add Code for message handler Call DefWindowProc if necessary OnClick (1,2); return 0;} LRESULT OnMButtonUp (UINT uMsg, WPARAM wParam , LPARAM lParam, BOOL & bHandled) {// TODO: Add Code for message handler Call DefWindowProc if necessary return 0;..} LRESULT OnRButtonUp (UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL & bHandled) {// TODO: Add Code for Message handler. Call DefWindowProc if Necessary. Return 0;} private: ccomdispatchdriver m_onclose; ccomdispatchDriver m_onload; ccomdispatchDriver m_onunload; ccomdispatchDriver m_onmousemove;

LRESULT OnMouseMove (UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL & bHandled) {// TODO:.. Add Code for message handler Call DefWindowProc if necessary if (m_OnMouseMove) {_variant_t varParams [3]; varParams [0] = (short) HiWord (LPARAM); Varparams [1] = (Short) Loword (LPARAM); Varparams [2] = (long) WParam; M_Onmousemove.Invokeen ((int) 0x0, varparams, 3);} returno 0;} LRESULT ONDESTROY UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL & bHandled) {// TODO:.. Add Code for message handler Call DefWindowProc if necessary if (m_OnUnload) m_OnUnload.Invoke0 ((int) 0x0); return 0;}}; #endif File: // __ form_h_ // form.cpp: importation of cform #include "stdafx.h" #include "winform.h" #include "form.h" / // cform stdmethodimp cform :: get_caption (bstr * pval) { // TODO: Add your implementation code here GetWindowText (pVal); return S_OK;} STDMETHODIMP CForm :: put_Caption (BSTR newVal) {// TODO: Add your implementation code here USES_CONVERSION; SetWindowText (W 2T (newVal)); return S_OK;} STDMETHODIMP CForm :: get_Visible (BOOL * pVal) {// TODO: Add your implementation code here * pVal = IsWindowVisible (); return S_OK;} STDMETHODIMP CForm :: put_Visible (BOOL newVal) {// TODO: Add your implementation code here ShowWindow (newVal SW_SHOW: SW_HIDE?); return S_OK;} STDMETHODIMP CForm :: get_Enabled (BOOL * pVal) {// TODO: Add your implementation code here return S_OK;} STDMETHODIMP CForm: : put_Enabled (BOOL newVal) {// TODO: Add your implementation code here return S_OK;} STDMETHODIMP CForm :: Create () {// TODO: Add your implementation code here CDialogImpl :: Create (NULL); return m_hWnd ? S_ok: e_unexpected;

} STDMETHODIMP CForm :: ShowModal (long * pResult) {// TODO: Add your implementation code here * pResult = DoModal (); return S_OK;} STDMETHODIMP CForm :: Show () {// TODO: Add your implementation code here ShowWindow (SW_SHOW); return S_OK;} STDMETHODIMP CForm :: get_OnLoad (LPDISPATCH * pVal) {// TODO: Add your implementation code here * pVal = m_OnLoad; return S_OK;} STDMETHODIMP CForm :: put_OnLoad (LPDISPATCH newVal) {// TODO : Add your implementation code here m_OnLoad = newVal; return S_OK;} STDMETHODIMP CForm :: get_OnClick (long nButton, LPDISPATCH * pVal) {// TODO: Add your implementation code here return S_OK;} STDMETHODIMP CForm :: put_OnClick (long nButton, LPDISPATCH newVal) {// TODO: Add your implementation code here return S_OK;} STDMETHODIMP CForm :: get_OnClose (LPDISPATCH * pVal) {// TODO: Add your implementation code here return S_OK;} STDMETHODIMP CForm :: put_OnClose (LPDISPATCH newVal) {// Todo: add your importation code here m_onclose = newval; return s_o K;} STDMETHODIMP CForm :: get_OnUnload (IDispatch ** pVal) {// TODO: Add your implementation code here * pVal = m_OnUnload; return S_OK;} STDMETHODIMP CForm :: put_OnUnload (IDispatch * newVal) {// TODO: Add your implementation code here m_OnUnload = newVal; return S_OK;} STDMETHODIMP CForm :: get_OnMouseMove (IDispatch ** pVal) {// TODO: Add your implementation code here * pVal = m_OnMouseMove; return S_OK;} STDMETHODIMP CForm :: put_OnMouseMove (IDispatch * newVal ) {// TODO: Add your implementation code here m_OnMouseMove = newVal; return S_OK;} STDMETHODIMP CForm :: DrawText (long x, long y, BSTR bstrText) {// TODO: Add your implementation code here HDC hDC = GetDC () ;

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

New Post(0)