Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are basic .NET Windows Forms controls native Win32 controls?

Do .NET Windows Forms applications use the equivalent native Win32 controls for basic controls such as Textbox and Button? WPF is non-native but Windows Forms looks and feels very native. The animations on the Button control look exactly like a Win32 button.

like image 560
Monstieur Avatar asked Apr 24 '13 04:04

Monstieur


People also ask

What Windows form controls?

Windows Forms controls are reusable components that encapsulate user interface functionality and are used in client-side, Windows-based applications. Not only does Windows Forms provide many ready-to-use controls, it also provides the infrastructure for developing your own controls.

What are custom controls in C#?

Custom controls are control that are created by user when Windows Forms controls do not fulfill their application requirements. Simple custom controls can be created by inheriting the Control class, UserControl class, or any Windows Forms controls.

What is the use of control class in Windows form creation?

For more information about authoring custom controls, see Developing Custom Windows Forms Controls with the . NET Framework. The Control class implements very basic functionality required by classes that display information to the user. It handles user input through the keyboard and pointing devices.


1 Answers

Yes, they are…with a couple of exceptions.

Some things in WinForms are custom drawn. They use the native control, but they turn on owner-draw and handle some of the drawing logic internally in C# code. The advantage of this is it allows things like buttons to have a BackColor property that supports a user-defined color instead of the standard system color. Generally, this should be avoided (at least in my opinion) because not only is the effect ugly, there's probably a reason the user chose the color scheme they did. But graphic designers often think they know better than users, so the option exists.

Controls that are implemented like this often expose a FlatStyle property that allows you to change how they are drawn (for example, ButtonBase.FlatStyle) . With FlatStyle.Standard, the .NET Framework does its normal owner drawing, even if you haven't customized any of the control's properties with unusual settings. With FlatStyle.System, the control is rendered directly by Win32 without any owner-draw or other overrides.

You can tell the difference on buttons pretty easily. When set to FlatStyle.System, the blue hover effect on buttons gradually fades in and out. When set to FlatStyle.Standard, the blue glow suddenly appears and disappears. Close, but not quite the same. Combo boxes do the same thing (at least when their DropDownStyle property is set to ComboBoxStyle.DropDownList).

I recommend setting all of the controls that have such a property to FlatStyle.System unless you absolutely require behavior not supported by this FlatStyle.

There are a couple of other exceptions. Some WinForms controls don't exist in Win32, so they aren't backed by native controls. The DataGridView is a good example of such a control.

Finally, the MenuStrip and ContextMenuStrip controls are written entirely in C# code and drawn manually by WinForms. They are not backed in any way by native Win32 controls. This is why they look so horribly ugly on Windows Vista and later, because they're forever stuck using the Office XP style. It looked cool on Windows XP, but it sticks out like a sore thumb on later versions. Changing the rendering style from Professional to System doesn't help very much, either.

Instead, you need to add the original versions of these controls, MainMenu and ContextMenu, to your toolbox. They aren't included by default on recent versions of Visual Studio, but they are absolutely still available for use and aren't going anywhere. Again, I highly recommend using these instead, as they are backed 100% by the native Win32 menus and therefore look as they should regardless of your user's Windows version.

like image 146
Cody Gray Avatar answered Oct 26 '22 06:10

Cody Gray