以文本方式查看主题 - 中文XML论坛 - 专业的XML技术讨论区 (http://bbs.xml.org.cn/index.asp) -- 『 C/C++编程思想 』 (http://bbs.xml.org.cn/list.asp?boardid=61) ---- 在VBS中使用事件 (http://bbs.xml.org.cn/dispbbs.asp?boardid=61&rootid=&id=64726) |
-- 作者:卷积内核 -- 发布时间:7/18/2008 11:07:00 AM -- 在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一样使用了 var frmMain;
|
-- 作者:卷积内核 -- 发布时间:7/18/2008 11:08:00 AM -- 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{}; }; |
W 3 C h i n a ( since 2003 ) 旗 下 站 点 苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》 |
8,580.078ms |