public void RefreshStyle(IHTMLCurrentStyle style) { object val = _extractor(style); SubItems[0].Text = _name; SubItems[1].Text = val != null?val.ToString() : ""; }
private void RefreshStyleItems(IHTMLCurrentStyle style) { //listViewStyle.BeginUpdate(); foreach (StyleListItem item in listViewStyle.Items) { item.RefreshStyle(style); } //listViewStyle.EndUpdate(); }
public virtual bool NodeRemoveHidden() { IHTMLCurrentStyle cstyle = null; cstyle = (IHTMLCurrentStyle)((IHTMLElement2)pNode).currentStyle; if (cstyle != null && (cstyle.display.ToLower() == "none" || cstyle.visibility.ToLower() == "hidden") ) { pNode.outerHTML = null; return(true); } return(false); }
public bool needsplit(HtmlElement e) //输入节点e一定有孩子,根据节点的背景、字体等信息决定是否需要划分 { HashSet <int> setsplitNum = new HashSet <int>(); ArrayList listbc = new ArrayList(); //背景色 ArrayList listfc = new ArrayList(); //字体色 ArrayList listfs = new ArrayList(); //字体大小 ArrayList listfst = new ArrayList(); //字体信息 foreach (HtmlElement ch in e.Children) //对e中的所有直接孩子节点提取背景色等信息 { mshtml.IHTMLElement2 ch2 = (mshtml.IHTMLElement2)ch.DomElement; IHTMLCurrentStyle chstyle = ch2.currentStyle; listbc.Add(chstyle.backgroundColor); listfc.Add(chstyle.color); listfs.Add(chstyle.fontSize); listfst.Add(chstyle.fontStyle); } //如果有孩子节点的上述样式有所不同则需要分割 foreach (Object value in listbc) { if (listbc[0] != value) //如果有一个是不一样的则需要分割 { return(true); } } foreach (Object value in listfc) { if (listfc[0] != value) //如果有一个是不一样的则需要分割 { return(true); } } foreach (Object value in listfs) { if (listfs[0] != value) //如果有一个是不一样的则需要分割 { return(true); } } foreach (Object value in listfst) { if (listfst[0] != value) //如果有一个是不一样的则需要分割 { return(true); } } return(false); }
public override void RemoveAllHiddenElements() { IHTMLElementCollection ec = (IHTMLElementCollection)GetWebBrowser().GetActiveDocument().body.all; IHTMLCurrentStyle cstyle = null; foreach (IHTMLElement e in ec) { cstyle = (IHTMLCurrentStyle)((IHTMLElement2)e).currentStyle; if (cstyle != null && (cstyle.display.ToLower() == "none" || cstyle.visibility.ToLower() == "hidden") ) { e.outerHTML = null; } Application.DoEvents(); } }
private void buttonLoad_Click(object sender, EventArgs e) { MarkupRange selection = GetSelectedMarkupRange(); if (selection.IsEmpty()) { this.listViewStyle.Enabled = true; IHTMLElement2 element = (IHTMLElement2)selection.Start.CurrentScope; IHTMLCurrentStyle style = element.currentStyle; RefreshStyleItems(style); } else { this.listViewStyle.Enabled = false; } }
//********** Pretreatment *********** public virtual void RemoveAllHiddenElements() { IHTMLElementCollection ec = (IHTMLElementCollection)GetWebBrowser().GetActiveDocument().body.all; IHTMLCurrentStyle cstyle = null; foreach (IHTMLElement e in ec) { cstyle = (IHTMLCurrentStyle)((IHTMLElement2)e).currentStyle; if (cstyle != null && (cstyle.display.ToLower() == "none" || cstyle.visibility.ToLower() == "hidden") ) { e.outerHTML = null; } Application.DoEvents(); } balancer.InvokeBalancePoint(); log.Debug("All Hidden Element Being Removed!"); }
private void PutHighlight(IElement elem) { Debug.Assert(this.lastSelectingElem == null); // Set the background. IHTMLElement2 elem2 = (IHTMLElement2)(elem.nativeElement); IHTMLCurrentStyle crntStyle = elem2.currentStyle; this.savedSelectBackground = crntStyle.backgroundColor; elem.nativeElement.style.backgroundColor = CatStudioConstants.TWEBST_SELECT_BCKG; // outline style is available starting with IE8. if (ie8orLater) { IHTMLStyle6 style = (IHTMLStyle6)(elem.nativeElement.style); IHTMLCurrentStyle5 crntStyle5 = (IHTMLCurrentStyle5)crntStyle; this.savedSelectOutline = crntStyle5.outline; style.outline = CatStudioConstants.TWEBST_SELECT_OUTLINE; } elem.SetAttribute(CatStudioConstants.CRNT_SELECTION_ATTR, "1"); this.lastSelectingElem = elem; }
private object GetFontWeight(IHTMLCurrentStyle style) { return(style.fontWeight); }
private object GetFontFamily(IHTMLCurrentStyle style){ return style.fontFamily; }
private object GetFontFamily(IHTMLCurrentStyle style) { return(style.fontFamily); }
private object GetFontVariant(IHTMLCurrentStyle style) { return(style.fontVariant); }
private object GetFontWeight(IHTMLCurrentStyle style){ return style.fontWeight; }
private object GetFontSize(IHTMLCurrentStyle style) { return(style.fontSize); }
private object GetColor(IHTMLCurrentStyle style){ return style.color; }
private static Color GetColorFromBackgroundImage(Bitmap backgroundImage, Rectangle childBounds, Rectangle containerBounds, IHTMLCurrentStyle containerStyle) { childBounds.Width = Math.Max(3, childBounds.Width); childBounds.Height = Math.Max(3, childBounds.Height); Point center = new Point(childBounds.X + Convert.ToInt32(childBounds.Width / 2f) - containerBounds.X, childBounds.Y + Convert.ToInt32(childBounds.Height / 2f) - containerBounds.Y); // Create a histogram of the 9x9 square around the center Dictionary <Color, int> colorCount = new Dictionary <Color, int>(); int pixelsCounted = 0; for (int x = center.X - 4; x < center.X + 5; x++) { for (int y = center.Y - 4; y < center.Y + 5; y++) { if (x < 0 || y < 0) { continue; } // This is only really valid for tiling background images that are not fixed and // start at the top-left. Oh well. Color pixel = backgroundImage.GetPixel(x % backgroundImage.Width, y % backgroundImage.Height); int count; if (!colorCount.TryGetValue(pixel, out count)) { count = 0; } colorCount[pixel] = ++count; ++pixelsCounted; } } if (pixelsCounted == 0) { return(Color.Transparent); } List <ColorInt> pairs = new List <ColorInt>(colorCount); pairs.Sort(new Comparison <ColorInt>(delegate(ColorInt pairA, ColorInt pairB) { return(pairB.Value - pairA.Value); })); // If the most common color represents more than half // of the pixels, return it if (pairs[0].Value > pixelsCounted / 2) { return(pairs[0].Key); } float alpha = 0, red = 0, green = 0, blue = 0; foreach (ColorInt pair in pairs) { alpha += pair.Key.A * pair.Value; red += pair.Key.R * pair.Value; green += pair.Key.G * pair.Value; blue += pair.Key.B * pair.Value; } alpha /= pixelsCounted; red /= pixelsCounted; blue /= pixelsCounted; green /= pixelsCounted; return(Color.FromArgb( Convert.ToInt32(alpha), Convert.ToInt32(red), Convert.ToInt32(green), Convert.ToInt32(blue))); }
private object GetMargin(IHTMLCurrentStyle style) { return(style.margin); }
public void RefreshStyle(IHTMLCurrentStyle style) { object val = _extractor(style); SubItems[0].Text = _name; SubItems[1].Text = val != null ? val.ToString() : ""; }
private object GetPadding(IHTMLCurrentStyle style) { return(style.padding); }
private static Color GetColorFromBackgroundImage(Bitmap backgroundImage, Rectangle childBounds, Rectangle containerBounds, IHTMLCurrentStyle containerStyle) { childBounds.Width = Math.Max(3, childBounds.Width); childBounds.Height = Math.Max(3, childBounds.Height); Point center = new Point(childBounds.X + Convert.ToInt32(childBounds.Width / 2f) - containerBounds.X, childBounds.Y + Convert.ToInt32(childBounds.Height / 2f) - containerBounds.Y); // Create a histogram of the 9x9 square around the center Dictionary<Color, int> colorCount = new Dictionary<Color, int>(); int pixelsCounted = 0; for (int x = center.X - 4; x < center.X + 5; x++) { for (int y = center.Y - 4; y < center.Y + 5; y++) { if (x < 0 || y < 0) continue; // This is only really valid for tiling background images that are not fixed and // start at the top-left. Oh well. Color pixel = backgroundImage.GetPixel(x % backgroundImage.Width, y % backgroundImage.Height); int count; if (!colorCount.TryGetValue(pixel, out count)) count = 0; colorCount[pixel] = ++count; ++pixelsCounted; } } if (pixelsCounted == 0) return Color.Transparent; List<ColorInt> pairs = new List<ColorInt>(colorCount); pairs.Sort(new Comparison<ColorInt>(delegate (ColorInt pairA, ColorInt pairB) { return pairB.Value - pairA.Value; })); // If the most common color represents more than half // of the pixels, return it if (pairs[0].Value > pixelsCounted / 2) return pairs[0].Key; float alpha = 0, red = 0, green = 0, blue = 0; foreach (ColorInt pair in pairs) { alpha += pair.Key.A * pair.Value; red += pair.Key.R * pair.Value; green += pair.Key.G * pair.Value; blue += pair.Key.B * pair.Value; } alpha /= pixelsCounted; red /= pixelsCounted; blue /= pixelsCounted; green /= pixelsCounted; return Color.FromArgb( Convert.ToInt32(alpha), Convert.ToInt32(red), Convert.ToInt32(green), Convert.ToInt32(blue)); }
private void RefreshStyleItems(IHTMLCurrentStyle style) { //listViewStyle.BeginUpdate(); foreach(StyleListItem item in listViewStyle.Items) item.RefreshStyle(style); //listViewStyle.EndUpdate(); }
private object GetMargin(IHTMLCurrentStyle style){ return style.margin; }
private object GetPadding(IHTMLCurrentStyle style){ return style.padding; }
private object GetFontStyle(IHTMLCurrentStyle style){ return style.fontStyle; }
private object GetColor(IHTMLCurrentStyle style) { return(style.color); }
/// <summary> /// Returns the .NET color that matches an element's background color. /// </summary> /// <param name="element"></param> /// <param name="detectImage"> /// Takes background-image into account when looking for background color. /// This is positionally sensitive so if you're not sure if your elements /// are in the "correct" positions relative to each other (such as in /// Web Layout view) you may want to set this to false. /// </param> /// <param name="pageUrl">The URL that should be used to escape relative background image paths. Can be null.</param> /// <param name="defaultColor"></param> /// <returns></returns> public static Color GetBackgroundColor(IHTMLElement element, bool detectImage, string pageUrl, Color defaultColor) { Rectangle childBounds = new Rectangle(HTMLElementHelper.CalculateOffset(element), new Size(element.offsetWidth, element.offsetHeight)); while (element != null) { IHTMLCurrentStyle style = ((IHTMLElement2)element).currentStyle; string colorStr = style.backgroundColor as string; if (!string.IsNullOrEmpty(colorStr) && colorStr != "transparent") { if (colorStr == "inherit") { detectImage = false; } else { return(GetColorFromHexColor(ParseColorToHex(colorStr), defaultColor)); } } string imageUrl = style.backgroundImage; if (detectImage && !string.IsNullOrEmpty(imageUrl) && imageUrl != "none" && (style.backgroundRepeat == "repeat" || style.backgroundRepeat == "repeat-y")) { StyleUrl styleUrl = new CssParser(imageUrl.Trim()).Next() as StyleUrl; Trace.Assert(styleUrl != null, "Style URL could not be parsed"); if (styleUrl != null) { // If there's a background image URL... string url = styleUrl.LiteralText; using (Stream imageStream = GetStreamForUrl(url, pageUrl, element)) { if (imageStream != null) { // ...and we were able to open/download it... using (Image image = Image.FromStream(imageStream)) { if (image is Bitmap) { // ...and it's a Bitmap, then use it to get the color. Rectangle containerBounds = new Rectangle(HTMLElementHelper.CalculateOffset(element), new Size(element.offsetWidth, element.offsetHeight)); Color color = GetColorFromBackgroundImage((Bitmap)image, childBounds, containerBounds, style); // We can't use semi-transparent backgrounds, keep looking if (color.A == 255) { return(color); } } } } } } } element = element.parentElement; } return(defaultColor); }
private object GetFontVariant(IHTMLCurrentStyle style){ return style.fontVariant; }