UWP Framework Direct2D TypeScript


TypeScriptでこんなコードを書いて、Chakra.dllに読み込ませるとButtonクラスはIDispatch*に
なりC++の世界でそれを操作できます。(IDispatchが何物なのかは、ググって下さい。)


declare function CreateWindow(me:any, ty:string,title:string, x:number,y:number,cx:number,cy:number ):void;
declare function SetWindowText( me:any, text:string ): void;
declare function GetWindowText( me:any ): string;

type click_delegate = (me: any) => void;

export module U {
export class Button {

Click : click_delegate;

constructor(title: string,x: number,y:number, cx:number) {
CreateWindow( this, 'button',title,x,y,cx,26);
}

OnClick():void{
if ( this.Click )
this.Click(this);
}

SetText( s: string ): void {
SetWindowText(this, s);
}

GetText(): string {
return GetWindowText(this);
}
}
...

var obj = new U.Button("Create Textbox",100, 500, 300 );
obj.Click = test_function;

new U.ButtonによってCreateWindowが呼ばれます。
thisがIDispatchです。CreateWindowは自前作成でWin32ではありません。
以下はC++のコードです。


D2DButton* btn = new D2DButton();
btn->Create( gparent, gf1, rc, VISIBLE, text.c_str(), L"noname" );
gWindowMap[disp.p] = btn;
btn->SetTarget( disp.p );

btn->OnClick_ = [](D2DButton* b)
{
variant v;
InvokeMethod0(b->GetTarget(), L"OnClick", v); // test_functionが呼ばれる
};

ボタンをクリックするとJavaScrit側のtest_functionが呼ばれます。

以上、JavaScript->C++の関数のcallとC++->JavaScriptのcallです。