/// <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();
        }
Example #2
0
        public static void TextBoxSelectionChanged(TextBoxBase textbox, Action <RowColumnIndex> notificationAct)
        {
            if (null == textbox)
            {
                return;
            }

            var line   = textbox.GetLineFromCharIndex(textbox.SelectionStart);
            var column = textbox.SelectionStart - textbox.GetFirstCharIndexOfCurrentLine();

            //Debug.WriteLine("Line: {0}, Column: {1}", line, column);

            RowColumnIndex rci = new RowColumnIndex()
            {
                Row             = line + 1,
                Column          = column + 1,
                SelectionLength = textbox.SelectionLength,
                Length          = textbox.TextLength,
            };

            if (notificationAct != null)
            {
                notificationAct(rci);
            }
        }
Example #3
0
        public static void GetLineAndPosFromIndex(this TextBoxBase self, int index, out int line, out int pos)
        {
            line = self.GetLineFromCharIndex(index);
            int linepos = self.GetFirstCharIndexFromLine(line);

            pos = index - linepos;
        }
Example #4
0
        public static string GetCurrentLineData(this TextBoxBase textbox)
        {
            int pos  = textbox.SelectionStart;
            int line = textbox.GetLineFromCharIndex(pos);

            if (line >= 0 && line < textbox.Lines.Length)
            {
                return(textbox.Lines[line]);
            }
            return(null);
        }
Example #5
0
        public static void FastScrollToCaret(this TextBoxBase control)
        {
            if (control.IsHandleCreated)
            {
                var textLength = control.TextLength;

                if (textLength == 0)
                {
                    return;
                }
                bool   flag       = false;
                object iunknown   = null;
                var    iunkHandle = IntPtr.Zero;
                try
                {
                    if (SendMessage(new HandleRef(control, control.Handle), 1084, 0, out iunknown) != 0)
                    {
                        iunkHandle = Marshal.GetIUnknownForObject(iunknown);
                        if (iunkHandle != IntPtr.Zero)
                        {
                            var itextDocumentHandle = IntPtr.Zero;
                            var gUID = typeof(ITextDocument).GUID;
                            try
                            {
                                Marshal.QueryInterface(iunkHandle, ref gUID, out itextDocumentHandle);
                                var textDocument = Marshal.GetObjectForIUnknown(itextDocumentHandle) as ITextDocument;
                                if (textDocument != null)
                                {
                                    int start             = control.SelectionStart;
                                    int lineFromCharIndex = control.GetLineFromCharIndex(start);
                                    var textRange         = textDocument.Range(start, start + control.SelectionLength);
                                    textRange.ScrollIntoView(0);                                     // scroll to start of selection
                                    int num3 = (int)Win32.SendMessage(control.Handle, (Win32.WM) 206, IntPtr.Zero, IntPtr.Zero);
                                    if (num3 > lineFromCharIndex)
                                    {
                                        textRange.ScrollIntoView(32);
                                    }
                                    flag = true;
                                }
                            }
                            finally
                            {
                                if (itextDocumentHandle != IntPtr.Zero)
                                {
                                    Marshal.Release(itextDocumentHandle);
                                }
                            }
                        }
                    }
                }
                finally
                {
                    if (iunkHandle != IntPtr.Zero)
                    {
                        Marshal.Release(iunkHandle);
                    }
                }
                if (!flag)
                {
                    Win32.SendMessage(control.Handle, (Win32.WM) 183, IntPtr.Zero, IntPtr.Zero);
                    return;
                }
            }
            else
            {
                control.ScrollToCaret();
            }
        }
Example #6
0
 public static int CurrentLineIndex(this TextBoxBase textBox) => textBox.GetLineFromCharIndex(textBox.SelectionStart);