分享
 
 
 

在VBS中使用事件

王朝vc·作者佚名  2006-01-08
窄屏简体版  字體: |||超大  

发信人: RoachCock (chen3feng), 信区: Programming

标 题: 在VBS中使用事件

发信站: BBS 水木清华站 (Thu Jun 13 18:55:43 2002)

VBS是一种功能强大的语言,这个从各种宏病毒的泛滥可以看得出,VBS功能强大不是语言

本身,而是它具有调用自动化COM组件的能力,而且,在VBS中还可以使用事件

VBS提供了一个GetRef函数,VBS手册上说是返回一个函数指针,我调试的结果,是返回一个

IDispatch接口指针,该指针只有一个DISP成员,就是DISPID为 0的方法

在COM组件中添加IDispatch*类型的属性,就可以接收该指针,并在需要的时候调用,也就是

激发事件

例子

一个WinFormCOM对象的驱动脚本:

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

神奇吧?现在VBS可以象VB一样使用了

//脚本的Java版

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(Cancel)

{

Cancel = true;

}

function Form_MouseMove(Button,x,y)

{

frmMain.DrawText(0,0,"x= " + x + "y=" + y );

}

function Form_Paint(Canvas)

{

Canvas.MoveTo(0, 0);

Canvas.LineTo(200, 200);

}

WinForm的部分代码

仔细看,我可没用连接点,:)

// WinForm.idl : IDL source for WinForm.dll

//

// This file will be processed by the MIDL tool to

// produce the type library (WinForm.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), helpstring("property Caption")] 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();

[id(5), helpstring("method ShowModal")] HRESULT ShowModal([out,retval] lon

g*pResult);

[id(6), helpstring("method Show")] HRESULT Show();

[propget, id(7), helpstring("property OnLoad")] 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 OnUnload([out,

retval] IDispatch* *pVal);

[propput, id(10), helpstring("property OnUnload")] HRESULT OnUnload([in] I

Dispatch* newVal);

[propget, id(11), helpstring("property OnMouseMove")] HRESULT OnMouseMove(

[out, retval] IDispatch* *pVal);

[propput, id(11), helpstring("property OnMouseMove")] HRESULT OnMouseMove(

[in] IDispatch* newVal);

[id(12), helpstring("method DrawText")] HRESULT DrawText([in]int long x,[i

n]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 : Declaration of the CForm

#ifndef __FORM_H_

#define __FORM_H_

#include "resource.h" // main symbols

////////////////////////////////////////////////////////////////////////////

/

// CForm

class ATL_NO_VTABLE CForm :

public CComObjectRootEx<CComSingleThreadModel>,

public CComCoClass<CForm, &CLSID_Form>,

public CDialogImpl<CForm>,

public IDispatchImpl<IForm, &IID_IForm, &LIBID_WINFORMLib>

{

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(Create)();

STDMETHOD(get_Enabled)(/*[out, retval]*/ BOOL *pVal);

STDMETHOD(put_Enabled)(/*[in]*/ BOOL newVal);

STDMETHOD(get_Visible)(/*[out, retval]*/ BOOL *pVal);

STDMETHOD(put_Visible)(/*[in]*/ BOOL newVal);

STDMETHOD(get_Caption)(/*[out, retval]*/ BSTR *pVal);

STDMETHOD(put_Caption)(/*[in]*/ BSTR newVal);

public :

STDMETHOD(DrawText)(/*[in]*/ long x,/*[in]*/ long y,/*[in]*/BSTR bstrText);

STDMETHOD(get_OnMouseMove)(/*[out, retval]*/ IDispatch* *pVal);

STDMETHOD(put_OnMouseMove)(/*[in]*/ IDispatch* newVal);

STDMETHOD(get_OnUnload)(/*[out, retval]*/ IDispatch* *pVal);

STDMETHOD(put_OnUnload)(/*[in]*/ IDispatch* newVal);

STDMETHOD(get_OnClose)(/*[out, retval]*/ LPDISPATCH *pVal);

STDMETHOD(put_OnClose)(/*[in]*/ LPDISPATCH newVal);

STDMETHOD(get_OnClick)(/*[in]*/long nButton, /*[out, retval]*/ LPDISPATCH *

pVal);

STDMETHOD(put_OnClick)(/*[in]*/long nButton, /*[in]*/ LPDISPATCH newVal);

STDMETHOD(get_OnLoad)(/*[out, retval]*/ LPDISPATCH *pVal);

STDMETHOD(put_OnLoad)(/*[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.InvokeN((int)0x0, varParams, 3);

}

return 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 : Implementation 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(W2T(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<CForm>::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 implementation code here

m_OnClose = newVal;

return S_OK;

}

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();

TextOutW(hDC, x,y, bstrText, SysStringLen(bstrText));

ReleaseDC(hDC);

return S_OK;

}

其它的东西都是Wizard生成的,不贴了

对了,还有一个对话框模板,你还可以提供从XML载入Form的能力

--

无知者无畏!

namespace cfc

{

class dummy{};

};

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有