在VBS中使用事件

类别:VC语言 点击:0 评论:0 推荐:

发信人: 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{};
};

本文地址:http://com.8s8s.com/it/it2271.htm