/// <summary> /// 绘制行号 /// </summary> /// <param name="richbox"></param> /// <param name="component"></param> public static void PaintLineNo(TextBoxBase richbox, Panel component) { Point point = richbox.Location; int firstIndex = richbox.GetCharIndexFromPosition(point); int firstLine = richbox.GetLineFromCharIndex(firstIndex); Point firstPosition = richbox.GetPositionFromCharIndex(firstIndex); point.Y += richbox.Height; int lastIndex = richbox.GetCharIndexFromPosition(point); int lastLine = richbox.GetLineFromCharIndex(lastIndex); Point lastPosition = richbox.GetPositionFromCharIndex(lastIndex); Graphics g = component.CreateGraphics(); Font font = new Font("微软雅黑", 9F, FontStyle.Regular, GraphicsUnit.Point, ((byte)(134))); SolidBrush brush = new SolidBrush(Color.FromArgb(15, 180, 148)); Rectangle rect = component.ClientRectangle; brush.Color = component.BackColor; g.FillRectangle(brush, 0, 0, component.ClientRectangle.Width, component.ClientRectangle.Height); brush.Color = Color.FromArgb(15, 180, 148); int lineSpace = 0; if (firstLine != lastLine) { lineSpace = (lastPosition.Y - firstPosition.Y) / (lastLine - firstLine); } else { lineSpace = (int)(Convert.ToInt32(richbox.Font.Size) * 1.6); } int brushX = component.ClientRectangle.Width - Convert.ToInt32(font.Size * 3.5); int brushY = lastPosition.Y + Convert.ToInt32(font.Size * 0.21f); int maxNum = 6; int charWidth = 7; int startWidth = 20; int noLength = 0; for (int i = lastLine; i >= 0; i--) { noLength = (i + 1).ToString().Length; if (noLength <= maxNum) { g.DrawString((i + 1).ToString(), font, brush, brushX + (startWidth - (noLength - 1) * charWidth), brushY); } else { g.DrawString((i + 1).ToString(), font, brush, brushX, brushY); } brushY -= lineSpace; } g.Dispose(); font.Dispose(); brush.Dispose(); }
/// <summary> /// Sets the location of the AutoComplete form, maiking sure it's on the screen where the cursor is. /// </summary> /// <param name="moveHorizontly">determines wheather or not to move the form horizontly.</param> private void SetAutoCompleteLocation(TextBoxBase control, bool moveHorizontly) { Point cursorLocation = control.GetPositionFromCharIndex(control.SelectionStart); Screen screen = Screen.FromPoint(cursorLocation); Point optimalLocation = new Point(PointToScreen(cursorLocation).X - 15, (int)(PointToScreen(cursorLocation).Y + Font.Size * 2 + 2)); Rectangle desiredPlace = new Rectangle(optimalLocation, this.Size); desiredPlace.Width = 152; if (desiredPlace.Left < screen.Bounds.Left) { desiredPlace.X = screen.Bounds.Left; } if (desiredPlace.Right > screen.Bounds.Right) { desiredPlace.X -= (desiredPlace.Right - screen.Bounds.Right); } if (desiredPlace.Bottom > screen.Bounds.Bottom) { desiredPlace.Y = cursorLocation.Y - 2 - desiredPlace.Height; } if (!moveHorizontly) { desiredPlace.X = this.Left; } this.Bounds = desiredPlace; }
private static Point GetPositionFromCharIndex(TextBoxBase textBox, int offset) { Point position = textBox.GetPositionFromCharIndex(offset); // the following is necessary to work around a .Net bug int x = (Int16)position.X; int y = (Int16)position.Y; return(new Point(x, y)); }
private System.Drawing.Point GetCurrentCoordinates(TextBoxBase control) { // note, get rid of the "+1" if you want the coordinates to be zero based //Point p = new System.Drawing.Point(); //p.Y = (rtb.GetLineFromCharIndex(rtb.SelectionStart)) + 1; //p.X = (rtb.SelectionStart - rtb.GetFirstCharIndexOfCurrentLine()) + 1; Point point = control.GetPositionFromCharIndex(control.SelectionStart); // FIX for textbox if (point.X == 0 && point.Y == 0) { if (control.SelectionStart > 0) { point = control.GetPositionFromCharIndex(control.SelectionStart - 1); } } return(point); }
/// <summary> /// Ermittelt die Position des Carets für den angegebenen Punkt /// </summary> /// <param name="textBox">das Eingabefeld, dessen Caret-Position ermittelt werden soll</param> /// <param name="point">der Punkt, für den die Caret-Position ermittelt werden soll</param> /// <returns></returns> public static int GetCaretIndexFromPoint(this TextBoxBase textBox, Point point) { Point actualPoint = textBox.PointToClient(point); int index = textBox.GetCharIndexFromPosition(actualPoint); if (index == textBox.Text.Length - 1) { Point caretPoint = textBox.GetPositionFromCharIndex(index); if (actualPoint.X > caretPoint.X) { index += 1; } } return(index); }
private Rectangle GetHotAreaBounds(IDeviceContext g, HotSpot area) { //GetPositionFromCharIndex give us the point at the top // left corner of this word. (see documentation on EM_POSFROMCHAR) Point start = _control.GetPositionFromCharIndex(area.Offset); // the following is necessary to work around a .Net bug int startX = (Int16)start.X; int startY = (Int16)start.Y; Size textSize = TextRenderer.MeasureText(g, area.Text, _control.Font, _control.ClientSize, TextFormatFlags.TextBoxControl | TextFormatFlags.NoPadding | TextFormatFlags.WordBreak); return(new Rectangle(startX, startY, textSize.Width, textSize.Height)); }
/// <summary> /// Gets the ranges of chars that represent the spelling errors and then draw a wavy red line underneath /// them. /// </summary> /// <remarks></remarks> //ByVal sender As Object, ByVal e As DoWorkEventArgs) private void CustomPaint() { CharacterRange[] errorRanges = mySpeller.GetSpellingErrorRanges; if (errorRanges.Length == 0) { return; } RichTextBox tempRTB = null; if (clientTextBox is RichTextBox) { tempRTB = new RichTextBox(); tempRTB.Rtf = ((RichTextBox)clientTextBox).Rtf; } //Clear the graphics buffer bufferGraphics.Clear(Color.Transparent); //First get the ranges of characters visible in the textbox Point startPoint = new Point(0, 0); Point endPoint = new Point(clientTextBox.ClientRectangle.Width, clientTextBox.ClientRectangle.Height); long startIndex = clientTextBox.GetCharIndexFromPosition(startPoint); long endIndex = clientTextBox.GetCharIndexFromPosition(endPoint); // Create the end points to call the drawWave foreach (CharacterRange currentRange in errorRanges) { //Get the X, Y of the start and end characters startPoint = clientTextBox.GetPositionFromCharIndex(currentRange.First); endPoint = clientTextBox.GetPositionFromCharIndex(currentRange.First + currentRange.Length - 1); if (startPoint.Y != endPoint.Y) { //We have a word on multiple lines int curIndex = 0; int startingIndex = 0; curIndex = currentRange.First; startingIndex = curIndex; GetNextLine: //Determine the first line of waves to draw while ((clientTextBox.GetPositionFromCharIndex(curIndex).Y == startPoint.Y) & (curIndex <= (currentRange.First + currentRange.Length - 1))) { curIndex++; } //Go back to the previous character curIndex--; endPoint = clientTextBox.GetPositionFromCharIndex(curIndex); Point offsets = GetOffsets(ref clientTextBox, startingIndex, curIndex, tempRTB); //If we're using a RichTextBox, we have to account for the zoom factor if (clientTextBox is RichTextBox) { offsets.Y = (int)(offsets.Y * ((RichTextBox)clientTextBox).ZoomFactor); } //Reset the starting and ending points to make sure we're underneath the word //(The measurestring adds some margin, so remove them) startPoint.Y += offsets.Y - 2; endPoint.Y += offsets.Y - 2; endPoint.X += offsets.X - 0; //Add a new wavy line using the starting and ending point DrawWave(startPoint, endPoint); startingIndex = curIndex + 1; curIndex++; startPoint = clientTextBox.GetPositionFromCharIndex(curIndex); if (curIndex <= (currentRange.First + currentRange.Length - 1)) { goto GetNextLine; } } else { Point offsets = GetOffsets(ref clientTextBox, currentRange.First, (currentRange.First + currentRange.Length - 1), tempRTB); //If we're using a RichTextBox, we have to account for the zoom factor if (clientTextBox is RichTextBox) { offsets.Y = (int)(offsets.Y * ((RichTextBox)clientTextBox).ZoomFactor); } //Reset the starting and ending points to make sure we're underneath the word //(The measurestring adds some margin, so remove them) startPoint.Y += offsets.Y - 2; endPoint.Y += offsets.Y - 2; endPoint.X += offsets.X - 4; //Add a new wavy line using the starting and ending point //If e.Cancel Then Return DrawWave(startPoint, endPoint); } } //We've drawn all of the wavy lines, so draw that image over the textbox textBoxGraphics.DrawImageUnscaled(myBitmap, 0, 0); }