Forum Discussion

Sariali's avatar
Sariali
Contributor
13 years ago

Casting OleObject


Hello again!



Is it possible to cast OleObjects?



Example:





Delphi:





Unit1:

TForm1 = class

  function GetValue : integer;

end;





Unit2:

TForm2 = class

  function GetValue : integer; // same functionname

end;









TestComplete / DelphiScript:





function GetValue(FormName : string) : integer;

begin

  Result := -1;

  if FormName = "Form1" then

    Result := Aliases.Sys.MyProg.Form1.GetValue

  else if FormName = "Form2" then

    Result :=  Aliases.Sys.MyProg.Form2.GetValue



   // ... and so on

end;





procedure Test;

var Value;

begin

  Value := GetValue('Form1'); // the Text of Object "Form1"

end;







It would be pretty nice to have this to avoid writing long case-cascades:





function GetValue(Form : OleObject) : integer;

begin

  Result := Form.GetValue; // The Problem is that the OleObject "Form" don't know about the function "GetValue"

  // How can one cast "Form" so that the function "Form1.GetValue" is accessible?

end;





procedure Test;

var Value;

begin

  Value := GetValue(Form1); // the Object "Form1"

end;













thank you for your help!

Necip Sariali





6 Replies


  • Hi Necip,



    Unlike Delphi, the DelphiScript language does not require strong typing. All available methods and properties are fetched via the IDispatch interface and you do not need to specify the type of an object to work with it.



    So, this should work for you:

    function GetValue(Form); 

    begin 

      Result := Form.GetValue;

    end;




    Of course, you need to get the form object that has the GetValue method in order to pass it to the GetValue script function. If you want to pass only the name of the needed form, you can do this using the Evaluate function:

    function GetValue(FormName); 

    begin 

      Result := Evaluate('Aliases.Sys.MyProg.' + FormName + '.GetValue');  

    end;
  • Hello David,



    I want to figure out the dimensions of a TWinControl. 



    Code in Delphi:




    function TMainUnitHelper.GetScreenCoordinatesOfWinControl(const Control: TWinControl; var x1,y1,x2,y2 : integer) : Boolean;

    var

      p : TPoint;





    begin

      Result := True;

      if Control = nil then

        Exit(False);





      with Control do

      begin

        p := ClientToScreen(Point(Left, Top));

        x1 := p.X;

        y1 := p.Y;





        p := ClientToScreen(Point(Left+Width, Top+Height));

        x2 := p.X;

        y2 := p.Y;

      end;

    end;








    In my Delphi-Script in TC I know the Name of the current WinControl,

    it is called "NameMapping.Sys.DiaShow.MainForm.Diagram"



    The Problem is, that this variable is from type OleObject,

    but my Delphi-function needs a TWinControl.



    so my test failes trying to use GetScreenCoodinatewsOfWinObject




    procedure Test_GetScreenCoordinatesOfWinControl;

    var RetCode, x1,y1,x2,y2;

    begin

      RetCode := NameMapping.Sys.DiaShow.MainForm.TestCompleteHelper.GetScreenCoordinatesOfWinControl(

          NameMapping.Sys.DiaShow.MainForm.Diagram, x1,y1,x2,y2);

    end;




    How can I cast OleObject into TWinControl?





    sincerly

    Necip Sariali
  • ... ok, i found a way : using the handle of the control! 



    function TMainUnitHelper.GetScreenCoordinatesOfHandle(const Hwnd: THandle; var x1,y1,x2,y2 : integer) : Boolean;


    var Control: TWinControl;

    begin

      Result := GetScreenCoordinatesOfWinControl(FindControl(Hwnd), x1,y1,x2,y2);

    end;

  • Hi Necip,



    Why do you use the GetScreenCoordinatesOfWinControl custom function? You can get the needed values using TestComplete means:



    function test;

    var

      obj, x1,y1,x2,y2; 

    begin

      obj := NameMapping.Sys.DiaShow.MainForm.Diagram;

      x1 := obj.ScreenLeft;

      y1 := obj.ScreenTop;

      x2 := obj.ScreenLeft + obj.Width;

      y2 := obj.ScreenTop + obj.Height;

    end;




  • ... there are so many things I don't know, David! thank you!





    i think during production

    that it's better to find quickly a path to the solution

    than spending a lot of time to find the right function.





    ... (thinking) ... but it would be highly desired

    to snip with the finger and TO KNOW...





    this is an idea like many other ideas too that comes true,

    when one pushes it up to the level of so-is-it!