unit symbloader;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ImgList, ExtCtrls, ShellAPI, Menus;

type
  Tsymbolloader = class(TForm)
    Label1: TLabel;
    Button1: TButton;
    Button2: TButton;
    Label2: TLabel;
    Panel2: TPanel;
    Panel1: TPanel;
    ScrollBar1: TScrollBar;
    GroupBox1: TGroupBox;
    Image2: TImage;
    high1: TImage;
    Label3: TLabel;
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    procedure ScrollBar1Change(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private-Deklarationen }
    procedure ImageCClick(aSender: TObject);
    procedure CloseForm;
  public
    { Public-Deklarationen }
    procedure LoadIcons(sSourcePath: String);
  end;

var
  symbolloader: Tsymbolloader;
  ImageC: array of TImage;
  aUsedIcon: Integer = 0;
implementation

uses Mainpage;

{$R *.dfm}
// Funktion: Messagebox   Quelle: http://www.Delphi-Treff.de
function ShowMessageDlgEx(const AText, ACaption: string; const ResID: Integer; Style: Cardinal = MB_OK):Cardinal;
var
 lpMsgBoxParams : MsgBoxParams;
begin
 with lpMsgBoxParams do
 begin
   cbSize              := SizeOf(lpMsgBoxParams);
   hwndOwner            := Application.Handle;
   hInstance            := SysInit.hInstance;
   lpszText            := PChar(AText);
   lpszCaption          := PChar(ACaption);
   dwStyle              := MB_USERICON or MB_TOPMOST or Style;
   lpszIcon            := MAKEINTRESOURCE(ResID);
   dwContextHelpID      := 0;
   lpfnMsgBoxCallback  := nil;
   dwLanguageId        := LANG_ENGLISH;
 end;

 Result := Cardinal(MessageBoxIndirect(lpMsgBoxParams));
end;

procedure Tsymbolloader.Button1Click(Sender: TObject);
begin
CloseForm;
end;

procedure Tsymbolloader.Button2Click(Sender: TObject);
begin
 aUsedIcon := -1;
 CloseForm;
end;

procedure Tsymbolloader.CloseForm;
var i: Integer;
begin
//Images aus Array leeren, bei Beenden
i:=0;
while ((panel1.FindComponent('iconimage'+inttostr(i)))<>nil) do
begin
  imageC[i].free;
  Inc(i,1);
end;
Form1.SetIconIndex(aUsedIcon);
Form1.Setgoon(True);
symbolloader.Hide;
end;

procedure Tsymbolloader.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
Button2Click(Self);
end;

procedure Tsymbolloader.ImageCClick(aSender: TObject);
begin
//Geklicktes Element herausfinden
aUsedIcon:=StrToInt(((aSender)as TImage).Hint);
Image2.Picture.Icon:=((aSender)as TImage).Picture.Icon;
high1.Left:=((aSender)as TImage).left-2;
high1.Top:=((aSender)as TImage).Top-2;
end;

procedure Tsymbolloader.LoadIcons(sSourcePath: String);
var icon: TIcon;
  NumberOfIcons, i: Integer;
begin
//Formular anzeigen
symbolloader.Show;
Label2.Caption:=' '+ExtractFileName(sSourcePath)+' ';
//Icons aus Datei laden
icon := TIcon.Create;
      try
        //Anzahl der Symbole in Quelldatei
        NumberOfIcons := ExtractIcon(Handle, PChar(sSourcePath), UINT(-1));
        if NumberOfIcons>0 then begin

        //Arraylänge festlegen
        SetLength(imageC,NumberOfIcons - 1);
        //Untergrundpanel und Scrollbar vorbereiten
        Panel1.Width:=(NumberOfIcons div 6 + 1)*34 + 10;
        panel1.left:=1;
        Scrollbar1.Max:=(NumberOfIcons div 6 + 1);
        Scrollbar1.Position:=0;
        //Symbole in Ansicht laden
        for i := 0 to NumberOfIcons - 1 do
          begin
            icon.Handle := ExtractIcon(Handle, PChar(sSourcePath), i);
            //Wenn Komponente nicht existiert erstellen
            if not (symbolloader.FindComponent('iconimage'+inttostr(i))<>nil) then begin
              imageC[i]:= TImage.Create(Panel1);
              imageC[i].Parent:=Panel1;
              imageC[i].Name:='iconimage'+inttostr(i);
              imageC[i].OnClick:=symbolloader.ImageCClick;
            end;
              //Bild laden und Position ermitteln
              imageC[i].Hint:=IntToStr(i);
              imageC[i].Picture.Icon:=icon;
              imageC[i].width:=32;
              imageC[i].height:=32;
              imageC[i].Center:=True;
              imageC[i].Top:=(i mod 6)*34 + 1;
              imageC[i].Left:=(i div 6)*34 + 1;
          end;
        end
        else
        begin
        ShowMessageDlgEx('Es wurde leider kein Symbol in der ausgewählten Datei gefunden!','Vorgang abgebrochen',101,MB_OK);
        Button2Click(Self);
        end;
      finally
        icon.Free;
      end;
//1. Bild laden, wenn mind. 1 vorhanden
if NumberOfIcons>0 then begin
  Image2.Picture.Icon:=imageC[0].Picture.Icon;
  high1.Show;
  high1.BringToFront;
  high1.Left:= 0;
  high1.Top:= 0;
end
else
high1.Hide;
end;

procedure Tsymbolloader.ScrollBar1Change(Sender: TObject);
begin
//In Ansicht scrollen
Panel1.Left:=0 - Scrollbar1.Position*32;
end;

end.
