2005. július 8., péntek

Knowing Run-Time verses Design-Time mode in an Active Form


How to find out if your ActiveX control is running in Design mode or Run mode


Every now and then an ActiveForm developer will want to add a feature to an ActiveX control that only shows up in either Design-Time or Run-Time (splash screens and nag screens are an example). (Background: Design-Time is when a control is applied to a form in the Visual Basic environment; Run-Time is when the control is running inside of an executable).  If you are using TActiveXControl as you’re base object, you get this for free.  If you are using TActiveForm you have to work for the information.

The property we need is UserMode.  If this property is True we are in Run-Time mode; if False we are in Design-Time mode. UserMode is one of the AmbientProperties that a control’s container is supposed to provide.  Delphi even supplies an interface call IAmbientDispatch in AxCtrls for retrieving the ambient properties.  

To get it: Your ActiveForm has a property called ActiveFormControl.ClientSite.  This property is set sometime before your control's OnShow event (ClientSite will not be set in the constructor or the initialize procedures).  Cast ClientSite to an IAmbientDispatch (found in AxCtrls), then get the UserMode property. (see code below)

procedure TDesignCtrl.HandleOnShow(Sender: TObject);
  b: Boolean;
  pIAmbient: IAmbientDispatch;
  pIAmbient := Self.ActiveFormControl.ClientSite as IAmbientDispatch;
  b := pIAmbient.UserMode;

  if b then
    ShowMessage('is in runtime mode')
    ShowMessage('is in design mode');

There are other properties in the IAmbientDispatch interface that could also be useful to a control writer so feel free to experiment.

Nincsenek megjegyzések:

Megjegyzés küldése