Copyright Notice
Respect Original Works. Reprinted, please maintain the integrity of the article, and in the form of a hyperlink to indicate the original author "tingsking18" and the main site address, so that other friends can ask and correct.
Previously, we used the event mechanism to call Javascript. Events and Callbacks are the same thing. However, since the thread synchronization problem is not solved, it has never been called. JavaScript does not respond to events sent asynchronously. I called JavaScript using the idispatch interface the day before yesterday. But I always think this is not the case. It will be troublesome.
So let's look back and think about it. Is it okay to use an event as long as the thread synchronizes it? I tried it this morning and it was OK.
The following describes the specific steps:
1. Establish MFC ActiveX Control (method omitted)
2. Add the method invoke on the classwizard automation page. The invoke Function Code is as follows:
Void cforthctrl: invoke (short)
{
M_a =;
_ Beginthread (F, 0, (void *) (this ));
}
3. in the preceding invoke method, the thread function f is called. In the f function, the interface control cforthctrl must be called, so thread synchronization is required. We used the postmessage Method for synchronization (previously we ignored this here, so JavaScript cannot be called ). The code for the F function is as follows:
Void F (void * r)
{
Cforthctrl * P = (cforthctrl *) R;
Sleep (5000 );
P-> M_a + = 10;
Postmessage (p-> m_hwnd, wm_threadfireevent, (wparam) null, (lparam) null );
Return;
}
4. Add the event calljs on the classwizard ActiveX Events page. The event parameter is a short type parameter, which is used to pass the result to JavaScript.
5. added the message ing function for the wm_threadfireevent message. The Code is as follows:
Lresult cforthctrl: onfireeventforthread (wparam, lparam)
{
This-> firecalljs (M_a );
Return true;
}
6. Test it with ActiveX control test container. Check whether the calljs event will be triggered when invoke is called.
7. Open Microsoft ActiveX control pad and insert the control we just compiled. Then add the call button and the code that responds to the calljs function.
<HTML>
<Head>
<Title> new page </title>
</Head>
<Body>
<Script language = "JavaScript" for = "forth1" event = "calljs (PARAM)">
Alert (PARAM );
</SCRIPT>
<Script language = "JavaScript">
Function Test ()
{
Forth1.invoke (1 );
}
</SCRIPT>
<Object ID = "forth1" width = 100 Height = 51
Classid = "CLSID: 52510a20-e949-4213-89fd-8a2ae5a7f6d4">
<Param name = "_ version" value = "65536">
<Param name = "_ extentx" value = "2646">
<Param name = "_ extenty" value = "1323">
<Param name = "_ stockprops" value = "0">
</Object>
<Input type = "button" value = "test" onclick = "test ();">
</Body>
</Html>
Summary:
1. Use the event mechanism to save effort and less code than the previous method.
2. Using the idispatch interface is still very error-prone and it is not easy to find errors.
3. If you use the event method, you only need to use ActiveX control test container for successful debugging and put it on the web page.
4. You do not need to pass the function name in the event mode. You can directly mount the event in JavaScript.
5. Remember to synchronize the thread operation interface !!!!!!