How to pass the events and values from ocx to html or JSP
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 3 123 LastLast
Results 1 to 15 of 31

Thread: How to pass the events and values from ocx to html or JSP

  1. #1
    Join Date
    Mar 2005
    Posts
    25

    Lightbulb How to pass the events and values from ocx to html or JSP

    Hi,
    I developed activex control using MFC activex control wizard. i need to pass events and values from activex control (ocx) to html page or JSP. Any options available
    in MFC activex control wizard to pass those events and values?
    Could anyone point out what I have to do next?
    Thanks in Advance
    Regards
    Jaiganesh

  2. #2
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    5,877

    Re: How to pass the events and values from ocx to html or JSP

    It doesn't matter what framework you use to implement ActiveX - the basics for AcitiveX-to-html interaction stay the same.

    Passing values between ActiveX and html is always made by means of properties defined in ActiveX.

    To firing events outside of ActiveX there should be source interface (events) defined in ActiveX, a connection point mechanism should be implemented incorporating these events - and html code should be aware of ActiveX events by means of <SCRIPT ID="SomeID" FOR="AxObjectInstanceName" EVENT="AxEventEntry()">...</SCRIPT> fragments as many as many events you whish to process.

    Am I clear? Or do you still need a sample?
    Best regards,
    Igor

  3. #3
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    5,877

    Re: How to pass the events and values from ocx to html or JSP

    Ok, the sample.

    Component is built as MFC control. Wizard adds source interface and connection points support by default. We have to add events to control class (right-click in class view and select Add Event... in context menu) and provide some way to fire them. Since our component absolutely dumb we'll make synchronous event firing - Test1 event will be fired on RunTest1 method (of default dispinterface) call, Test2 event will be fired on setting component's OutName property.

    As I previously said HTML page (Test.htm) includes two <script> entries - for each event of the single object instance. RunTest1 method is called by correspondent button click, another button been clicked sets the OutName property value equal to editbox text.
    Attached Files Attached Files
    Best regards,
    Igor

  4. #4
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    5,877

    Re: How to pass the events and values from ocx to html or JSP

    Another sample. Now we have ActiveX control which have child dialog with edit box and button. Text input from edit is fired to HTML page by button click. This is another way to pass some value to container - to implement event with parameter. But previous mechanism (where event with no parameter serves for notification purpose only, but data are passed through properties) stays suitable too (but not implemented in this sample - see previous one).
    Attached Files Attached Files
    Last edited by Igor Vartanov; July 27th, 2005 at 06:20 AM.
    Best regards,
    Igor

  5. #5
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    5,877

    Re: How to pass the events and values from ocx to html or JSP

    I was asked for a kind of step-by-step tutorial for last sample.
    Let's see how it could be implemented in VC++6.

    1. Run MFC ActiveX ControlWizard for AxMfcTest2 project
    • at first screen change nothing and press Next
    • unselect Has an "About" box
    • select Available in "Insert Object" dialog
    • press Finish (since we're going to implement control with child dialog there will none of window classes subclassed)
    • press OK

    2. Add event to control class
    • At ClassView tab select _DAxMfcTest2Events interface with right click and select Add Event... in context menu
    • in Add Event dialog put string "SetText" to External Name field and in Parameter list add parameter szText of BSTR* type.
    • press OK

    Now we have:
    • eventidSetText = 1 added to enum{}
    • void FireSetText(BSTR FAR* szText) protected method added to class CAxMfcTest2Ctl
    • EVENT_CUSTOM("SetText", FireSetText, VTS_PBSTR) entry added to event map of the class

    3. Add child dialog resource to project:
    • add dialog resource, rename it to IDD_MYCTRL
    • delete all controls set by default and add static, edit box (IDC_TEXT) and button (IDC_SETTEXT)
    • tune dialog resource settings:
      - style -> Child
      - border -> None
      - check Control
      - check Control parent

    4. Generate dialog class for child dialog:
    • in resource editor right-click dialog resource and select Class Wizard
    • push OK on Create a new class proposal - New Class wizard dialog should appear with Base Class field set to CDialog and Dialog ID field set to IDD_MYCTRL
    • put CMyCtrlDlg class name and press OK
    • in class wizard dialog select IDC_SETTEXT object ID and doubleclick BN_CLICKED message to generate OnSettext() method
    • close wizard dialog with OK
    • add #include "AxMfcTest2Ctl.h" to MyCtrlDlg.cpp includes
    • add handler code to Settext() (see sources)
    Code:
    // MyCtrlDlg.cpp
    void CMyCtrlDlg::OnSettext() 
    {
    	// TODO: Add your control notification handler code here
    	CString szText;
    	GetDlgItemText( IDC_TEXT, szText );
    	CAxMfcTest2Ctrl* pctrl = (CAxMfcTest2Ctrl*)GetParent();
    	pctrl->SetText(szText);
    }
    5. Tie together dialog and control classes:
    Note: You can see call for non-existent method SetText(CString szText) in code of handler, so we have to add this method to control class to provide event firing with string passed to it
    • add void SetText(CString szText) to control class (see sources)
    Code:
        // AxMfcTest2Ctl.h
    class AxMfcTest2Ctrl: public COleControl
    {
    . . .
    public:
    	void SetText(CString szText) 
    	{ 
    		BSTR sText = szText.AllocSysString();
    		FireSetText( &sText ); 
    		::SysFreeString( sText );
    	}
    };
    Note: Now you can see that we have dialog class but there were no its instances created yet
    • add #include "MyCtrlDlg.h" to AxMfcTest3Ctl.h includes
    • add protected CMyCtrlDlg m_dlg member to control class
    Code:
    . . .
    #include "MyCtrlDlg.h"
    . . .
    protected:
    	CMyCtrlDlg m_dlg;
    6. Add missing parts of control code with Class Wizard:
    • run ClassWizard and select CAxMfcTest2Ctl class
    • add handler for WM_CREATE message

    it will be needed for creation of child dialog window
    Code:
    int CAxMfcTest2Ctrl::OnCreate(LPCREATESTRUCT lpCreateStruct) 
    {
    	if (COleControl::OnCreate(lpCreateStruct) == -1)
    		return -1;
    	
    	// TODO: Add your specialized creation code here
    	ASSERT( ::IsWindow( GetSafeHwnd() ) );
    
    	if( m_dlg.Create( IDD_MYCTRL, this ) )
    		m_dlg.ShowWindow(SW_SHOW);
    	
    	return 0;
    }
    • add OnSetObjectRects() overriding method

    it will resize control window to dialog size
    Code:
    BOOL CAxMfcTest2Ctrl::OnSetObjectRects(LPCRECT lpRectPos, LPCRECT lpRectClip) 
    {
    	// TODO: Add your specialized code here and/or call the base class
    	CRect r;
    	m_dlg.GetWindowRect(r);
    	r.OffsetRect( lpRectPos->left - r.left, lpRectPos->top - r.top );
    
    	return COleControl::OnSetObjectRects(r, r);
    }
    7. Compose test HTML page according to sample
    • go to AxMfcTest2.odl and copy AxMfcTest2 coclass uuid
    • paste it into <object> tag after CLSID: (no braces!)
    Code:
    <HTML>
    <HEAD>
    <META NAME="GENERATOR" Content="Microsoft Developer Studio">
    <META HTTP-EQUIV="Content-Type" content="text/html; charset=windows-1251">
    <TITLE>Document Title</TITLE>
        <script id="OnSetText" for="AxMfc" event="SetText(sText)">
        Text1.value = sText;
        </script>
    </HEAD>
    <BODY>
    
        <object id="AxMfc" width="300" height="220" classid="CLSID:1130F708-876F-4AC8-858E-7CE744557F85">
        </object><br>
        Text got from ActiveX:<br>
        <input type="text" name="Text1"><br>
    
    </BODY>
    </HTML>
    8. Build the project, run the page and test it.

    Finally you will get the project same to attached one.
    Attached Files Attached Files
    Last edited by Igor Vartanov; July 31st, 2005 at 04:28 AM.
    Best regards,
    Igor

  6. #6
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    5,877

    Re: How to pass the events and values from ocx to html or JSP

    Now we will add to our MFC ActiveX ability to report its safety.

    To make it seemed safe for browser we have to implement two category registration for our control. These categories are CATID_SafeForInitializing and CATID_SafeForScripting.

    For this purpose we have to add to our AxMfcTest2 project the AxMfcTest2CtrlCatReg.cpp file (see attachment) and ensure its RegisterControlAsSafe and UnregisterControlAsSafe functions to be called on ActiveX registration and unregistration respectively.

    BTW, you may be surprised with such code in AxMfcTest2CtrlCatReg.cpp:
    Code:
    	CLSID CLSID_AxMfcTest2;
    	hr = CLSIDFromString( L"{1130F708-876F-4AC8-858E-7CE744557F85}", &CLSID_AxMfcTest2 );
    The string "1130F708-876F-4AC8-858E-7CE744557F85" is got from AxMfcTest2.odl - it is just UUID of coclass AxMfcTest2.

    Now let's get back to registration/unregistration.

    The code of AxMfcTest2.cpp should look like this (changes are in bold)
    Code:
    . . .
    /////////////////////////////////////////////////////////////////////////////
    // DllRegisterServer - Adds entries to the system registry
    
    // here we won't mess with header and declare them right here
    HRESULT RegisterControlAsSafe();
    HRESULT UnregisterControlAsSafe();
    
    STDAPI DllRegisterServer(void)
    {
    	AFX_MANAGE_STATE(_afxModuleAddrThis);
    
    	if (!AfxOleRegisterTypeLib(AfxGetInstanceHandle(), _tlid))
    		return ResultFromScode(SELFREG_E_TYPELIB);
    
    	if (!COleObjectFactoryEx::UpdateRegistryAll(TRUE))
    		return ResultFromScode(SELFREG_E_CLASS);
    
    	HRESULT hr = RegisterControlAsSafe();
    	if(FAILED(hr))
    		return hr;
    
    	return NOERROR;
    }
    
    
    /////////////////////////////////////////////////////////////////////////////
    // DllUnregisterServer - Removes entries from the system registry
    
    STDAPI DllUnregisterServer(void)
    {
    	AFX_MANAGE_STATE(_afxModuleAddrThis);
    
    	HRESULT hr = UnregisterControlAsSafe();
    	if(FAILED(hr))
    		return hr;
    
    	if (!AfxOleUnregisterTypeLib(_tlid, _wVerMajor, _wVerMinor))
    		return ResultFromScode(SELFREG_E_TYPELIB);
    
    	if (!COleObjectFactoryEx::UpdateRegistryAll(FALSE))
    		return ResultFromScode(SELFREG_E_CLASS);
    
    	return NOERROR;
    }
    That's all. Build control and have fun!
    Attached Files Attached Files
    Last edited by Igor Vartanov; August 5th, 2005 at 10:13 AM.
    Best regards,
    Igor

  7. #7
    Join Date
    Feb 2002
    Posts
    3,788

    Re: How to pass the events and values from ocx to html or JSP

    Igor, why don't you right an article about what you explained above? It would be great!

  8. #8
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    5,877

    Re: How to pass the events and values from ocx to html or JSP

    Yes, I already thought about to write article. Thanks for your approval.
    Best regards,
    Igor

  9. #9
    Join Date
    Feb 2006
    Posts
    26

    Re: How to pass the events and values from ocx to html or JSP

    Hi Igor,

    I am a java guy, looking for a quick solution for a small component i need. Your code (AxMfcTest2) helped me a lot.

    Just for one quick change, i need exact same exept in opposite. I mean i want to set text when a button(i mean <input type="button"...) clicked on html. I want to pass text from html text box to activex text button when html button is clicked.

    I appreciate if anyone tells me how to do it.

    Thanks,
    John

  10. #10
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    5,877

    Re: How to pass the events and values from ocx to html or JSP

    Quote Originally Posted by zxmn50
    Hi Igor,

    I am a java guy, looking for a quick solution for a small component i need. Your code (AxMfcTest2) helped me a lot.

    Just for one quick change, i need exact same exept in opposite. I mean i want to set text when a button(i mean <input type="button"...) clicked on html. I want to pass text from html text box to activex text button when html button is clicked.

    I appreciate if anyone tells me how to do it.

    Thanks,
    John
    If ActiveX button has Caption property just set it to value you need

    or other way you can use some default interface method for this:
    Code:
    <object name="myAx" ...></object>
    <input type="text" name="my_edt"><br>
    <input type="button" name="my_but" onClick="myAx.SetCaptionString(my_edt.value);">
    Last edited by Igor Vartanov; March 30th, 2006 at 07:36 AM.
    Best regards,
    Igor

  11. #11
    Join Date
    Feb 2006
    Posts
    26

    Question Re: How to pass the events and values from ocx to html or JSP

    Igor,

    Please don't mind. I didn't understand what you said because i don't know vc++. Could you explain little more details or sample code would help.

    I have text box and button in html. I have text box in activesx control. When we click button on html, text should pass from html text box to activex control text box.

    Thanks so much for your help, i have been searching and trying to read about mfc activex controls, it's just been taking long time.

    Thanks,
    John.

  12. #12
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    5,877

    Re: How to pass the events and values from ocx to html or JSP

    Quote Originally Posted by zxmn50
    Please don't mind. I didn't understand what you said because i don't know vc++. Could you explain little more details or sample code would help.
    My reply was in HTML, not C++.

    I have text box and button in html. I have text box in activesx control. When we click button on html, text should pass from html text box to activex control text box.
    Now your description looks more clear.

    You have to think in object categories. Your button and ActiveX dialog are objects of some HTML page. The button press will fire its onClick handler. There you can call the method of ActiveX dialog object passing him the string what you want. This method will set the dialog's edit text with the string you passed. If you implement the ActiveX acording to my sample, you have to know its dialog is just a Windows object having no interface to outer world, I mean it has no communication abilities to HTML's virtual machine except the abilities given to him by the ActiveX control which wraps the dialog. So, to make the string from HTML be able to reach the dialog you have to have the method in ActiveX which will receive the string, access the internal dialog object and instruct it to set the edit text.

    You have to add the method to ActiveX (that addition detailed actions will depend on VisualStudio version), say SetEditText(BSTR text).

    And finally in it you do:
    Code:
       m_dlg.GetDlgItem(IDC_SETTEXT)->SetWindowText(text);
    In HTML the button control should look like:
    Code:
    <input type="button" . . . onClick="myAx.SetEditText(someTextString);">
    Last edited by Igor Vartanov; March 30th, 2006 at 11:06 AM.
    Best regards,
    Igor

  13. #13
    Join Date
    Feb 2006
    Posts
    26

    Exclamation Re: How to pass the events and values from ocx to html or JSP

    hi Igor,

    Thanks for reply, i was in offline for past 3 days.
    Sorry to bug you again. Yes, yyou understood my problem, but i was not sure where to add the additional method you mentioned.
    I tried to do the way you said, but it didn't work. surely, i am missing something.

    I attached sameple project from vc++ 2005, its basically same code from you, with little modifications.

    The purpose of control i am doing is to show a baloon from html. The message has to come from html.
    AcitveX control has a text box, and one button.
    Html page has one text box and a button. When user clicks on html button, Text from Html text box needs to pass to Activex control text box and when user clicks on activex control button it has to show baloon in tray icon.

    Right now activex control is working itself. the problem only i have is passing text from html textbox to activex textbox.


    Please check page1.html file to test.

    Thanks for your help, please suggest where to write that additional method to accept html text.

    John
    Attached Files Attached Files

  14. #14
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    5,877

    Re: How to pass the events and values from ocx to html or JSP

    Quote Originally Posted by zxmn50
    I attached sameple project from vc++ 2005, its basically same code from you, with little modifications.
    Well, John, I've recognized it.

    The purpose of control i am doing is to show a baloon...
    ...please suggest where to write that additional method to accept html text.
    Ok, I got it.

    As it must be a method of default interface, you declare it in IDL and dispatch map.
    Code:
    // AxMfcTest2.idl
    	dispinterface _DAxMfcTest2
    	{
    		properties:
    		methods:
    		[id(1)] void SetMsg(BSTR szMsg);
    	};
    
    // AxMfcTest2Ctrl.cpp
    BEGIN_DISPATCH_MAP(CAxMfcTest2Ctrl, COleControl)
    	DISP_FUNCTION(CAxMfcTest2Ctrl, "SetMsg", SetMsg, VT_EMPTY, VTS_BSTR)
    END_DISPATCH_MAP()
    Now DISP_FUNCTION macro ties together interface method SetMsg and class member function SetMsg, so don't miss it:
    Code:
    // AxMfcTest2Ctrl.h
    protected:
    	void SetMsg(LPCTSTR szMsg);
    
    // AxMfcTest2Ctrl.cpp
    void CAxMfcTest2Ctrl::SetMsg(LPCTSTR szMsg)
    {
    	m_dlg.showBalloon(szMsg);
    }
    That's it. Now it works fine.
    Attached Files Attached Files
    Best regards,
    Igor

  15. #15
    Join Date
    Feb 2006
    Posts
    26

    Smile Re: How to pass the events and values from ocx to html or JSP

    Wonderful, Thanks Igor. I made few changes to send more parameters, and more functionality. Itís all working fine now.

    I spent lot of time trying to learn c++ mfc, discovered lot of things.

    One last question, how do i convert this activex using from shared DLLs to use Static libraries. I don't want this activex to have dependency on client machine.


    Thanks again,
    John

Page 1 of 3 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center