Partilhar via


Adding custom DOM to the VS client script intellisense. Part 2: Events.

In my previous post I showed how to create a simple client script intellisense schema. I recei ved some feedback that this stuff is complex. Yes, I have to admit it might look scary. However, there are very few ways exist to describe objects, their methods and events and none of those methods is particularly better than the others. but having all the fancy decorations in place, authoring your own schemas is just a matter of copy/paste. So, for those of you who have no fear :-), let's add a window object, well-known alert method and some events. Here is now the object description looks like.

    [ uuid(00000000-0000-0000-0000-000000000101) ]
coclass window {
[default] dispinterface DispHTMLWindow;
[default, source] dispinterface WindowEvents;
};
 

    [ uuid(00000000-0000-0000-0002-000000000102), hidden ]
dispinterface DispHTMLWindow {
properties:
        methods:
[id(1)] void alert([in] BSTR message);
};

Pretty much the same as the document so you can just copy-paste document declaration and rename it. The difference is in the [default, source] dispinterface WindowEvents; line which is declaration that object has events. Here is how you declare the event source interface:

    [ uuid(00000000-0000-0000-0003-000000001C00), hidden]
dispinterface WindowEvents {
properties:
methods:
[id(1)] void onclick();
[id(2)] void onmousemove();
};
 

Complete file:

import "oaidl.idl";
import "ocidl.idl";

[
uuid(16cbf680-56fb-4af6-9cb7-1589f61b575d),
version(1.0),
helpstring("Test Object Model")
]
library Test
{
importlib("stdole2.tlb");

[ uuid(00000000-0000-0000-0000-000000000100) ]
coclass document {
[default] dispinterface DispDocument;
};

[ uuid(00000000-0000-0000-0002-000000000100), hidden ]
dispinterface DispDocument {
properties:
methods:
[id(1)] void method1([in] long x);
};

[ uuid(00000000-0000-0000-0000-000000000101) ]
coclass window {
[default] dispinterface DispHTMLWindow;
[default, source] dispinterface WindowEvents;
};

[ uuid(00000000-0000-0000-0002-000000000102), hidden ]
dispinterface DispHTMLWindow {
properties:
methods:
[id(1)] void alert([in] BSTR message);
};
 

    [ uuid(00000000-0000-0000-0003-000000001C00), hidden]
dispinterface WindowEvents {
properties:
methods:
[id(1)] void onclick();
[id(2)] void onmousemove();
};
};

Compile the code with MIDL and drop the resulting TLB file in the schemas folder. You now should be able to invoke intellisense on window object and if you select window in the left dropdown, you should see onclick and onmousemove events listed in the right dropdown. You can now use right dropdown to generate client script event handlers.

Comments