/// <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();
        }
Beispiel #2
0
 /// <summary>
 /// Позиция вертикального скролла
 /// </summary>
 internal static int GetVScrollPos(TextBoxBase richTextBox)
 {
     Win32.SCROLLINFO info = VScrollInfo(richTextBox, Win32.SIF_ALL);
     if (info.nTrackPos == 0)
     {
         if (richTextBox.GetCharIndexFromPosition(new Point(2, 2)) == 0)
         {
             return(0);
         }
         return(info.nPos);
     }
     return(info.nTrackPos);
 }
        /// <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);
        }
        /// <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);
        }
Beispiel #5
0
            public ICollection <HotSpotInternal> GetHotSpotsFromPosition(TextBoxBase textBox, Point position)
            {
                int offset = textBox.GetCharIndexFromPosition(position);

                return(GetHotSpotsFromOffset(offset));
            }
Beispiel #6
0
 private void textBox_MouseWheel(object sender, MouseEventArgs args)
 {
     _scrollTextBox = _textBox.TextLength == _textBox.GetCharIndexFromPosition(new System.Drawing.Point(_textBox.ClientRectangle.Right, _textBox.ClientRectangle.Bottom)) + 1;
 }