简体   繁体   中英

Button glyphs not updated when action's image changes

I'm using an Action Manager, and I have some buttons associated with some of those actions. The glyphs of those buttons are automatically assigned the corresponding images from the action manager's image list. However, if I go and change the Image Index of one of such actions, or otherwise make a change to the actual image within the image list, the button keeps the original glyph from when it was first assigned to the action, and does not update to the new image.

How can I ensure the glyphs of these buttons get updated when I make a change to the action's image?

This is one of the pitfalls of using Delphi's actions. When you assign an action to a button, or any glyph-based control, it makes a copy of the image corresponding with that action. Any changes to that image are not reflected in the button's glyph - or otherwise, any glyph-based controls.

At any time when you make changes to the action's image, you can run this following code which forcefully updates all controls on a form to reflect any and all changes to the action (primarily the image):

procedure UpdateActions(AControl: TWinControl);
var
  C: TControl;
  X: Integer;
begin
  for X := 0 to AControl.ControlCount-1 do begin
    C:= AControl.Controls[X];
    C.Action:= C.Action; //Forces everything to update
    if C is TWinControl then
      UpdateActions(TWinControl(C));
  end;
end;

And you call it like so:

UpdateActions(MyForm);

This will simply recursively iterate through all the controls on the form and assign each control's action back to itself, thus triggering it to update any glyph-based controls with the new image.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM