Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows Forms Application - Visual Style?

I'll try to keep this as simple as possible.

A button created in a Windows Forms Application looks like this:

enter image description here

If I create a form manually, buttons I create will look like this:

enter image description here

I looked thoroughly through the Windows Forms Application and found no code that changes the visual style of buttons.

Is there any simple explanation as to why this is happening?

Thanks in advance.

like image 698
Acidic Avatar asked Nov 23 '11 15:11

Acidic


People also ask

Will WinForms be deprecated?

WinForms is based on the user32/GDI technology that has existed since the dawn of modern Windows. It is not going to go anywhere, in all senses of the phrase: it won't get new features; it won't get support dropped.

What is difference between WinForms and Windows Forms?

Winforms and Windows Forms are the same thing. Winforms is the shortened name for Windows Forms.


3 Answers

You will need to call the EnableVisualStyles method, which is by default called in the Main method of the Program class prior to calling Application.Run (when you create a Windows Forms project, with the auto-generated code).

This method enables visual styles for the application. 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. A separate manifest is not required to enable visual styles when calling EnableVisualStyles().

like image 98
Grant Thomas Avatar answered Nov 13 '22 16:11

Grant Thomas


Make sure that you set the buttons UseVisualStyleBackColor to true and that you call Application.EnableVisualStyles(); in your startup code.

static class Program
{
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new frmAddress());
    }

}

EDIT: Effect of UseVisualStyleBackColor:

enter image description here

like image 38
Olivier Jacot-Descombes Avatar answered Nov 13 '22 17:11

Olivier Jacot-Descombes


Even if you have Visual Styles enabled (the default), part of the display of the button is controlled by the Operating System, outside the control of your program. The same program running on Windows 7 with the standard interface will look different than when it's running in classic mode.

Bold emphasis added to the excerpt from the documentaiton to illustrate this point.

http://msdn.microsoft.com/en-us/library/y6kzhf8d(VS.80).aspx

Windows XP introduced a new look and feel to the Windows user interface, with controls that have rounded corners and that change color when you pause your mouse over them. By default, Windows-based applications created with Visual Basic automatically support visual styles, also known as Windows XP Themes. When run on a platform that does not support Windows XP Themes, the application reverts to the traditional Windows look and feel. If you do not want your application to support visual styles, you can change the property on the Application page of the Project Designer.

Also from here: http://msdn.microsoft.com/en-us/library/ms171733(VS.80).aspx

in the Checking for Visual Styles Support section.

the following conditions must be true for the visual styles to work:

  • The operating system supports visual styles.
  • The user has enabled visual styles in the operating system.
  • Visual styles are enabled in the application.
  • Visual styles are being used to draw the client area of application windows.
like image 21
David Avatar answered Nov 13 '22 17:11

David