2010. szeptember 29., szerda

Check if a component sits on a normal TForm or on an ActiveX one


Problem/Question/Abstract:

I have a component that is used both on an Active Form and on a normal TForm. The component needs to find out if the application it's being used in is an ActiveX project or a normal project. Is there a good way to find out?

Answer:

In case your component is TControl you can use the functions below:

function IsParentFormActiveXOne(Control: TControl): Boolean;
function GetParentForm(Control: TControl): TCustomForm;

    function GetParentForm(Control: TControl): TCustomForm;
    begin
      Result := nil;
      if Assigned(Control) then
        if Control is TCustomForm then
        begin
          Result := Control as TCustomForm;
          if Assigned(Result) and (Result is TForm) and
                                                (TForm(Result).FormStyle = fsMDIForm) then
          begin
            Exit;
          end;
        end
        else
        begin
          if Assigned(Control.Parent) then
            Result := GetParentForm(Control.Parent);
        end;
    end;

    function IsParentFormActiveXOne(Control: TControl): Boolean;
    var
      Form: TCustomForm;
    begin
      Form := GetParentForm(Control);
      Result := Assigned(Form) and (Form is TCustomActiveForm);
    end;

Otherwise simply use:

if TCustomForm(Owner) is TCustomActiveForm then
  { ... }

Nincsenek megjegyzések:

Megjegyzés küldése