⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 iconctls.pas

📁 动态提示控件
💻 PAS
📖 第 1 页 / 共 2 页
字号:
  FFileName := Value;
  { Initialize icon handles from new icon file. }
  LoadIcons;
  { Call user event handler, if one exists }
  if assigned(FOnFileChange) then
    FOnFileChange(Self);
end;

{ Update the AutoDisable property }
procedure TdfsIconComboBox.SetAutoDisable(Value: boolean);
begin
  { If it's the same, we don't need to do anything }
  if Value = FAutoDisable then exit;
  FAutoDisable := Value;
  { Update the enabled state of control based on new AutoDisable setting }
  UpdateEnabledState;
end;

{ Update the EnableCaching property }
procedure TdfsIconComboBox.SetEnableCaching(Value: boolean);
begin
  { If it's the same, we don't need to do anything }
  if Value = FEnableCaching then exit;
  FEnableCaching := Value;
  { If load on demand is not enabled, we need to load all the icons. }
  if not FEnableCaching then
    LoadIcons;
end;

{ Used to extract icons from files and assign them to a TIcon object }
function TdfsIconComboBox.ReadIcon(const Index: integer): TIcon;
var
  Buff: array[0..255] of char;
begin
  { Create the new icon }
  Result := TIcon.Create;
  { Assign it the icon handle }
  Result.Handle := ExtractIcon(hInstance, StrPCopy(Buff, FileName), Index);
end;

{ Returns the icon for a given combobox index }
function TdfsIconComboBox.GeTdfsIcon(Index: integer): TIcon;
begin
  { If load on demand is enabled... }
  if EnableCaching then
    { Has the icon been loaded yet? }
    if Items.Objects[Index] = NIL then
      { No, we must get the icon and add it to Objects }
      Items.Objects[Index] := ReadIcon(Index);
  { Return the requested icon }
  Result := TIcon(Items.Objects[Index]);
end;

{ Return the size of the item we are drawing }
procedure TdfsIconComboBox.MeasureItem(Index: Integer; var Height: Integer);
begin
  { Ask Windows how tall icons are }
  Height := GetSystemMetrics(SM_CYICON);
end;

{ Draw the item requested in the given rectangle.  Because of the parent's default }
{ behavior, we needn't worry about the State.  That's very nice.                   }
procedure TdfsIconComboBox.DrawItem(Index: Integer; Rect: TRect; State: TOwnerDrawState);
var
  Icon: TIcon;
begin
  { Use the controls canvas for drawing... }
  with Canvas do begin
    try
      { Fill in the rectangle.  The proper brush has already been set up for us,   }
      { so we needn't use State to set it ourselves.                               }
      FillRect(Rect);
      { Get the icon to be drawn }
      Icon := GeTdfsIcon(Index);
      { If nothing has gone wrong, draw the icon.  Theoretically, it should never  }
      { be NIL, but why take the chance?                                           }
      if Icon <> nil then
        { Using the given rectangle, draw the icon on the control's canvas,        }
        { centering it within the rectangle.                                       }
        with Rect do Draw(Left + (Right - Left - Icon.Width) div 2,
                          Top + (Bottom - Top - Icon.Width) div 2, Icon);
    except
      { If anything went wrong, we fall down to here.  You may want to add some    }
      { sort of user notification.  No clean up is necessary since we did not      }
      { create anything.  We'll just ignore the problem and hope it goes away. :)  }
      {!};
    end;
  end;
end;

function TdfsIconComboBox.GetVersion: string;
begin
  Result := DFS_COMBO_VERSION;
end;

procedure TdfsIconComboBox.SetVersion(const Val: string);
begin
  { empty write method, just needed to get it to show up in Object Inspector }
end;



{ TdfsIconListBox Component }

constructor TdfsIconListBox.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FRecreating := FALSE;
  { Set default values }
  FMargin := 5;
  ItemHeight := GetSystemMetrics(SM_CYICON) + FMargin;{ + 6;}
  Style := lbOwnerDrawFixed;
  Font.Name := 'Arial';
  Font.Height := ItemHeight;
  FileName := '';
  FAutoDisable := TRUE;
  FEnableCaching := TRUE;
  FNumberOfIcons := -1;
end;

procedure TdfsIconListBox.CreateParams(var Params: TCreateParams);
begin
  inherited CreateParams(Params);
  Params.Style := Params.Style or LBS_MULTICOLUMN;
{  if Orientation = lbVertical then
    Params.Style := Params.Style or LBS_DISABLENOSCROLL or WS_VSCROLL and (not WS_HSCROLL)
  else
    Params.Style := Params.Style or LBS_DISABLENOSCROLL or WS_HSCROLL and (not WS_VSCROLL);}
end;

procedure TdfsIconListBox.CNDeleteItem(var Msg: TWMDeleteItem);
var
  Icon: TIcon;
begin
  if FRecreating then exit;

  { Don't use GeTdfsIcon here! }
  Icon := TIcon(Items.Objects[Msg.DeleteItemStruct^.itemID]);
  { Free it.  If it is NIL, Free ignores it, so it is safe }
  Icon.Free;
  { Zero out the TIcon we just freed }
  Items.Objects[Msg.DeleteItemStruct^.itemID] := NIL;
end;


{ Initialize the icon handles, which are stored in the Objects property }
procedure TdfsIconListBox.LoadIcons;
  function CounTdfsIcons(Inst: THandle; Filename: PChar): integer;
  var
    TmpIcon: HICON;
  begin
    Result := 0;
    TmpIcon := ExtractIcon(Inst, Filename, Result);
    while (TmpIcon <> 0) do begin
      inc(Result);
      DestroyIcon(TmpIcon);
      TmpIcon := ExtractIcon(Inst, Filename, Result);
    end;
  end;
var
  x: integer;
  Icon: TIcon;
  Buff: array[0..255] of char;
  OldCursor: TCursor;
begin
  { Clear any old icon handles }
  FreeIcons;
  { Reset the contents of the listbox }
  Clear;
  { Update the enabled state of the control }
  UpdateEnabledState;
  { If we have a valid file then setup the combobox. }
  if FileExists(FileName) then begin
    { If we are not loading on demand, set the cursor to an hourglass }
    OldCursor := Screen.Cursor;
    if not EnableCaching then
      Screen.Cursor := crHourGlass;
    { Find out how many icons are in the file }
      FNumberOfIcons := ExtractIcon(hInstance, StrPCopy(Buff, FileName),
         {$IFDEF DFS_WIN32} UINT(-1)); {$ELSE} word(-1)); {$ENDIF}
    { Loop for every icon in the file }
    for x := 0 to NumberOfIcons - 1 do begin
      { If we are not loading on demand... }
      if not EnableCaching then begin
        { Create a TIcon object... }
        Icon := TIcon.Create;
        { and assign the icon to it. }
        Icon.Handle := ExtractIcon(hInstance, Buff, x);
        { Add the icon and a dummy string to the combobox }
        Items.AddObject(Format('%d',[x]), Icon);
      end else
        { We're loading on demand, so just add a dummy string }
        Items.AddObject(Format('%d',[x]), NIL);
    end;
    { Reset the index to the first item. }
    ItemIndex := 0;
    { if not loading on demand, restore the cursor }
    if not EnableCaching then
      Screen.Cursor := OldCursor;
  end;
end;

{ Free the icon resources we created. }
procedure TdfsIconListBox.FreeIcons;
var
  x: integer;
  Icon: TIcon;
begin
  { Loop for every icon }
  for x := 0 to Items.Count-1 do begin
    { Get the icon object }
    Icon := TIcon(Items.Objects[x]);  { Don't use GeTdfsIcon here! }
    { Free it.  If it is NIL, Free ignores it, so it is safe }
    Icon.Free;
    { Zero out the TIcon we just freed }
    Items.Objects[x] := NIL;
  end;
  { Reset the number of Icons to reflect that we have no file. }
  FNumberOfIcons := -1;
end;

{ Disable the control if we don't have a valid filename, and option is enabled }
procedure TdfsIconListBox.UpdateEnabledState;
begin
  if AutoDisable then
    Enabled := FileExists(FileName)
  else
    Enabled := TRUE;
end;

(*
{ Reset the size of the listbox to reflect changes in orientation and IconsDisplayed }
procedure TdfsIconListBox.ResetSize;
var
  NewWidth, NewHeight: integer;
  Multiplier: integer;
begin
  NewWidth := FItemWidth * XIcons + 2;
  NewHeight := ItemHeight * YIcons + GetSystemMetrics(SM_CYHSCROLL) + 4;
  SetBounds(Left, Top, NewWidth+3, NewHeight);
  // Stupid scrollbar
  Multiplier := NumberOfIcons div YIcons;
  if NumberOfIcons mod YIcons > 0 then
    inc(Multiplier);
  if NewWidth >= FItemWidth * Multiplier + 2 then
    SetBounds(Left, Top, NewWidth+3, NewHeight - GetSystemMetrics(SM_CYHSCROLL));
  { I've had nothing but trouble with Delphi's Columns property.  I'll just do
    it myself, thank you very much. }
  {  Columns := XIcons;}
  { Delphi 4 (maybe other versions, too) screws up in SetColumnWidth.  Things
    get out of whack as the width grows larger. Fix it up after Columns set. }
  if HandleAllocated then
//    SendMessage(Handle, LB_SETCOLUMNWIDTH, FItemWidth, 0);
    SendMessage(Handle, LB_SETCOLUMNWIDTH, NewWidth div XIcons, 0);

{
  if Width < FItemWidth * XIcons + 2 then
    Height := ItemHeight * YIcons + GetSystemMetrics(SM_CYHSCROLL) + 1
  else
    Height := ItemHeight * YIcons + 3;
  Width := FItemWidth * XIcons + 2;
  Columns := XIcons;
}
*)
(*  if Orientation = lbVertical then begin
    { Set height to hold the desired number of icons }
    Height := ItemHeight * IconsDisplayed + 2;
    { Set width to an icon plus a scrollbar }
    Width := FItemWidth + GetSystemMetrics(SM_CXVSCROLL) + 10;
    { Make sure we don't have any columns. }
    Columns := 0;
  end else begin
    { Set height to an icon plus a scrollbar }
    Height := ItemHeight + GetSystemMetrics(SM_CYHSCROLL) + 1;
    { Set width to hold the desired number of icons }
    Width := FItemWidth * IconsDisplayed + 2;
    { Set number of columns in the listbox to the desired number of icons }
    Columns := IconsDisplayed;
  end;
end;  *)

{ Update the filename of the icon file. }
procedure TdfsIconListBox.SetFileName(Value: String);
begin
  { If new value is same as old, don't reload icons.  That's silly. }
  if FFileName = Value then exit;
  FFileName := Value;
  { Initialize icon handles from new icon file. }
  LoadIcons;
  { Call user event handler, if one exists }
  if assigned(FOnFileChange) then
    FOnFileChange(Self);
end;

{ Update the AutoDisable property }
procedure TdfsIconListBox.SetAutoDisable(Value: boolean);
begin
  { If it's the same, we don't need to do anything }
  if Value = FAutoDisable then exit;
  FAutoDisable := Value;
  { Update the enabled state of control based on new AutoDisable setting }
  UpdateEnabledState;
end;

{ Update the EnableCaching property }
procedure TdfsIconListBox.SetEnableCaching(Value: boolean);
begin
  { If it's the same, we don't need to do anything }
  if Value = FEnableCaching then exit;
  FEnableCaching := Value;
  { If load on demand is not enabled, we need to load all the icons. }
  if not FEnableCaching then
    LoadIcons;
end;

{ Used to extract icons from files and assign them to a TIcon object }
function TdfsIconListBox.ReadIcon(const Index: integer): TIcon;
var
  Buff: array[0..255] of char;
begin
  { Create the new icon }
  Result := TIcon.Create;
  { Assign it the icon handle }
  Result.Handle := ExtractIcon(hInstance, StrPCopy(Buff, FileName), Index);
end;

{ Returns the icon for a given combobox index }
function TdfsIconListBox.GeTdfsIcon(Index: integer): TIcon;
begin
  { If load on demand is enabled... }
  if EnableCaching then
    { Has the icon been loaded yet? }
    if Items.Objects[Index] = NIL then
      { No, we must get the icon and add it to Objects }
      Items.Objects[Index] := ReadIcon(Index);
  { Return the requested icon }
  Result := TIcon(Items.Objects[Index]);
end;


{ Draw the item requested in the given rectangle.  Because of the parent's default }
{ behavior, we needn't worry about the State.  That's very nice.                   }
procedure TdfsIconListBox.DrawItem(Index: Integer; Rect: TRect; State: TOwnerDrawState);
var
  Icon: TIcon;
begin
  { Use the controls canvas for drawing... }
  with Canvas do begin
    try
      { Fill in the rectangle.  The proper brush has already been set up for us,   }
      { so we needn't use State to set it ourselves.                               }
      FillRect(Rect);
      { Get the icon to be drawn }
      Icon := GeTdfsIcon(Index);
      { If nothing has gone wrong, draw the icon.  Theoretically, it should never  }
      { be NIL, but why take the chance?                                           }
      if Icon <> nil then
        { Using the given rectangle, draw the icon on the control's canvas,        }
        { centering it within the rectangle.                                       }
        with Rect do Draw(Left + (Right - Left - Icon.Width) div 2,
                          Top + (Bottom - Top - Icon.Width) div 2, Icon);
    except
      { If anything went wrong, we fall down to here.  You may want to add some    }
      { sort of user notification.  No clean up is necessary since we did not      }
      { create anything.  We'll just ignore the problem and hope it goes away. :)  }
      {!};
    end;
  end;
end;

procedure TdfsIconListBox.SetMargin(const Value: integer);
begin
  if Value <> FMargin then
  begin
    FMargin := Value;
    if HandleAllocated then
      SendMessage(Handle, LB_SETCOLUMNWIDTH, GetSystemMetrics(SM_CXICON) +
         FMargin, 0);
    ItemHeight := GetSystemMetrics(SM_CYICON) + FMargin;

{    Invalidate;}
  end;
end;

function TdfsIconListBox.GetVersion: string;
begin
  Result := DFS_LIST_VERSION;
end;

procedure TdfsIconListBox.SetVersion(const Val: string);
begin
  { empty write method, just needed to get it to show up in Object Inspector }
end;

procedure TdfsIconListBox.CreateWnd;
begin
  inherited CreateWnd;
  SendMessage(Handle, LB_SETCOLUMNWIDTH, GetSystemMetrics(SM_CXICON) + FMargin,
     0);
end;

{$IFDEF DFS_COMPILER_3_UP}
procedure TdfsIconListBox.CMRecreateWnd(var Message: TMessage);
begin
  FRecreating := TRUE;
  try
    inherited;
  finally
    FRecreating := FALSE;
  end;
end;
{$ENDIF}

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -