public void Render(System.Drawing.Graphics target) { if (target != null) { IntPtr hdc = target.GetHdc(); try { this.RenderInternal(new HandleRef(target, hdc), this); } finally { target.ReleaseHdcInternal(hdc); } } }
/// <include file='doc\GridEntry.uex' path='docs/doc[@for="GridEntry.PaintValue"]/*' /> /// <devdoc> /// Paints the value portion of this GridEntry into the given Graphics object. This /// is called by the GridEntry host (the PropertyGridView) when this GridEntry is /// to be painted. /// </devdoc> public virtual void PaintValue(object val, System.Drawing.Graphics g, Rectangle rect, Rectangle clipRect, PaintValueFlags paintFlags) { PropertyGridView gridHost = this.GridEntryHost; Debug.Assert(gridHost != null, "No propEntryHost"); int cPaint = 0; Color textColor = gridHost.GetTextColor(); if (this.ShouldRenderReadOnly) { textColor = GridEntryHost.GrayTextColor; } string strValue; if ((paintFlags & PaintValueFlags.FetchValue) != PaintValueFlags.None) { if (cacheItems != null && cacheItems.useValueString) { strValue = cacheItems.lastValueString; val = cacheItems.lastValue; } else { val = this.PropertyValue; strValue = GetPropertyTextValue(val); if (cacheItems == null) { cacheItems = new CacheItems(); } cacheItems.lastValueString = strValue; cacheItems.useValueString = true; cacheItems.lastValueTextWidth = -1; cacheItems.lastValueFont = null; cacheItems.lastValue = val; } } else { strValue = GetPropertyTextValue(val); } // paint out the main rect using the appropriate brush Brush bkBrush = this.GetBackgroundBrush(g); Debug.Assert(bkBrush != null, "We didn't find a good background brush for PaintValue"); if ((paintFlags & PaintValueFlags.DrawSelected) != PaintValueFlags.None) { bkBrush = gridHost.GetSelectedItemWithFocusBackBrush(g); textColor = gridHost.GetSelectedItemWithFocusForeColor(); } Brush blank = bkBrush; #if PBRS_PAINT_DEBUG blank = Brushes.Yellow; #endif //g.FillRectangle(blank, rect.X-1, rect.Y, rect.Width+1, rect.Height); g.FillRectangle(blank, clipRect); if (IsCustomPaint) { cPaint = gridHost.GetValuePaintIndent(); Rectangle rectPaint = new Rectangle(rect.X + 1, rect.Y + 1, gridHost.GetValuePaintWidth(), gridHost.GetGridEntryHeight() - 2); if (!Rectangle.Intersect(rectPaint, clipRect).IsEmpty) { UITypeEditor uie = UITypeEditor; if (uie != null) { uie.PaintValue(new PaintValueEventArgs(this, val, g, rectPaint)); } // paint a border around the area rectPaint.Width --; rectPaint.Height--; g.DrawRectangle(SystemPens.WindowText, rectPaint); } } rect.X += cPaint + gridHost.GetValueStringIndent(); rect.Width -= cPaint + 2 * gridHost.GetValueStringIndent(); // bold the property if we need to persist it (e.g. it's not the default value) bool valueModified = ((paintFlags & PaintValueFlags.CheckShouldSerialize) != PaintValueFlags.None) && ShouldSerializePropertyValue(); // If we have text to paint, paint it if (strValue != null && strValue.Length > 0) { Font f = GetFont(valueModified); if (strValue.Length > maximumLengthOfPropertyString) { strValue = strValue.Substring(0, maximumLengthOfPropertyString); } int textWidth = GetValueTextWidth(strValue, g, f); bool doToolTip = false; //subhag (66503) To check if text contains multiple lines // if (textWidth >= rect.Width || GetMultipleLines(strValue)) doToolTip = true; if (Rectangle.Intersect(rect, clipRect).IsEmpty) { return; } // Do actual drawing //strValue = ReplaceCRLF(strValue); // AS/URT 55015 // bump the text down 2 pixels and over 1 pixel. if ((paintFlags & PaintValueFlags.PaintInPlace) != PaintValueFlags.None) { rect.Offset(1, 2); } else { // only go down one pixel when we're painting in the listbox rect.Offset(1, 1); } Matrix m = g.Transform; IntPtr hdc = g.GetHdc(); IntNativeMethods.RECT lpRect = IntNativeMethods.RECT.FromXYWH(rect.X + (int)m.OffsetX + 2, rect.Y + (int)m.OffsetY - 1, rect.Width - 4, rect.Height); IntPtr hfont = GetHfont(valueModified); int oldTextColor = 0; int oldBkColor = 0; Color bkColor = ((paintFlags & PaintValueFlags.DrawSelected) != PaintValueFlags.None) ? GridEntryHost.GetSelectedItemWithFocusBackColor() : GridEntryHost.BackColor; try { oldTextColor = SafeNativeMethods.SetTextColor(new HandleRef(g, hdc), SafeNativeMethods.RGBToCOLORREF(textColor.ToArgb())); oldBkColor = SafeNativeMethods.SetBkColor(new HandleRef(g, hdc), SafeNativeMethods.RGBToCOLORREF(bkColor.ToArgb())); hfont = SafeNativeMethods.SelectObject(new HandleRef(g, hdc), new HandleRef(null, hfont)); int format = IntNativeMethods.DT_EDITCONTROL | IntNativeMethods.DT_EXPANDTABS | IntNativeMethods.DT_NOCLIP | IntNativeMethods.DT_SINGLELINE | IntNativeMethods.DT_NOPREFIX; if (gridHost.DrawValuesRightToLeft) { format |= IntNativeMethods.DT_RIGHT | IntNativeMethods.DT_RTLREADING; } // For password mode, Replace the string value either with * or a bullet, depending on the OS platform if (ShouldRenderPassword) { if (passwordReplaceChar == (char)0) { if (Environment.OSVersion.Version.Major > 4) { passwordReplaceChar = (char)0x25CF; // Bullet is 2022, but edit box uses round circle 25CF } else { passwordReplaceChar = '*'; } } strValue = new string(passwordReplaceChar, strValue.Length); } IntUnsafeNativeMethods.DrawText(new HandleRef(g, hdc), strValue, ref lpRect, format); } finally { SafeNativeMethods.SetTextColor(new HandleRef(g, hdc), oldTextColor); SafeNativeMethods.SetBkColor(new HandleRef(g, hdc), oldBkColor); hfont = SafeNativeMethods.SelectObject(new HandleRef(g, hdc), new HandleRef(null, hfont)); g.ReleaseHdcInternal(hdc); } #if PBRS_PAINT_DEBUG rect.Width --; rect.Height--; g.DrawRectangle(new Pen(Color.Purple), rect); #endif if (doToolTip) { this.ValueToolTipLocation = new Point(rect.X+2, rect.Y-1); } else { this.ValueToolTipLocation = InvalidPoint; } } return; }