Exemplo n.º 1
0
        public unsafe WindowClass(
            string className = default,
            ModuleInstance moduleInstance = default,
            ClassStyle classStyle         = ClassStyle.HorizontalRedraw | ClassStyle.VerticalRedraw,
            BrushHandle backgroundBrush   = default,
            IconHandle icon      = default,
            IconHandle smallIcon = default,
            CursorHandle cursor  = default,
            string menuName      = null,
            int menuId           = 0,
            int classExtraBytes  = 0,
            int windowExtraBytes = 0)
        {
            // Handle default values
            className = className ?? Guid.NewGuid().ToString();

            if (backgroundBrush == default)
            {
                backgroundBrush = SystemColor.Window;
            }
            else if (backgroundBrush == BrushHandle.NoBrush)
            {
                backgroundBrush = default;
            }

            if (icon == default)
            {
                icon = IconId.Application;
            }
            else if (icon == IconHandle.NoIcon)
            {
                icon = default;
            }

            if (cursor == default)
            {
                cursor = CursorId.Arrow;
            }
            else if (cursor == CursorHandle.NoCursor)
            {
                cursor = default;
            }

            // Unfortunately GetHINSTANCE isn't part of .NET Standard
            if (moduleInstance == default)
            {
                var    method = typeof(Marshal).GetMethod("GetHINSTANCE");
                Module module = Assembly.GetCallingAssembly().Modules.First();
                moduleInstance = (IntPtr)method.Invoke(null, new object[] { module });
            }

            if (menuId != 0 && menuName != null)
            {
                throw new ArgumentException("Can only set menu name or ID.");
            }

            _windowProcedure = WindowProcedure;
            ModuleInstance   = moduleInstance;

            _className = className;
            _menuName  = menuName ?? string.Empty;

            _wndClass = new WNDCLASSEX
            {
                cbSize        = (uint)sizeof(WNDCLASSEX),
                style         = classStyle,
                lpfnWndProc   = Marshal.GetFunctionPointerForDelegate(_windowProcedure),
                cbClassExtra  = classExtraBytes,
                cbWndExtra    = windowExtraBytes,
                hInstance     = (IntPtr)moduleInstance,
                hIcon         = icon,
                hCursor       = cursor,
                hbrBackground = backgroundBrush,
                hIconSm       = smallIcon,
                lpszMenuName  = (char *)menuId
            };
        }
Exemplo n.º 2
0
        public unsafe WindowClass(
            string?className = default,
            ModuleInstance?moduleInstance = default,
            ClassStyle classStyle         = ClassStyle.HorizontalRedraw | ClassStyle.VerticalRedraw,
            BrushHandle backgroundBrush   = default,
            IconHandle icon      = default,
            IconHandle smallIcon = default,
            CursorHandle cursor  = default,
            string?menuName      = null,
            int menuId           = 0,
            int classExtraBytes  = 0,
            int windowExtraBytes = 0)
        {
            // Handle default values
            className ??= Guid.NewGuid().ToString();

            if (backgroundBrush == default)
            {
                backgroundBrush = SystemColor.Window;
            }
            else if (backgroundBrush == BrushHandle.NoBrush)
            {
                backgroundBrush = default;
            }

            if (icon == default)
            {
                icon = IconId.Application;
            }
            else if (icon == IconHandle.NoIcon)
            {
                icon = default;
            }

            if (cursor == default)
            {
                cursor = CursorId.Arrow;
            }
            else if (cursor == CursorHandle.NoCursor)
            {
                cursor = default;
            }

            moduleInstance ??= new ModuleInstance(Marshal.GetHINSTANCE(Assembly.GetCallingAssembly().Modules.First()));

            if (menuId != 0 && menuName != null)
            {
                throw new ArgumentException($"Can't set both {nameof(menuName)} and {nameof(menuId)}.");
            }

            _windowProcedure = WindowProcedure;
            ModuleInstance   = moduleInstance;

            _className = className;
            _menuName  = menuName ?? string.Empty;

            _wndClass = new WNDCLASSEX
            {
                cbSize        = (uint)sizeof(WNDCLASSEX),
                style         = classStyle,
                lpfnWndProc   = Marshal.GetFunctionPointerForDelegate(_windowProcedure),
                cbClassExtra  = classExtraBytes,
                cbWndExtra    = windowExtraBytes,
                hInstance     = (IntPtr)moduleInstance,
                hIcon         = icon,
                hCursor       = cursor,
                hbrBackground = backgroundBrush,
                hIconSm       = smallIcon,
                lpszMenuName  = (char *)menuId
            };
        }