File: activexcontainer.pas

package info (click to toggle)
lazarus 2.0.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 214,460 kB
  • sloc: pascal: 1,862,622; xml: 265,709; cpp: 56,595; sh: 3,008; java: 609; makefile: 535; perl: 297; sql: 222; ansic: 137
file content (639 lines) | stat: -rw-r--r-- 20,261 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
unit activexcontainer;

{$mode delphi}{$H+}

{ Visual ActiveX container.

  Copyright (C) 2011 Ludo Brands

  This library is free software; you can redistribute it and/or modify it
  under the terms of the GNU Library General Public License as published by
  the Free Software Foundation; either version 2 of the License, or (at your
  option) any later version with the following modification:

  As a special exception, the copyright holders of this library give you
  permission to link this library with independent modules to produce an
  executable, regardless of the license terms of these independent modules,and
  to copy and distribute the resulting executable under terms of your choice,
  provided that you also meet, for each linked independent module, the terms
  and conditions of the license of that module. An independent module is a
  module which is not derived from or based on this library. If you modify
  this library, you may extend this exception to your version of the library,
  but you are not obligated to do so. If you do not wish to do so, delete this
  exception statement from your version.

  This program is distributed in the hope that it will be useful, but WITHOUT
  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
  for more details.

  You should have received a copy of the GNU Library General Public License
  along with this library; if not, write to the Free Software Foundation,
  Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1335, USA.
}

interface

uses
  Classes, SysUtils, Controls, Windows, ActiveX, ComObj, Forms, Graphics;

type
  //from OCIDL.h
  PPointF = ^TPointF;
  tagPOINTF = record
    x: Single;
    y: Single;
  end;
  TPointF = tagPOINTF;
  POINTF = TPointF;


  IOleControlSite = interface
    ['{B196B289-BAB4-101A-B69C-00AA00341D07}']
    function OnControlInfoChanged: HResult; stdcall;
    function LockInPlaceActive(fLock: BOOL): HResult; stdcall;
    function GetExtendedControl(out disp: IDispatch): HResult; stdcall;
    function TransformCoords(var ptlHimetric: TPoint; var ptfContainer: TPointF;
      flags: Longint): HResult; stdcall;
    function TranslateAccelerator(msg: PMsg; grfModifiers: Longint): HResult;
       stdcall;
    function OnFocus(fGotFocus: BOOL): HResult; stdcall;
    function ShowPropertyFrame: HResult; stdcall;
  end;

  IPropertyNotifySink = interface
    ['{9BFBBC02-EFF1-101A-84ED-00AA00341D07}']
    function OnChanged(dispid: TDispID): HResult; stdcall;
    function OnRequestEdit(dispid: TDispID): HResult; stdcall;
  end;

  ISimpleFrameSite = interface
    ['{742B0E01-14E6-101B-914E-00AA00300CAB}']
    function PreMessageFilter(wnd: HWnd; msg, wp, lp: Integer;
      out res: Integer; out Cookie: Longint): HResult;
      stdcall;
    function PostMessageFilter(wnd: HWnd; msg, wp, lp: Integer;
      out res: Integer; Cookie: Longint): HResult;
      stdcall;
  end;


  TStatusTextEvent = procedure(Sender: TObject; Status:string) of object;

  { TActiveXContainer }

  TActiveXContainer = class(TWinControl, IUnknown, IOleClientSite,
    IOleControlSite, IOleInPlaceSite, IOleInPlaceFrame, IDispatch)
    private
      FActive: boolean;
      FAttached: boolean;
      FClassName: string;
      FOleObject: IDispatch;
      FOnStatusText: TStatusTextEvent;
      FPrevWndProc:windows.WNDPROC;
      Function GetvObject:variant;
      //IOleClientSite
      Function SaveObject: HResult;StdCall;
      Function GetMoniker(dwAssign: Longint; dwWhichMoniker: Longint;OUT mk: IMoniker):HResult;StdCall;
      Function GetContainer(OUT container: IOleContainer):HResult;StdCall;
      procedure SetActive(AValue: boolean);
      procedure SetClassName(AValue: string);
      procedure SetOleObject(AValue: IDispatch);
      Function ShowObject:HResult;StdCall;
      Function OnShowWindow(fShow: BOOL):HResult;StdCall;
      Function RequestNewObjectLayout:HResult;StdCall;
      //IOleControlSite
      function OnControlInfoChanged: HResult; stdcall;
      function LockInPlaceActive(fLock: BOOL): HResult; stdcall;
      function GetExtendedControl(out disp: IDispatch): HResult; stdcall;
      function TransformCoords(var ptlHimetric: TPoint; var ptfContainer: TPointF;
        flags: Longint): HResult; stdcall;
      function TranslateAccelerator(msg: PMsg; grfModifiers: Longint): HResult;overload;
         stdcall;
      function OnFocus(fGotFocus: BOOL): HResult; stdcall;
      function ShowPropertyFrame: HResult; stdcall;
      //IOleInPlaceSite
      function CanInPlaceActivate : HResult;stdcall;
      function OnInPlaceActivate : HResult;stdcall;
      function OnUIActivate : HResult;stdcall;
      function GetWindowContext(out ppframe:IOleInPlaceFrame;out ppdoc:IOleInPlaceUIWindow;lprcposrect:LPRECT;lprccliprect:LPRECT;lpframeinfo:LPOLEINPLACEFRAMEINFO):hresult; stdcall;
      function Scroll(scrollExtant:TSIZE):hresult; stdcall;
      function OnUIDeactivate(fUndoable:BOOL):hresult; stdcall;
      function OnInPlaceDeactivate :hresult; stdcall;
      function DiscardUndoState :hresult; stdcall;
      function DeactivateAndUndo :hresult; stdcall;
      function OnPosRectChange(lprcPosRect:LPRect):hresult; stdcall;
      //IOleWindow
      function GetWindow(out wnd: HWnd): HResult; stdcall;
      function ContextSensitiveHelp(fEnterMode: BOOL): HResult; stdcall;
       //IOleInPlaceFrame
      function InsertMenus(hmenuShared: HMenu; var menuWidths: TOleMenuGroupWidths): HResult;StdCall;
      function SetMenu(hmenuShared: HMenu; holemenu: HMenu; hwndActiveObject: HWnd): HResult;StdCall;
      function RemoveMenus(hmenuShared: HMenu): HResult;StdCall;
      function SetStatusText(pszStatusText: POleStr): HResult;StdCall;
      function EnableModeless(fEnable: BOOL): HResult;StdCall;
      function TranslateAccelerator(var msg: TMsg; wID: Word): HResult;StdCall;overload;
      //IOleInPlaceUIWindow
      function GetBorder(out rectBorder: TRect):HResult;StdCall;
      function RequestBorderSpace(const borderwidths: TRect):HResult;StdCall;
      function SetBorderSpace(const borderwidths: TRect):HResult;StdCall;
      function SetActiveObject(const activeObject: IOleInPlaceActiveObject;pszObjName: POleStr):HResult;StdCall;
      //IDispatch
      function GetTypeInfoCount(out count : longint) : HResult;stdcall;
      function GetTypeInfo(Index,LocaleID : longint;
        out TypeInfo): HResult;stdcall;
      function GetIDsOfNames(const iid: TGUID; names: Pointer;
        NameCount, LocaleID: LongInt; DispIDs: Pointer) : HResult;stdcall;
      function Invoke(DispID: LongInt;const iid : TGUID;
            LocaleID : longint; Flags: Word;var params;
            VarResult,ExcepInfo,ArgErr : pointer) : HResult;stdcall;
      //internal
      procedure Attach;
      procedure Detach;
   public
      constructor Create(TheOwner: TComponent); override;
      destructor Destroy; override;
      //VT_DISPATCH variant used for late binding
      property vObject:Variant read GetvObject;
   published
      property Align;
      property Anchors;
      property AutoSize;
      property BorderSpacing;
      property ChildSizing;
      //ActiveX object is automatically created from classname and destroyed when set
      property OleClassName:string read FClassName write SetClassName;
      property ClientHeight;
      property ClientWidth;
      property Constraints;
      property DockSite;
      property DragCursor;
      property DragKind;
      property DragMode;
      property Enabled;
      {IDispatch interface for ActiveX object. Overrides classname. Set ComServer
      when you create and destroy the object yourself, fe. using CoClass.
      When Active, returns the IDispatch for the object.
      }
      property ComServer:IDispatch read FOleObject write SetOleObject;
      property ParentShowHint;
      property PopupMenu;
      property ShowHint;
      property TabOrder;
      property TabStop;
      property UseDockManager default True;
      property Visible;
      property OnDockDrop;
      property OnDockOver;
      property OnDragDrop;
      property OnDragOver;
      property OnEndDock;
      property OnEndDrag;
      property OnEnter;
      property OnExit;
      property OnGetSiteInfo;
      property OnGetDockCaption;
      property OnResize;
      property OnStartDock;
      property OnStartDrag;
      property OnStatusText:TStatusTextEvent read FOnStatusText write FOnStatusText;
      property OnUnDock;
      {When set, binds ActiveX component to control.
      When cleared, detaches the component from the control
      If Classname is provided the ActiveX component will also be created and destroyed
      automatically.}
      property Active:boolean read FActive write SetActive;
    end;

implementation

{$ifdef wince}
const
  GWLP_USERDATA=GWL_USERDATA;

function GetWindowLongPtrW(hWnd:HWND; nIndex:longint):LONG;
begin
  result:=GetWindowLongW(hWnd, nIndex);
end;

function SetWindowLongPtrW(hWnd:HWND; nIndex:longint; dwNewLong:LONG):LONG;
begin
  result:=SetWindowLongW(hWnd, nIndex, dwNewLong);
end;

function SetWindowLongPtr(hWnd:HWND; nIndex:longint; dwNewLong:LONG):LONG;
begin
  result:=SetWindowLongW(hWnd, nIndex, dwNewLong);
end;
{$endif wince}

function WndCallback(Ahwnd: HWND; uMsg: UINT; wParam: WParam;
  lParam: LParam): LRESULT; stdcall;
  var
    bounds:TRect;
    DC: HDC;
    size:TPOINT;
    AXC:TActiveXContainer;
begin
  AXC:=TActiveXContainer(GetWindowLongPtrW( Ahwnd, GWLP_USERDATA));
  case uMsg of
    WM_DESTROY:AXC.Detach;
    WM_SIZE:
      begin
      size.x:=(LOWORD(lparam)*2540) div Screen.PixelsPerInch;
      size.y:=(HIWORD(lparam)*2540) div Screen.PixelsPerInch;
      AXC.Width:=LOWORD(lparam);
      AXC.Height:=HIWORD(lparam);
      olecheck((AXC.ComServer as IOleObject).SetExtent(DVASPECT_CONTENT,size));
      bounds:=AXC.ClientRect;
      olecheck((AXC.ComServer as IOleInPlaceObject).SetObjectRects(@bounds,@bounds));
      end;
    WM_PAINT:
       begin
       DC:=GetDC(AXC.handle);
       bounds:=AXC.ClientRect;
       olecheck((AXC.ComServer as IViewObject).Draw(DVASPECT_CONTENT,0,nil,nil,0,DC,@bounds,@bounds,nil,0));
       ReleaseDC(AXC.handle,DC);
      end;
  end;
  result:=CallWindowProc(AXC.FPrevWndProc,Ahwnd, uMsg, WParam, LParam);
end;

{ TActiveXContainer }

function TActiveXContainer.GetvObject: variant;
begin
  result:=FOleObject;
end;

function TActiveXContainer.SaveObject: HResult; StdCall;
begin
  Result := S_OK;
end;

function TActiveXContainer.GetMoniker(dwAssign: Longint; dwWhichMoniker: Longint; out
  mk: IMoniker): HResult; StdCall;
begin
  mk := nil;
  Result := E_NOTIMPL;
end;

function TActiveXContainer.GetContainer(out container: IOleContainer): HResult;
  StdCall;
begin
  container := nil;
  Result := E_NOINTERFACE;
end;

procedure TActiveXContainer.SetActive(AValue: boolean);
begin
  if FActive=AValue then Exit;
  if AValue then
    begin
      if (FClassName='') and not assigned(ComServer) then
        raise exception.Create('OleClassName and ComServer not assigned.');
      if not assigned(FOleObject) then
        FOleObject:=CreateOleObject(FClassName);
      Attach;
    end
  else
    begin
    Detach;
    if FClassName<>'' then //destroy com object
      FOleObject:=nil;
    end;
  FActive:=AValue;
end;

procedure TActiveXContainer.SetClassName(AValue: string);
begin
  if (FClassName=AValue) or FActive then Exit;
  FClassName:=AValue;
end;

procedure TActiveXContainer.SetOleObject(AValue: IDispatch);
begin
  if (FOleObject=AValue) or FActive then Exit;
  FOleObject:=AValue;
end;

function TActiveXContainer.ShowObject: HResult; StdCall;
begin
  Result := S_OK;
end;

function TActiveXContainer.OnShowWindow(fShow: BOOL): HResult; StdCall;
begin
  Result := S_OK;
end;

function TActiveXContainer.RequestNewObjectLayout: HResult; StdCall;
begin
  Result := S_OK;
end;

function TActiveXContainer.OnControlInfoChanged: HResult; stdcall;
begin
  Result := E_NOTIMPL;
end;

function TActiveXContainer.LockInPlaceActive(fLock: BOOL): HResult; stdcall;
begin
  Result := E_NOTIMPL;
end;

function TActiveXContainer.GetExtendedControl(out disp: IDispatch): HResult; stdcall;
begin
  Result := E_NOTIMPL;
end;

function TActiveXContainer.TransformCoords(var ptlHimetric: TPoint;
  var ptfContainer: TPointF; flags: Longint): HResult; stdcall;
begin
  if flags and 4 <> 0 then  //XFORMCOORDS_HIMETRICTOCONTAINER=4
    begin
    ptfContainer.X := (ptlHimetric.X * Screen.PixelsPerInch) div 2540;
    ptfContainer.Y := (ptlHimetric.Y * Screen.PixelsPerInch) div 2540;
    end
  else if assigned(@ptlHimetric) and (flags and 8 <> 0) then  //XFORMCOORDS_CONTAINERTOHIMETRIC = 8
    begin
    ptlHimetric.X := Integer(Round(ptfContainer.X * 2540 / Screen.PixelsPerInch));
    ptlHimetric.Y := Integer(Round(ptfContainer.Y * 2540 / Screen.PixelsPerInch));
    end;
  Result := S_OK;
end;

function TActiveXContainer.TranslateAccelerator(msg: PMsg; grfModifiers: Longint
  ): HResult; stdcall;
begin
  Result := E_NOTIMPL;
end;

function TActiveXContainer.OnFocus(fGotFocus: BOOL): HResult; stdcall;
begin
  Result := S_OK;
end;

function TActiveXContainer.ShowPropertyFrame: HResult; stdcall;
begin
  Result := E_NOTIMPL;
end;

function TActiveXContainer.CanInPlaceActivate: HResult;stdcall;
begin
  Result := S_OK;
end;

function TActiveXContainer.OnInPlaceActivate: HResult;stdcall;
begin
  Result := S_OK;
end;

function TActiveXContainer.OnUIActivate: HResult; stdcall;
begin
  Result := S_OK;
end;

function TActiveXContainer.GetWindowContext(out ppframe: IOleInPlaceFrame; out
  ppdoc: IOleInPlaceUIWindow; lprcposrect: LPRECT; lprccliprect: LPRECT;
  lpframeinfo: LPOLEINPLACEFRAMEINFO): hresult; stdcall;
begin
    if assigned (ppframe) then
      ppframe := Self as IOleInPlaceFrame;
    if assigned(ppdoc) then
      ppdoc:= nil;
    if assigned(lpframeinfo) then
      begin
      lpframeinfo.fMDIApp := False;
      lpframeinfo.cAccelEntries := 0;
      lpframeinfo.haccel := 0;
      lpframeinfo.hwndFrame := Handle;
      end;

    if assigned (lprcPosRect) then
      lprcPosRect^:=GetClientRect;
    if assigned (lprcClipRect) then
      lprcClipRect^:=GetClientRect;

    Result := S_OK;
end;

function TActiveXContainer.Scroll(scrollExtant: TSIZE): hresult; stdcall;
begin
  Result := E_NOTIMPL;
end;

function TActiveXContainer.OnUIDeactivate(fUndoable: BOOL): hresult; stdcall;
begin
  Result := S_OK;
end;

function TActiveXContainer.OnInPlaceDeactivate: hresult; stdcall;
begin
  Result := S_OK;
end;

function TActiveXContainer.DiscardUndoState: hresult; stdcall;
begin
  Result := E_NOTIMPL;
end;

function TActiveXContainer.DeactivateAndUndo: hresult; stdcall;
begin
  Result := E_NOTIMPL;
end;

function TActiveXContainer.OnPosRectChange(lprcPosRect: LPRect): hresult; stdcall;
begin
  Result := S_OK;
end;

function TActiveXContainer.GetWindow(out wnd: HWnd): HResult; stdcall;
begin
  wnd:=Handle;
  Result := S_OK;
end;

function TActiveXContainer.ContextSensitiveHelp(fEnterMode: BOOL): HResult; stdcall;
begin
  Result := E_NOTIMPL;
end;

function TActiveXContainer.InsertMenus(hmenuShared: HMenu;
  var menuWidths: TOleMenuGroupWidths): HResult; StdCall;
begin
  Result := E_NOTIMPL;
end;

function TActiveXContainer.SetMenu(hmenuShared: HMenu; holemenu: HMenu;
  hwndActiveObject: HWnd): HResult; StdCall;
begin
  Result := E_NOTIMPL;
end;

function TActiveXContainer.RemoveMenus(hmenuShared: HMenu): HResult; StdCall;
begin
  Result := S_OK;
end;

function TActiveXContainer.SetStatusText(pszStatusText: POleStr): HResult; StdCall;
begin
  if assigned(FOnStatusText) then
    FOnStatusText(Self,utf8encode(WideString(pszStatusText)));
  Result := S_OK;
end;

function TActiveXContainer.EnableModeless(fEnable: BOOL): HResult; StdCall;
begin
  Result := S_OK;
end;

function TActiveXContainer.TranslateAccelerator(var msg: TMsg; wID: Word): HResult;
  StdCall;
begin
  Result := E_NOTIMPL;
end;

function TActiveXContainer.GetBorder(out rectBorder: TRect): HResult; StdCall;
begin
  Result := INPLACE_E_NOTOOLSPACE;
end;

function TActiveXContainer.RequestBorderSpace(const borderwidths: TRect): HResult;
  StdCall;
begin
  Result := INPLACE_E_NOTOOLSPACE;
end;

function TActiveXContainer.SetBorderSpace(const borderwidths: TRect): HResult;
  StdCall;
begin
  Result := E_NOTIMPL;
end;

function TActiveXContainer.SetActiveObject(
  const activeObject: IOleInPlaceActiveObject; pszObjName: POleStr): HResult;
  StdCall;
begin
  Result := S_OK;
end;

function TActiveXContainer.GetTypeInfoCount(out count: longint): HResult;
  stdcall;
begin
  Count := 0;
  Result := S_OK;
end;

function TActiveXContainer.GetTypeInfo(Index, LocaleID: longint; out TypeInfo
  ): HResult; stdcall;
begin
  Pointer(TypeInfo) := nil;
  Result := E_NOTIMPL;
end;

function TActiveXContainer.GetIDsOfNames(const iid: TGUID; names: Pointer;
  NameCount, LocaleID: LongInt; DispIDs: Pointer): HResult; stdcall;
begin
  Result := E_NOTIMPL;
end;

function TActiveXContainer.Invoke(DispID: LongInt; const iid: TGUID;
  LocaleID: longint; Flags: Word; var params; VarResult, ExcepInfo,
  ArgErr: pointer): HResult; stdcall;
const
  DISPID_AMBIENT_BACKCOLOR         = -701;
  DISPID_AMBIENT_DISPLAYNAME       = -702;
  DISPID_AMBIENT_FONT              = -703;
  DISPID_AMBIENT_FORECOLOR         = -704;
  DISPID_AMBIENT_LOCALEID          = -705;
  DISPID_AMBIENT_MESSAGEREFLECT    = -706;
  DISPID_AMBIENT_USERMODE          = -709;
  DISPID_AMBIENT_UIDEAD            = -710;
  DISPID_AMBIENT_SHOWGRABHANDLES   = -711;
  DISPID_AMBIENT_SHOWHATCHING      = -712;
  DISPID_AMBIENT_SUPPORTSMNEMONICS = -714;
  DISPID_AMBIENT_AUTOCLIP          = -715;
begin
  if (Flags and DISPATCH_PROPERTYGET <> 0) and (VarResult <> nil) then
  begin
    Result := S_OK;
    case DispID of
      DISPID_AMBIENT_BACKCOLOR:
        PVariant(VarResult)^ := Color;
      DISPID_AMBIENT_DISPLAYNAME:
        PVariant(VarResult)^ := OleVariant(Name);
      DISPID_AMBIENT_FONT:
        PVariant(VarResult)^ :=nil;
      DISPID_AMBIENT_FORECOLOR:
        PVariant(VarResult)^ := Font.Color;
      DISPID_AMBIENT_LOCALEID:
        PVariant(VarResult)^ := Integer(GetUserDefaultLCID);
      DISPID_AMBIENT_MESSAGEREFLECT:
        PVariant(VarResult)^ := False;
      DISPID_AMBIENT_USERMODE:
        PVariant(VarResult)^ := not (csDesigning in ComponentState);
      DISPID_AMBIENT_UIDEAD:
        PVariant(VarResult)^ := csDesigning in ComponentState;
      DISPID_AMBIENT_SHOWGRABHANDLES:
        PVariant(VarResult)^ := False;
      DISPID_AMBIENT_SHOWHATCHING:
        PVariant(VarResult)^ := False;
      DISPID_AMBIENT_SUPPORTSMNEMONICS:
        PVariant(VarResult)^ := True;
      DISPID_AMBIENT_AUTOCLIP:
        PVariant(VarResult)^ := True;
    else
      Result := DISP_E_MEMBERNOTFOUND;
    end;
  end else
    Result := DISP_E_MEMBERNOTFOUND;
end;

procedure TActiveXContainer.Attach;
var
  size:TPOINT;
begin
  SetWindowLongPtr(Handle,GWLP_USERDATA, PtrInt(Self));
  FPrevWndProc:=Windows.WNDPROC(SetWindowLongPtr(Handle,GWL_WNDPROC,PtrInt(@WndCallback)));
  FAttached:=true;
  olecheck((FOleObject as IOleObject).SetClientSite(Self as IOleClientSite));
  olecheck((FOleObject as IOleObject).SetHostNames(PWideChar(name),PWideChar(name)));
  size.x:=(Width*2540) div Screen.PixelsPerInch;
  size.y:=(Height*2540) div Screen.PixelsPerInch;
  olecheck((FOleObject as IOleObject).SetExtent(DVASPECT_CONTENT,size));
  olecheck((FOleObject as IOleObject).DoVerb(OLEIVERB_INPLACEACTIVATE,nil,Self as IOleClientSite,0,Handle,ClientRect));
end;

procedure TActiveXContainer.Detach;
const
  OLECLOSE_NOSAVE = 1;
begin
  if FAttached then
    begin
    SetWindowLongPtr(Handle,GWL_WNDPROC,PtrInt(@FPrevWndProc));
    SetWindowLongPtr(Handle,GWLP_USERDATA, 0);
    end;
  if assigned(FOleObject) then
    begin
    olecheck((FOleObject as IOleObject).SetClientSite(nil));
    olecheck((FOleObject as IOleObject).Close(OLECLOSE_NOSAVE));
    end;
end;

constructor TActiveXContainer.Create(TheOwner: TComponent);
begin
  inherited Create(TheOwner);
  parent:=TWinControl(TheOwner);
  Width:=24;
  Height:=24;
end;

destructor TActiveXContainer.Destroy;
begin
  Active:=false; //destroys com object if created by Self
  inherited Destroy;
end;

end.