home *** CD-ROM | disk | FTP | other *** search
- unit EditImp;
-
- interface
-
- uses
- Windows, ActiveX, Classes, Controls, Graphics, Menus, Forms, StdCtrls,
- ComServ, StdVCL, AXCtrls, PBag_TLB;
-
- type
- TEditX = class(TActiveXControl, IEditX, IPersistPropertyBag)
- private
- { Private declarations }
- FDelphiControl: TEdit;
- FEvents: IEditXEvents;
- procedure ChangeEvent(Sender: TObject);
- procedure ClickEvent(Sender: TObject);
- procedure DblClickEvent(Sender: TObject);
- procedure KeyPressEvent(Sender: TObject; var Key: Char);
- protected
- { IPersistPropertyBag }
- { Methods should be aliased so they don't collide with existing names }
- function IPersistPropertyBag.InitNew = PersistPropBagInitNew;
- function IPersistPropertyBag.Load = PersistPropBagLoad;
- function IPersistPropertyBag.Save = PersistPropBagSave;
- function PersistPropBagInitNew: HResult; stdcall;
- function PersistPropBagLoad(const pPropBag: IPropertyBag;
- const pErrorLog: IErrorLog): HResult; stdcall;
- function PersistPropBagSave(const pPropBag: IPropertyBag; fClearDirty: BOOL;
- fSaveAllProperties: BOOL): HResult; stdcall;
- { Protected declarations }
- procedure InitializeControl; override;
- procedure EventSinkChanged(const EventSink: IUnknown); override;
- procedure DefinePropertyPages(DefinePropertyPage: TDefinePropertyPage); override;
- function Get_AutoSelect: WordBool; safecall;
- function Get_AutoSize: WordBool; safecall;
- function Get_BorderStyle: TxBorderStyle; safecall;
- function Get_CharCase: TxEditCharCase; safecall;
- function Get_Color: TColor; safecall;
- function Get_Ctl3D: WordBool; safecall;
- function Get_Cursor: Smallint; safecall;
- function Get_DragCursor: Smallint; safecall;
- function Get_DragMode: TxDragMode; safecall;
- function Get_Enabled: WordBool; safecall;
- function Get_Font: Font; safecall;
- function Get_HideSelection: WordBool; safecall;
- function Get_ImeMode: TxImeMode; safecall;
- function Get_ImeName: WideString; safecall;
- function Get_MaxLength: Integer; safecall;
- function Get_Modified: WordBool; safecall;
- function Get_OEMConvert: WordBool; safecall;
- function Get_ParentColor: WordBool; safecall;
- function Get_ParentCtl3D: WordBool; safecall;
- function Get_PasswordChar: Smallint; safecall;
- function Get_ReadOnly: WordBool; safecall;
- function Get_SelLength: Integer; safecall;
- function Get_SelStart: Integer; safecall;
- function Get_SelText: WideString; safecall;
- function Get_Text: WideString; safecall;
- function Get_Visible: WordBool; safecall;
- procedure AboutBox; safecall;
- procedure Clear; safecall;
- procedure ClearSelection; safecall;
- procedure CopyToClipboard; safecall;
- procedure CutToClipboard; safecall;
- procedure PasteFromClipboard; safecall;
- procedure SelectAll; safecall;
- procedure Set_AutoSelect(Value: WordBool); safecall;
- procedure Set_AutoSize(Value: WordBool); safecall;
- procedure Set_BorderStyle(Value: TxBorderStyle); safecall;
- procedure Set_CharCase(Value: TxEditCharCase); safecall;
- procedure Set_Color(Value: TColor); safecall;
- procedure Set_Ctl3D(Value: WordBool); safecall;
- procedure Set_Cursor(Value: Smallint); safecall;
- procedure Set_DragCursor(Value: Smallint); safecall;
- procedure Set_DragMode(Value: TxDragMode); safecall;
- procedure Set_Enabled(Value: WordBool); safecall;
- procedure Set_Font(const Value: Font); safecall;
- procedure Set_HideSelection(Value: WordBool); safecall;
- procedure Set_ImeMode(Value: TxImeMode); safecall;
- procedure Set_ImeName(const Value: WideString); safecall;
- procedure Set_MaxLength(Value: Integer); safecall;
- procedure Set_Modified(Value: WordBool); safecall;
- procedure Set_OEMConvert(Value: WordBool); safecall;
- procedure Set_ParentColor(Value: WordBool); safecall;
- procedure Set_ParentCtl3D(Value: WordBool); safecall;
- procedure Set_PasswordChar(Value: Smallint); safecall;
- procedure Set_ReadOnly(Value: WordBool); safecall;
- procedure Set_SelLength(Value: Integer); safecall;
- procedure Set_SelStart(Value: Integer); safecall;
- procedure Set_SelText(const Value: WideString); safecall;
- procedure Set_Text(const Value: WideString); safecall;
- procedure Set_Visible(Value: WordBool); safecall;
- end;
-
- implementation
-
- uses SysUtils, ComObj, About1;
-
- { Helper Methods }
-
- const
- DispIDArgs: Longint = DISPID_PROPERTYPUT;
-
- function HandleException: HResult;
- var
- E: TObject;
- begin
- E := ExceptObject;
- if (E is EOleSysError) and (EOleSysError(E).ErrorCode < 0) then
- Result := EOleSysError(E).ErrorCode else
- Result := E_UNEXPECTED;
- end;
-
- { GetDispatchPropValue returns the value of the property associated with }
- { Disp's DispID. }
- function GetDispatchPropValue(Disp: IDispatch; DispID: Integer): OleVariant;
- var
- ExcepInfo: TExcepInfo;
- DispParams: TDispParams;
- Status: HResult;
- begin
- FillChar(DispParams, SizeOf(DispParams), 0);
- Status := Disp.Invoke(DispID, GUID_NULL, 0, DISPATCH_PROPERTYGET, DispParams,
- @Result, @ExcepInfo, nil);
- if Status <> S_OK then DispatchInvokeError(Status, ExcepInfo);
- end;
-
- { SetDispatchPropValue sets the value of the property associated with }
- { Disp's DispID. }
- procedure SetDispatchPropValue(Disp: IDispatch; DispID: Integer;
- const Value: OleVariant);
- var
- ExcepInfo: TExcepInfo;
- DispParams: TDispParams;
- Status: HResult;
- begin
- with DispParams do
- begin
- rgvarg := @Value;
- rgdispidNamedArgs := @DispIDArgs;
- cArgs := 1;
- cNamedArgs := 1;
- end;
- Status := Disp.Invoke(DispId, GUID_NULL, 0, DISPATCH_PROPERTYPUT, DispParams,
- nil, @ExcepInfo, nil);
- if Status <> S_OK then DispatchInvokeError(Status, ExcepInfo);
- end;
-
- { EnumDispatchProperties fills a TStrings with property names and }
- { dispids for the properties of Dispatch. You can use PropType and }
- { VTCode to filter for properties of a specific type, or you can pass }
- { GUID_NULL and VT_EMPTY respectively to get all properties. }
- procedure EnumDispatchProperties(Dispatch: IDispatch; PropType: TGUID;
- VTCode: Integer; PropList: TStrings);
- const
- INVOKE_PROPERTYSET = INVOKE_PROPERTYPUT or INVOKE_PROPERTYPUTREF;
- var
- I: Integer;
- TypeInfo: ITypeInfo;
- TypeAttr: PTypeAttr;
- FuncDesc: PFuncDesc;
- VarDesc: PVarDesc;
-
- procedure SaveName(Id: Integer);
- var
- Name: WideString;
- begin
- OleCheck(TypeInfo.GetDocumentation(Id, @Name, nil, nil, nil));
- if PropList.IndexOfObject(TObject(Id)) = -1 then
- PropList.AddObject(Name, TObject(Id));
- end;
-
- function IsPropType(const TypeInfo: ITypeInfo; TypeDesc: PTypeDesc): Boolean;
- var
- RefInfo: ITypeInfo;
- RefAttr: PTypeAttr;
- IsNullGuid: Boolean;
- begin
- IsNullGuid := IsEqualGuid(PropType, GUID_NULL);
- Result := IsNullGuid and (VTCode = VT_EMPTY);
- if Result then Exit;
- case TypeDesc.vt of
- VT_PTR: Result := IsPropType(TypeInfo, TypeDesc.ptdesc);
- VT_USERDEFINED:
- begin
- OleCheck(TypeInfo.GetRefTypeInfo(TypeDesc.hreftype, RefInfo));
- OleCheck(RefInfo.GetTypeAttr(RefAttr));
- try
- Result := IsEqualGUID(RefAttr.guid, PropType);
- if not Result and (RefAttr.typekind = TKIND_ALIAS) then
- Result := IsPropType(RefInfo, @RefAttr.tdescAlias);
- finally
- RefInfo.ReleaseTypeAttr(RefAttr);
- end;
- end;
- else
- Result := IsNullGuid and (TypeDesc.vt = VTCode);
- end;
- end;
-
- function HasMember(const TypeInfo: ITypeInfo; Cnt, MemID, InvKind: Integer): Boolean;
- var
- I: Integer;
- FuncDesc: PFuncDesc;
- begin
- for I := 0 to Cnt - 1 do
- begin
- OleCheck(TypeInfo.GetFuncDesc(I, FuncDesc));
- try
- if (FuncDesc.memid = MemID) and (FuncDesc.invkind and InvKind <> 0) then
- begin
- Result := True;
- Exit;
- end;
- finally
- TypeInfo.ReleaseFuncDesc(FuncDesc);
- end;
- end;
- Result := False;
- end;
-
- begin
- OleCheck(Dispatch.GetTypeInfo(0,0,TypeInfo));
- if TypeInfo = nil then Exit;
- OleCheck(TypeInfo.GetTypeAttr(TypeAttr));
- try
- for I := 0 to TypeAttr.cVars - 1 do
- begin
- OleCheck(TypeInfo.GetVarDesc(I, VarDesc));
- try
- if (VarDesc.wVarFlags and VARFLAG_FREADONLY <> 0) and
- IsPropType(TypeInfo, @VarDesc.elemdescVar.tdesc) then
- SaveName(VarDesc.memid);
- finally
- TypeInfo.ReleaseVarDesc(VarDesc);
- end;
- end;
- for I := 0 to TypeAttr.cFuncs - 1 do
- begin
- OleCheck(TypeInfo.GetFuncDesc(I, FuncDesc));
- try
- if ((FuncDesc.invkind = INVOKE_PROPERTYGET) and
- HasMember(TypeInfo, TypeAttr.cFuncs, FuncDesc.memid, INVOKE_PROPERTYSET) and
- IsPropType(TypeInfo, @FuncDesc.elemdescFunc.tdesc)) or
- ((FuncDesc.invkind and INVOKE_PROPERTYSET <> 0) and
- HasMember(TypeInfo, TypeAttr.cFuncs, FuncDesc.memid, INVOKE_PROPERTYGET) and
- IsPropType(TypeInfo,
- @FuncDesc.lprgelemdescParam[FuncDesc.cParams - 1].tdesc)) then
- SaveName(FuncDesc.memid);
- finally
- TypeInfo.ReleaseFuncDesc(FuncDesc);
- end;
- end;
- finally
- TypeInfo.ReleaseTypeAttr(TypeAttr);
- end;
- end;
-
- { TEditX }
-
- { TEditX.IPersistPropertyBag }
-
- function TEditX.PersistPropBagInitNew: HResult;
- begin
- // NOTE: A return value of E_NOTIMPL is not allowed. You must return S_OK
- // even if this method does nothing.
- Result := S_OK;
- end;
-
- function TEditX.PersistPropBagLoad(const pPropBag: IPropertyBag;
- const pErrorLog: IErrorLog): HResult;
- var
- PropList: TStringList;
- i: Integer;
- WPropName: WideString;
- PropValue: OleVariant;
- begin
- try
- if pPropBag = nil then
- begin
- Result := E_POINTER;
- Exit;
- end;
- Result := S_OK;
- PropList := TStringList.Create;
- try
- EnumDispatchProperties(Self as IDispatch, GUID_NULL, VT_EMPTY, PropList);
- for i := 0 to PropList.Count - 1 do
- begin
- WPropName := PropList[i];
- if pPropBag.Read(PWideChar(WPropName), PropValue, pErrorLog) = S_OK then
- SetDispatchPropValue(Self as IDispatch, Integer(PropList.Objects[i]),
- PropValue);
- end;
- finally
- PropList.Free;
- end;
- except
- Result := HandleException;
- end;
- end;
-
- function TEditX.PersistPropBagSave(const pPropBag: IPropertyBag;
- fClearDirty: BOOL; fSaveAllProperties: BOOL): HResult;
- var
- PropList: TStringList;
- i: Integer;
- WPropName: WideString;
- begin
- try
- if pPropBag = nil then
- begin
- Result := E_POINTER;
- Exit;
- end;
- Result := S_OK;
- PropList := TStringList.Create;
- try
- EnumDispatchProperties(Self as IDispatch, GUID_NULL, VT_EMPTY, PropList);
- for i := 0 to PropList.Count - 1 do
- begin
- WPropName := PropList[i];
- pPropBag.Write(PWideChar(WPropName),
- GetDispatchPropValue(Self as IDispatch, Integer(PropList.Objects[i])));
- end;
- finally
- PropList.Free;
- end;
- except
- Result := HandleException;
- end;
- end;
-
- { TEditX Delphi-generated methods }
-
- procedure TEditX.InitializeControl;
- begin
- FDelphiControl := Control as TEdit;
- FDelphiControl.OnChange := ChangeEvent;
- FDelphiControl.OnClick := ClickEvent;
- FDelphiControl.OnDblClick := DblClickEvent;
- FDelphiControl.OnKeyPress := KeyPressEvent;
- end;
-
- procedure TEditX.EventSinkChanged(const EventSink: IUnknown);
- begin
- FEvents := EventSink as IEditXEvents;
- end;
-
- procedure TEditX.DefinePropertyPages(DefinePropertyPage: TDefinePropertyPage);
- begin
- { Define property pages here. Property pages are defined by calling
- DefinePropertyPage with the class id of the page. For example,
- DefinePropertyPage(Class_EditXPage); }
- end;
-
- function TEditX.Get_AutoSelect: WordBool;
- begin
- Result := FDelphiControl.AutoSelect;
- end;
-
- function TEditX.Get_AutoSize: WordBool;
- begin
- Result := FDelphiControl.AutoSize;
- end;
-
- function TEditX.Get_BorderStyle: TxBorderStyle;
- begin
- Result := Ord(FDelphiControl.BorderStyle);
- end;
-
- function TEditX.Get_CharCase: TxEditCharCase;
- begin
- Result := Ord(FDelphiControl.CharCase);
- end;
-
- function TEditX.Get_Color: TColor;
- begin
- Result := FDelphiControl.Color;
- end;
-
- function TEditX.Get_Ctl3D: WordBool;
- begin
- Result := FDelphiControl.Ctl3D;
- end;
-
- function TEditX.Get_Cursor: Smallint;
- begin
- Result := Smallint(FDelphiControl.Cursor);
- end;
-
- function TEditX.Get_DragCursor: Smallint;
- begin
- Result := Smallint(FDelphiControl.DragCursor);
- end;
-
- function TEditX.Get_DragMode: TxDragMode;
- begin
- Result := Ord(FDelphiControl.DragMode);
- end;
-
- function TEditX.Get_Enabled: WordBool;
- begin
- Result := FDelphiControl.Enabled;
- end;
-
- function TEditX.Get_Font: Font;
- begin
- GetOleFont(FDelphiControl.Font, Result);
- end;
-
- function TEditX.Get_HideSelection: WordBool;
- begin
- Result := FDelphiControl.HideSelection;
- end;
-
- function TEditX.Get_ImeMode: TxImeMode;
- begin
- Result := Ord(FDelphiControl.ImeMode);
- end;
-
- function TEditX.Get_ImeName: WideString;
- begin
- Result := WideString(FDelphiControl.ImeName);
- end;
-
- function TEditX.Get_MaxLength: Integer;
- begin
- Result := FDelphiControl.MaxLength;
- end;
-
- function TEditX.Get_Modified: WordBool;
- begin
- Result := FDelphiControl.Modified;
- end;
-
- function TEditX.Get_OEMConvert: WordBool;
- begin
- Result := FDelphiControl.OEMConvert;
- end;
-
- function TEditX.Get_ParentColor: WordBool;
- begin
- Result := FDelphiControl.ParentColor;
- end;
-
- function TEditX.Get_ParentCtl3D: WordBool;
- begin
- Result := FDelphiControl.ParentCtl3D;
- end;
-
- function TEditX.Get_PasswordChar: Smallint;
- begin
- Result := Smallint(FDelphiControl.PasswordChar);
- end;
-
- function TEditX.Get_ReadOnly: WordBool;
- begin
- Result := FDelphiControl.ReadOnly;
- end;
-
- function TEditX.Get_SelLength: Integer;
- begin
- Result := FDelphiControl.SelLength;
- end;
-
- function TEditX.Get_SelStart: Integer;
- begin
- Result := FDelphiControl.SelStart;
- end;
-
- function TEditX.Get_SelText: WideString;
- begin
- Result := WideString(FDelphiControl.SelText);
- end;
-
- function TEditX.Get_Text: WideString;
- begin
- Result := WideString(FDelphiControl.Text);
- end;
-
- function TEditX.Get_Visible: WordBool;
- begin
- Result := FDelphiControl.Visible;
- end;
-
- procedure TEditX.AboutBox;
- begin
- ShowEditXAbout;
- end;
-
- procedure TEditX.Clear;
- begin
-
- end;
-
- procedure TEditX.ClearSelection;
- begin
-
- end;
-
- procedure TEditX.CopyToClipboard;
- begin
-
- end;
-
- procedure TEditX.CutToClipboard;
- begin
-
- end;
-
- procedure TEditX.PasteFromClipboard;
- begin
-
- end;
-
- procedure TEditX.SelectAll;
- begin
-
- end;
-
- procedure TEditX.Set_AutoSelect(Value: WordBool);
- begin
- FDelphiControl.AutoSelect := Value;
- end;
-
- procedure TEditX.Set_AutoSize(Value: WordBool);
- begin
- FDelphiControl.AutoSize := Value;
- end;
-
- procedure TEditX.Set_BorderStyle(Value: TxBorderStyle);
- begin
- FDelphiControl.BorderStyle := TBorderStyle(Value);
- end;
-
- procedure TEditX.Set_CharCase(Value: TxEditCharCase);
- begin
- FDelphiControl.CharCase := TEditCharCase(Value);
- end;
-
- procedure TEditX.Set_Color(Value: TColor);
- begin
- FDelphiControl.Color := Value;
- end;
-
- procedure TEditX.Set_Ctl3D(Value: WordBool);
- begin
- FDelphiControl.Ctl3D := Value;
- end;
-
- procedure TEditX.Set_Cursor(Value: Smallint);
- begin
- FDelphiControl.Cursor := TCursor(Value);
- end;
-
- procedure TEditX.Set_DragCursor(Value: Smallint);
- begin
- FDelphiControl.DragCursor := TCursor(Value);
- end;
-
- procedure TEditX.Set_DragMode(Value: TxDragMode);
- begin
- FDelphiControl.DragMode := TDragMode(Value);
- end;
-
- procedure TEditX.Set_Enabled(Value: WordBool);
- begin
- FDelphiControl.Enabled := Value;
- end;
-
- procedure TEditX.Set_Font(const Value: Font);
- begin
- SetOleFont(FDelphiControl.Font, Value);
- end;
-
- procedure TEditX.Set_HideSelection(Value: WordBool);
- begin
- FDelphiControl.HideSelection := Value;
- end;
-
- procedure TEditX.Set_ImeMode(Value: TxImeMode);
- begin
- FDelphiControl.ImeMode := TImeMode(Value);
- end;
-
- procedure TEditX.Set_ImeName(const Value: WideString);
- begin
- FDelphiControl.ImeName := TImeName(Value);
- end;
-
- procedure TEditX.Set_MaxLength(Value: Integer);
- begin
- FDelphiControl.MaxLength := Value;
- end;
-
- procedure TEditX.Set_Modified(Value: WordBool);
- begin
- FDelphiControl.Modified := Value;
- end;
-
- procedure TEditX.Set_OEMConvert(Value: WordBool);
- begin
- FDelphiControl.OEMConvert := Value;
- end;
-
- procedure TEditX.Set_ParentColor(Value: WordBool);
- begin
- FDelphiControl.ParentColor := Value;
- end;
-
- procedure TEditX.Set_ParentCtl3D(Value: WordBool);
- begin
- FDelphiControl.ParentCtl3D := Value;
- end;
-
- procedure TEditX.Set_PasswordChar(Value: Smallint);
- begin
- FDelphiControl.PasswordChar := Char(Value);
- end;
-
- procedure TEditX.Set_ReadOnly(Value: WordBool);
- begin
- FDelphiControl.ReadOnly := Value;
- end;
-
- procedure TEditX.Set_SelLength(Value: Integer);
- begin
- FDelphiControl.SelLength := Value;
- end;
-
- procedure TEditX.Set_SelStart(Value: Integer);
- begin
- FDelphiControl.SelStart := Value;
- end;
-
- procedure TEditX.Set_SelText(const Value: WideString);
- begin
- FDelphiControl.SelText := String(Value);
- end;
-
- procedure TEditX.Set_Text(const Value: WideString);
- begin
- FDelphiControl.Text := TCaption(Value);
- end;
-
- procedure TEditX.Set_Visible(Value: WordBool);
- begin
- FDelphiControl.Visible := Value;
- end;
-
- procedure TEditX.ChangeEvent(Sender: TObject);
- begin
- if FEvents <> nil then FEvents.OnChange;
- end;
-
- procedure TEditX.ClickEvent(Sender: TObject);
- begin
- if FEvents <> nil then FEvents.OnClick;
- end;
-
- procedure TEditX.DblClickEvent(Sender: TObject);
- begin
- if FEvents <> nil then FEvents.OnDblClick;
- end;
-
- procedure TEditX.KeyPressEvent(Sender: TObject; var Key: Char);
- var
- TempKey: Smallint;
- begin
- TempKey := Smallint(Key);
- if FEvents <> nil then FEvents.OnKeyPress(TempKey);
- Key := Char(TempKey);
- end;
-
- initialization
- TActiveXControlFactory.Create(
- ComServer,
- TEditX,
- TEdit,
- Class_EditX,
- 1,
- '',
- 0);
- end.
-