Exemple #1
0
        /// <summary>
        /// Registers any attributed fields found on the type</summary>
        /// <param name="type">Type to scan for the attributes Sce.Atf.ImageResourceAttribute and
        /// Sce.Atf.CursorResourceAttribute, which are usually on static public readonly fields</param>
        /// <param name="resourcePath">Path to resources in the given type's assembly, deliminated by '.'
        /// and ending in a '.'</param>
        public static void Register(Type type, string resourcePath)
        {
            Assembly resourceAssembly = type.Assembly;

            FieldInfo[] fields = type.GetFields(
                BindingFlags.Static | BindingFlags.Public);

            foreach (FieldInfo field in fields)
            {
                object[] attributes;

                attributes = field.GetCustomAttributes(typeof(ImageResourceAttribute), false);
                if (attributes.Length > 0)
                {
                    ImageResourceAttribute attribute = attributes[0] as ImageResourceAttribute;
                    string name1 = resourcePath + attribute.ImageName1;
                    field.SetValue(null, name1);
                    string name2 = attribute.ImageName2 != null ? resourcePath + attribute.ImageName2 : null;
                    string name3 = attribute.ImageName2 != null ? resourcePath + attribute.ImageName2 : null;
                    RegisterImage(resourceAssembly, name1, name2, name3);

                    continue;
                }

                attributes = field.GetCustomAttributes(typeof(CursorResourceAttribute), false);
                if (attributes.Length > 0)
                {
                    CursorResourceAttribute attribute = attributes[0] as CursorResourceAttribute;
                    string name = resourcePath + attribute.CursorName;
                    field.SetValue(null, name);
                    RegisterCursor(resourceAssembly, name);

                    continue;
                }
            }
        }
Exemple #2
0
        /// <summary>
        /// Registers any attributed fields found on the type</summary>
        /// <param name="type">Type to scan for the attributes Sce.Atf.ImageResourceAttribute and
        /// Sce.Atf.CursorResourceAttribute, which are usually on static public readonly fields</param>
        /// <param name="resourcePath">Path to resources in the given type's assembly, deliminated by '.'
        /// and ending in a '.'</param>
        public static void Register(Type type, string resourcePath)
        {
            Assembly resourceAssembly = type.Assembly;

            // Register any resources in the assembly
            foreach (var resName in resourceAssembly.GetManifestResourceNames())
            {
                var ext = Path.GetExtension(resName);
                if (string.IsNullOrEmpty(ext) || ext.ToLower() != ".png")
                {
                    continue;
                }

                // 16 bit image is the leading image for lookup
                var fileNameOnly = Path.GetFileNameWithoutExtension(resName);
                if (!fileNameOnly.EndsWith("_16"))
                {
                    continue;
                }

                var    fileNameTemplate = fileNameOnly.Substring(0, fileNameOnly.Length - "_16".Length);
                string imageName16      = resName;
                string imageName24      = fileNameTemplate + "_24" + ext;
                string imageName32      = fileNameTemplate + "_32" + ext;

                if (resourceAssembly.GetManifestResourceInfo(imageName24) == null)
                {
                    imageName24 = null;
                }

                if (resourceAssembly.GetManifestResourceInfo(imageName32) == null)
                {
                    imageName32 = null;
                }

                RegisterImage(imageName16,
                              GdiUtil.GetImage(resourceAssembly, imageName16),
                              imageName24 != null ? GdiUtil.GetImage(resourceAssembly, imageName24) : null,
                              imageName32 != null ? GdiUtil.GetImage(resourceAssembly, imageName32) : null);
            }


            // register fields for custom names
            FieldInfo[] fields = type.GetFields(
                BindingFlags.Static | BindingFlags.Public);

            foreach (FieldInfo field in fields)
            {
                object[] attributes;

                attributes = field.GetCustomAttributes(typeof(ImageResourceAttribute), false);
                if (attributes.Length > 0)
                {
                    ImageResourceAttribute attribute = attributes[0] as ImageResourceAttribute;
                    string name1 = resourcePath + attribute.ImageName1;
                    field.SetValue(null, name1);
                    string name2 = attribute.ImageName2 != null ? resourcePath + attribute.ImageName2 : null;
                    string name3 = attribute.ImageName3 != null ? resourcePath + attribute.ImageName3 : null;
                    RegisterImage(resourceAssembly, name1, name2, name3);

                    continue;
                }

                attributes = field.GetCustomAttributes(typeof(CursorResourceAttribute), false);
                if (attributes.Length > 0)
                {
                    CursorResourceAttribute attribute = attributes[0] as CursorResourceAttribute;
                    string name = resourcePath + attribute.CursorName;
                    field.SetValue(null, name);
                    RegisterCursor(resourceAssembly, name);

                    continue;
                }
            }
        }