Loading GUI images at runtime

<< Click to Display Table of Contents >>

Navigation:  Programming > WPImageList > TWPToolbar and TWPImageList >

Loading GUI images at runtime

You can load icon images at runtime. Esspecially when using a resource, this reduces the size of your DFM files and the forms will load faster.

 

You can use the method AddIconsFromResource to load a list of imasge elements (RT_DATA type!) from the resource.

 

In our demo we use some simple code to switch the theme.

Here we also load the images for the WPImageList and also the shading pattern for the editor background.

 

{$IFDEF THEMED}

{$R WPShadePNG.RES} // Draw desktop

 

{$R WPICON16.RES}

{$R WPICON22.RES}

{$R WPICON24.RES}

{$R WPICON32.RES}

{$R WPICON48.RES}

 

{$R WPICONINV16.RES}

{$R WPICONINV22.RES}

{$R WPICONINV24.RES}

{$R WPICONINV32.RES}

{$R WPICONINV48.RES}

 

var pageshadepng_dark : TPNGImage;

   pageshadepng_light : TPNGImage;

 

{$ENDIF}

 

 

procedure TWPMiniEd.ChangeThemeClick(Sender: TObject);

begin

{$IFDEF THEMED}

  case (Sender as TMenuItem).Tag of

     0 :

    begin

       // Assign the background:

      WPDrawRectWithBitmap_bitmap := pageshadepng_dark;

       WPImageList2.Clear;

      WPImageList2.AddIconsFromResource('WPICONINV16,WPICONINV22,WPICONINV24,WPICONINV32,WPICONINV48');

       TStyleManager.TrySetStyle('Windows10 SlateGray');

       Dark1.Checked := true;

       Light1.Checked := false;

       Legacy1.Checked := false;

       WPToolbar1.WPImageList := WPImageList2;

       glWPImageList := WPImageList2;

    end;

     1 :

    begin

        WPDrawRectWithBitmap_bitmap := pageshadepng_light;

        WPImageList2.Clear;

        WPImageList2.AddIconsFromResource('WPICON16,WPICON22,WPICON24,WPICON32,WPICON48');

        TStyleManager.TrySetStyle('Iceberg Classico');

        Dark1.Checked := false;

        Light1.Checked := true;

        Legacy1.Checked := false;

        WPToolbar1.WPImageList := WPImageList2;

        glWPImageList := WPImageList2;

    end;

     2 :

    begin

        // Classic

        WPDrawRectWithBitmap_bitmap := nil;

        WPImageList2.Clear;

        TStyleManager.TrySetStyle('Windows');

        Dark1.Checked := false;

        Light1.Checked := false;

        Legacy1.Checked := true;

        WPToolbar1.WPImageList := nil;

        glWPImageList := nil;

    end;

  end;

{$ENDIF}

end;

 

In the initialization section we load 2 PNG images which can be used to draw the "desktop" of a TWPRichText editors.

 

initialization

 

{$IFDEF THEMED}

 

pageshadepng_dark := TPNGImage.Create;

pageshadepng_dark.LoadFromResourceName(HInstance, WPDesktopShadingNames[wpDrawDarkGlow]);

 

pageshadepng_light := TPNGImage.Create;

pageshadepng_light.LoadFromResourceName(HInstance, WPDesktopShadingNames[wpDrawBlueShade]);

 

{$ENDIF}

 

finalization

 

{$IFDEF THEMED}

pageshadepng_dark.Free;

pageshadepng_light.Free;

{$ENDIF}

 

end.