Should I call Application.EnableVisualStyles() on terminal services?
Asked Answered
D

1

3

In a terminal services/citrix environment, should I call Application.EnableVisualStyles() in my .NET 3.5 WinForms app when my program starts? Or, is it better to refrain from doing that?

I am looking for the option that gives the best performance, and do not need any controls drawn with themes.

Dx answered 15/5, 2011 at 10:40 Comment(0)
P
4

Visual styles are the colors, fonts, and other visual elements that form an operating system theme. Controls will draw with visual styles if the control and the operating system support it. To have an effect, EnableVisualStyles() must be called before creating any controls in the application; typically, EnableVisualStyles() is the first line in the Main function.

So, if you need to have your application look in line with the current OS theme, you need to call this. If the classic Windows look is enough for you, you can skip this. I personally never enable visual styles for my server-only apps (like control panels, etc.).

Below is a configurator tool without the visual styles enabled. It's good looking for me this way so EnableVisualStyles was skipped:

enter image description here

A quick look into Application.EnableVisualStyles() method with reflector revealed below code in the method EnableVisualStyles -> EnableVisualStylesInternal -> CreateActivationContext:

if (!contextCreationSucceeded && OSFeature.Feature.IsPresent(OSFeature.Themes))
    {
      enableThemingActivationContext = new ACTCTX();
      enableThemingActivationContext.cbSize = Marshal.SizeOf(typeof(ACTCTX));
      enableThemingActivationContext.lpSource = dllPath;
      enableThemingActivationContext.lpResourceName = (IntPtr) nativeResourceManifestID;
      enableThemingActivationContext.dwFlags = 8;
      hActCtx = CreateActCtx(ref enableThemingActivationContext);
      contextCreationSucceeded = hActCtx != new IntPtr(-1);
    }

If OSFeature.Feature.IsPresent(OSFeature.Themes) returns false, EnableVisualStyles has absolutely no effect so calling it or not makes no difference.

Possessory answered 15/5, 2011 at 10:55 Comment(2)
Yes, but the terminal server in question will probably have OS themes turned off (it will use the "classic" look), in order to conserve resources. My question is thus if calling Application.EnableVisualStyles() will make any difference in this setting, performance wise. For clients running the app outside of terminal services, I will call EnableVisualStyles().Dx
No Application.EnableVisualStyles() will only be effective if OS theme services and theme support is on so calling EnableVisualStyles or not will have no effect at all (neither performance wise, nor the visuals) if themes are already off.Possessory

© 2022 - 2024 — McMap. All rights reserved.