Beispiel #1
0
        /// <summary>
        /// Creates a new rule and adds them to the given style element.
        /// </summary>
        /// <param name="styleElement">The style element the new rule will be added to.</param>
        /// <param name="ruleName">The name of the rule, including preceding signs (. for class, # for ID).</param>
        /// <param name="rule">The rule content, e.g. "font-family: Verdana" (without quotes and braces).</param>
        public StyleRule(StyleElement styleElement, string ruleName, string rule)
        {
            int insertedRule = ((Interop.IHTMLStyleElement)styleElement.GetBaseElement()).styleSheet.AddRule(ruleName, " ", 0);

            // if succeeded, synchronize content
            Interop.IHTMLStyleSheetRulesCollection rules = ((Interop.IHTMLStyleElement)styleElement.GetBaseElement()).styleSheet.GetRules() as Interop.IHTMLStyleSheetRulesCollection;
            if (rules == null)
            {
                throw new ArgumentException("Cannot create style rule");
            }
            if (insertedRule >= 0)
            {
                styleRule = (Interop.IHTMLRuleStyle)rules.Item(insertedRule).GetStyle();
                RuleName  = ruleName;
            }
            else
            {
                for (int i = 0; i < rules.GetLength(); i++)
                {
                    Interop.IHTMLStyleSheetRule rulestyle = rules.Item(i);
                    if (ruleName.Equals(rulestyle.GetSelectorText()))
                    {
                        styleRule         = rulestyle.GetStyle();
                        styleRule.cssText = rule;
                        RuleName          = ruleName;
                        break;
                    }
                }
            }
        }
Beispiel #2
0
        /////<overloads/>
        ///// <summary>
        ///// Creates a new rule and adds them to the given style element.
        ///// </summary>
        //public StyleRule()
        //{

        //}

        internal void InitializeRule(StyleElement styleElement, string ruleName, string rule)
        {
            ((Interop.IHTMLStyleElement)styleElement.GetBaseElement()).styleSheet.AddRule(ruleName, rule, 0);
            // if succeeded, synchronize content
            Interop.IHTMLStyleSheetRulesCollection rules = ((Interop.IHTMLStyleElement)styleElement.GetBaseElement()).styleSheet.GetRules() as Interop.IHTMLStyleSheetRulesCollection;
            if (rules != null)
            {
                styleRule = (Interop.IHTMLRuleStyle)rules.Item(0).GetStyle();
                RuleName  = ruleName;
            }
        }