Jump to content
TheTired

Getting current Style value and changing style.

Recommended Posts

Is it possible to get current values of element in current style being use and to modify it? 

 

For example, of modify is if I wanted to be able to set windowborderstyle visible between true and false at runtime. Besides being able to read properties set could be useful for custom drawing. 

Share this post


Link to post

Yes, it is possible to get and modify style properties of a control at runtime in Delphi.

To get the current style being used by a control, you can use:

 

StyleName: string;
...
StyleName := Control.StyleName;

This will give you the name of the active style.

Then you can get the style object itself:

Style: TStyle; 
...
Style := StyleServices.GetStyle(StyleName);

Now you can read or modify any of the style properties. For example:

// Read current value
BorderVisible := Style.Properties['BorderVisible'];

// Modify value
Style.Properties['BorderVisible'] := not BorderVisible;

The TStyle has a Properties dictionary that contains all the style elements.

To apply the changes to the control, call:

StyleServices.SetStyle(Control, Style);

So in summary:

  1. Get active style name
  2. Get TStyle object
  3. Modify properties
  4. Apply changed style

This allows you to dynamically tweak the visual styles at runtime.

Share this post


Link to post

FireMonkey, the cross-platform GUI framework used in Embarcadero's RAD Studio, doesn't include the same concept of styles as, say, CSS in web development. Instead, it uses style resources and style objects, which allow you to create and apply graphical properties to various UI elements.

To access and modify the current style being used by an element at runtime, you'd typically need to:

Access the style object of the control (this style object is basically a mini-component tree that's used to draw the control).
Once you have the style object, you can find the element you're interested in and change its properties.
For example, here's some simple code to change a button's text color at runtime:
 

procedure TForm1.Button1Click(Sender: TObject);
var
  TextObj: TText;
begin
  TextObj := TText(Button1.FindStyleResource('text'));
  if Assigned(TextObj) then
    TextObj.FontColor := TAlphaColorRec.Red;  // Change color to red
end;

As you can see, we're using FindStyleResource to get the 'text' style object (which is of type TText) for the button, and then we're changing its FontColor property.

Keep in mind that the names of style objects ('text' in this case) depend on the style being used and the type of control. You'll need to look at the style in the IDE's style designer to know what names to use.

As for a windowborderstyle visible property, I'm not sure if there's an equivalent in FireMonkey as it has a slightly different approach to how windows and borders are managed, compared to something like Windows Forms. You might be able to use the Form.BorderStyle property to control whether or not a form has a border, but this would typically be set at design-time, not runtime. It is possible to manipulate some aspects of the window appearance at runtime using platform-specific code, but it's not as straightforward or cross-platform as most FireMonkey code.

The key idea in FireMonkey is that the appearance of controls is determined by style objects, not by properties of the control itself. If you want to change how a control looks or behaves in some way, you usually need to change the style, not the control

Share this post


Link to post

There is 'windowborderstyle' but using findStyleResource to find it returns nil. One method I can think of is to get current style resource change value and load style again. I was not sure if there was better approach. I have the styles loaded as resources I know how to modify them on Windows but multiple platform approach, I probably need to copy it to its own stream, change the value and set it.  

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×