Dynamic Changing of Form Icon

Any form, that is an operating system window has a custom icon. By default, any form created in the development environment of Foresight Analytics Platform uses a system icon. The IFormControl.Icon property is used to set a custom icon. Click the button in the class inspector in the form design mode next to the Icon property and select the required icon. Only files in the *.ico format are available to select from. During form execution the icon can be changed from application code. To reset the custom icon and set a system icon, set the Icon property to Null.

Consider several examples of setting a custom icon from application code.

Loading from the ImageList Component

If a form contains the ImageList (GlobalImageList) component, the custom icon can be loaded from the component by means of the IImageList.Icon property. If images in other format are loaded to the component, on working they are converted into the *.ico format.

Sub TESTFormOnCreate(Sender: Object; Args: IEventArgs);
Begin
    Self.Icon := ImageList1.Icon(0);
End Sub TESTFormOnCreate;

Loading from Resources

To get icons from the Resources object, use properties of IResourceBinaries.Value, IResourceBinaries.ValueByKey properties of the IResourceBinaries.Load method. After getting the icon as a stream, pass the stream to the GxIcon.CreateFromStream or GxIcon.CreateFromStreamS constructor. Set the created icon for the form:

Sub TESTFormOnCreate(Sender: Object; Args: IEventArgs);
Var
    MB: IMetabase;
    Res: IResourceObject;
Begin
    MB := MetabaseClass.Active;
    Res := MB.ItemById("Project_Resources").Bind As IResourceObject;
    Self.Icon := New GxIcon.CreateFromStream(Res.Binaries.Value("FORM_ICON"));
End Sub TESTFormOnCreate;

Loading Icon from File

To load an icon from file, use the GxIcon.CreateFromFile or GxIcon.CreateFromFileS method:

Sub TESTFormOnCreate(Sender: Object; Args: IEventArgs);
Var
    Size: IGxSize;
Begin
    Size := New GxSize.Create(1616);
    Self.Icon := New GxIcon.CreateFromFileS("C:\Work\Images\Form_Icon.ico", Size);
End Sub TESTFormOnCreate;

See also:

Examples