Paul B.
Paul B.

Reputation: 2524

Visual style anomaly in extended open/save file dialog

I followed this article that explains how Windows's built-in file open/save dialogs can be extended using .NET. After some minor changes everything works fine but the appearance of the dialog is slightly different from other dialogs. It's not critical, yet I am curious what could be the reason behind it.

My dialog (flat buttons): My Dialog (flat buttons)

Other dialogs (3D buttons): Other dialogs (3D style buttons)

Upvotes: 1

Views: 142

Answers (2)

Paul B.
Paul B.

Reputation: 2524

LarsTech's solution seems to work in most cases but seemingly not for Office add-ins. Here implementing EnableThemingInScope as described in this Microsoft article and using it with the following code helps.

using( new EnableThemingInScope( true ) )
{
    if (!GetSaveFileName(ref ofn))
    {
        int ret=CommDlgExtendedError();

        if (ret!=0)
        {
            throw new ApplicationException("Couldn't show file open dialog - " + ret.ToString());
        }

        return DialogResult.Cancel;
    }
}

Upvotes: 1

LarsTech
LarsTech

Reputation: 81610

From the comments of that article:

The SaveFileDialogWithEncoding example is great. However, the look and feel are "old style" -- in other words, the buttons and controls don't have the new "XP look" (i.e. rounded buttons, etc.). It's probably a flag setting in one of the fields in the OPENFILENAME structure and I'm looking into that. I was just wondering if you (or anyone else) had any insight to solving that problem.

and then the self-answer:

Never mind -- figured it out. Before you instantiate the form object you need to call Application.EnableVisualStyles() like so:

[STAThread]
static void Main() 
{
  Application.EnableVisualStyles();
  Application.Run(new Form1());
}

Upvotes: 3

Related Questions