private unsafe void PaintColoredText(char[] chars, byte[] colors, IntPtr hdc, int rectLeft, int rectRight, int yPos, int startIndex, int endIndex, bool selected) { if ((chars == null) || (chars.Length <= 0)) { this.ColorTable[0].SetupHdc(hdc); Interop.RECT lpRect = new Interop.RECT(rectLeft, yPos, rectRight, yPos + this._fontHeight); Interop.ExtTextOutW(hdc, 0, 0, 2, ref lpRect, IntPtr.Zero, 0, null); } else { // 清空行末空间颜色 if ((endIndex - startIndex) >= 0) { Interop.RECT rect = new Interop.RECT(rectLeft + ((endIndex - startIndex) * this._fontWidth), yPos, rectRight, yPos + this._fontHeight); this.ColorTable[colors[chars.Length]].SetupHdc(hdc); Interop.ExtTextOutW(hdc, 0, 0, 2, ref rect, IntPtr.Zero, 0, null); } // 开始把字符串画到控件上 Interop.RECT textRect = new Interop.RECT(rectLeft, yPos, rectLeft, yPos + this._fontHeight); int startColumnIndex = startIndex; int curColumnIndex = startColumnIndex; int count = 0; while (startColumnIndex < endIndex) { int colorIndex = colors[startColumnIndex]; while ((curColumnIndex < endIndex) && (colors[curColumnIndex] == colorIndex)) { curColumnIndex++; } IntPtr zero = IntPtr.Zero; try { ColorInfo info2 = this.ColorTable[colorIndex]; ColorInfo info3 = info2; if (selected) { if (this._owner.ContainsFocus && (this._owner.ActiveView == this)) { info2 = this.ColorTable[5]; } else { info2 = this.ColorTable[6]; } } if (info3.IsStyledFont) { zero = info2.SetupHdc(hdc, this.GetStyledFont(info3.Bold, info3.Italic)); } else { info2.SetupHdc(hdc); } count = curColumnIndex - startColumnIndex; string text = new string(chars, startColumnIndex, count); Interop.SIZE size = new Interop.SIZE(); bool ret = Interop.GetTextExtentPoint32W(hdc, text, count, ref size); textRect.left = textRect.right; textRect.right += size.x ; fixed (char* chRef = chars) { char* chPtr = chRef; chPtr += startColumnIndex; Interop.ExtTextOutW(hdc, textRect.left, yPos, 2, ref textRect, (IntPtr) chPtr, count, null); } continue; } finally { startColumnIndex = curColumnIndex; if (zero != IntPtr.Zero) { Interop.SelectObject(hdc, zero); } } } } }
private Interop.SIZE MeasureString(IntPtr hdc, char[] chars, int startIndex, int length) { Interop.SIZE size = new Interop.SIZE(); if (length > 0) { string text = new string(chars, startIndex, length); bool ret = Interop.GetTextExtentPoint32W(hdc, text, length, ref size); } return size; }
internal void UpdateFont() { this._fontPtr = this.Font.ToHfont(); this._styledFontPtrs = null; Graphics graphics = Graphics.FromHwnd(IntPtr.Zero); IntPtr hdc = graphics.GetHdc(); Interop.SelectObject(hdc, this._fontPtr); Interop.SIZE size = new Interop.SIZE(); Interop.GetTextExtentPoint32W(hdc, "O", 1, ref size); this._fontWidth = size.x; this._fontHeight = size.y; graphics.ReleaseHdc(hdc); if (this.LineNumbersVisible) { this._lineNumbersWidth = (this._fontWidth * 5) + 3; } this.UpdateLayout(); if (base.IsHandleCreated) { bool flag = !this._caretHiding; this.HideCaret(); if (flag) { Interop.DestroyCaret(); this.UpdateCaret(); } } base.Invalidate(); }
private Interop.SIZE MeasureString(char[] chars, int startIndex, int length) { //NOTE: 使用这种方法的话, 反复选中文本会导致程序崩溃 //IntPtr fontPtr = this.Font.ToHfont(); //Graphics graphics = Graphics.FromHwnd(IntPtr.Zero); //IntPtr hdc = graphics.GetHdc(); //Interop.SelectObject(hdc, fontPtr); //Interop.SIZE size = this.MeasureString(hdc, chars, startIndex, length); //Interop.SelectObject(hdc, IntPtr.Zero); //graphics.ReleaseHdc(hdc); //graphics.Dispose(); //return size; //直接使用Graphics对象的MeasureString就没问题, 但是我觉得效率可能会比较低, 哪位童鞋比较了解这方面的请赐教!! Graphics g = this.CreateGraphics(); SizeF s = g.MeasureString(new string(chars, startIndex, length), this.Font); g.Dispose(); Interop.SIZE size = new Interop.SIZE(); size.x = (int)s.Width; size.y = (int)s.Height; return size; }
protected override Size Measure(ElementRenderData renderData) { string text = this.Text; Size empty = Size.Empty; if (text.Length != 0) { if (renderData.AntiAliasedText) { return renderData.Graphics.MeasureString(this.Text, renderData.Font, new PointF(0f, 0f), renderData.TextFormat).ToSize(); } IntPtr graphicsHandle = renderData.GraphicsHandle; IntPtr hObject = Interop.SelectObject(graphicsHandle, renderData.FontHandle); try { Interop.SIZE size = new Interop.SIZE(); Interop.GetTextExtentPoint32W(graphicsHandle, text, text.Length, ref size); empty = new Size(size.x, size.y); } finally { Interop.SelectObject(graphicsHandle, hObject); } } return empty; }