新书推介:《语义网技术体系》
作者:瞿裕忠,胡伟,程龚
   XML论坛     W3CHINA.ORG讨论区     计算机科学论坛     SOAChina论坛     Blog     开放翻译计划     新浪微博  
 
  • 首页
  • 登录
  • 注册
  • 软件下载
  • 资料下载
  • 核心成员
  • 帮助
  •   Add to Google

    >> 本版讨论高级C/C++编程、代码重构(Refactoring)、极限编程(XP)、泛型编程等话题
    [返回] 中文XML论坛 - 专业的XML技术讨论区计算机技术与应用『 C/C++编程思想 』 → 在VBS中使用事件 查看新帖用户列表

      发表一个新主题  发表一个新投票  回复主题  (订阅本版) 您是本帖的第 3504 个阅读者浏览上一篇主题  刷新本主题   树形显示贴子 浏览下一篇主题
     * 贴子主题: 在VBS中使用事件 举报  打印  推荐  IE收藏夹 
       本主题类别:     
     卷积内核 帅哥哟,离线,有人找我吗?
      
      
      威望:8
      头衔:总统
      等级:博士二年级(版主)
      文章:3942
      积分:27590
      门派:XML.ORG.CN
      注册:2004/7/21

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给卷积内核发送一个短消息 把卷积内核加入好友 查看卷积内核的个人资料 搜索卷积内核在『 C/C++编程思想 』的所有贴子 访问卷积内核的主页 引用回复这个贴子 回复这个贴子 查看卷积内核的博客楼主
    发贴心情 在VBS中使用事件

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


       收藏   分享  
    顶(0)
      




    ----------------------------------------------
    事业是国家的,荣誉是单位的,成绩是领导的,工资是老婆的,财产是孩子的,错误是自己的。

    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2008/7/18 11:07:00
     
     卷积内核 帅哥哟,离线,有人找我吗?
      
      
      威望:8
      头衔:总统
      等级:博士二年级(版主)
      文章:3942
      积分:27590
      门派:XML.ORG.CN
      注册:2004/7/21

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给卷积内核发送一个短消息 把卷积内核加入好友 查看卷积内核的个人资料 搜索卷积内核在『 C/C++编程思想 』的所有贴子 访问卷积内核的主页 引用回复这个贴子 回复这个贴子 查看卷积内核的博客2
    发贴心情 
    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{};
    };

    ----------------------------------------------
    事业是国家的,荣誉是单位的,成绩是领导的,工资是老婆的,财产是孩子的,错误是自己的。

    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2008/7/18 11:08:00
     
     GoogleAdSense
      
      
      等级:大一新生
      文章:1
      积分:50
      门派:无门无派
      院校:未填写
      注册:2007-01-01
    给Google AdSense发送一个短消息 把Google AdSense加入好友 查看Google AdSense的个人资料 搜索Google AdSense在『 C/C++编程思想 』的所有贴子 访问Google AdSense的主页 引用回复这个贴子 回复这个贴子 查看Google AdSense的博客广告
    2024/11/28 15:54:08

    本主题贴数2,分页: [1]

    管理选项修改tag | 锁定 | 解锁 | 提升 | 删除 | 移动 | 固顶 | 总固顶 | 奖励 | 惩罚 | 发布公告
    W3C Contributing Supporter! W 3 C h i n a ( since 2003 ) 旗 下 站 点
    苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
    78.125ms