How to group constants of Windows API

When defining Windows API constant values, it is better to have them as const

public const int SW_HIDE = 0; public const int SW_SHOWNORMAL = 1; public const int SW_NORMAL = 1; public const int SW_SHOWMINIMIZED = 2; public const int SW_SHOWMAXIMIZED = 3; public const int SW_MAXIMIZE = 3; public const int SW_SHOWNOACTIVATE = 4; public const int SW_SHOW = 5; public const int SW_MINIMIZE = 6; public const int SW_SHOWMINNOACTIVE = 7; public const int SW_SHOWNA = 8; public const int SW_RESTORE = 9; public const int SW_SHOWDEFAULT = 10; public const int SW_MAX = 10; [DllImport( "user32.dll" )] public static extern bool ShowWindow( HandleRef hWnd, int nCmdShow ); 

or group them together as an enumeration.

 public enum SW { SW_HIDE = 0, SW_SHOWNORMAL = 1, SW_NORMAL = 1, SW_SHOWMINIMIZED = 2, SW_SHOWMAXIMIZED = 3, SW_MAXIMIZE = 3, SW_SHOWNOACTIVATE = 4, SW_SHOW = 5, SW_MINIMIZE = 6, SW_SHOWMINNOACTIVE = 7, SW_SHOWNA = 8, SW_RESTORE = 9, SW_SHOWDEFAULT = 10, SW_MAX = 10 } [DllImport( "user32.dll" )] public static extern bool ShowWindow( HandleRef hWnd, SW nCmdShow ); 
+7
c # winapi constants
source share
2 answers

Group them as listings.

Why? Ints are used everywhere, and you can transfer them where, for example, size is needed .. This led to the Frickkin Hungarian notation (szSomething ..) in the first place. The type system was missing, and they tried to “fix” it using a variable naming scheme. Now you are better off with a better type system; use it.

Define the enumerations, group them in a reasonable way, and you will not be Thread.Sleep (WM_User) someday (yes, I am not very serious about this example, but I think you understand).

+5
source share

With the exception of the maintainability of the code, this does not matter.

I recommend using enum ; this allows you to use IntelliSense when calling a function and can help prevent errors.
However, you must provide your enum with a meaningful name, such as WindowShowType .
You can also remove prefixes and possibly standardize names for CamelCase.

+5
source share

All Articles