本文整理汇总了C#中System.Windows.Forms.RichTextBox.GetPositionFromCharIndex方法的典型用法代码示例。如果您正苦于以下问题:C# RichTextBox.GetPositionFromCharIndex方法的具体用法?C# RichTextBox.GetPositionFromCharIndex怎么用?C# RichTextBox.GetPositionFromCharIndex使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Windows.Forms.RichTextBox
的用法示例。
在下文中一共展示了RichTextBox.GetPositionFromCharIndex方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: BuildSyntaxView
/// <summary>
/// Uses Roslyn technology to build a syntax tree from, and acquire
/// diagnostics about, the source code.
/// Diagnostics are rendered into the rich text box. If the compiler
/// has highlighted a non-empty span then it is highlighted otherwise
/// a red X is display at the error pixel point of an empty span.
/// </summary>
/// <param name="tbSource">Text box with C# source code.</param>
/// <param name="richResult">Rich text box to render syntax highlighting into.</param>
/// <remarks>
/// Only the first source code syntax error is highlighted.
/// </remarks>
static void BuildSyntaxView(TextBox tbSource, RichTextBox richResult)
{
richResult.Text = tbSource.Text;
// Roslyn!! - SyntaxTree.*
var syntaxTree = SyntaxTree.ParseText(tbSource.Text);
var diags = syntaxTree.GetDiagnostics();
// Display all compiler diagnostics in console
Console.WriteLine("============================");
Console.WriteLine("Compiler has {0} diagnostic messages.", diags.Count());
// Roslyn again!!
foreach (var d in diags)
Console.WriteLine("> {0}", d.Info.GetMessage());
Console.WriteLine("-------------------------");
List<Point> issuePoints = new List<Point>();
picX.Visible = false;
foreach (var d in diags)
{
// More Roslyn !! - d.*
if (d.Location.IsInSource)
{
var origFore = Console.ForegroundColor;
var origBack = Console.BackgroundColor;
Console.BackgroundColor = ConsoleColor.Red;
Console.ForegroundColor = ConsoleColor.Black;
Console.Write(tbSource.Text.Substring(d.Location.SourceSpan.Start, d.Location.SourceSpan.Length));
// Hey Roslyn !! - d.*
int lineEndOffset = d.Location.GetLineSpan(true).StartLinePosition.Line;
// Note: line endings in rich text box use one character where textbox input source uses two
richResult.Select(d.Location.SourceSpan.Start - lineEndOffset, d.Location.SourceSpan.Length);
richResult.SelectionColor = Color.Yellow;
richResult.SelectionBackColor = Color.Red;
if (d.Location.SourceSpan.Length == 0)
issuePoints.Add(richResult.GetPositionFromCharIndex(d.Location.SourceSpan.Start));
Console.BackgroundColor = origBack;
Console.ForegroundColor = origFore;
Console.WriteLine(tbSource.Text.Substring(d.Location.SourceSpan.Start + d.Location.SourceSpan.Length));
break; // Stop after first error is highlighted
// No dealing with multiple errors or overlaps in this sample.
}
}
// Put the X on the first error
if (issuePoints.Count != 0)
{
picX.Location = issuePoints[0];
picX.Visible = true;
}
Console.WriteLine("============================");
}
示例2: GetCharWidthInTwips
int GetCharWidthInTwips()
{
using (RichTextBox rtb = new RichTextBox())
{
rtb.Font = Font;
rtb.WordWrap = false;
rtb.Text = "XX";
Point p0 = rtb.GetPositionFromCharIndex(0);
Point p1 = rtb.GetPositionFromCharIndex(1);
using (Graphics g = rtb.CreateGraphics())
return (p1.X - p0.X) * 72 * 20 / Convert.ToInt32(g.DpiX);
}
}
示例3: GetCorrection
private static int GetCorrection(RichTextBox e, int index)
{
Point pt1 = Point.Empty;
GetCaretPos(ref pt1);
Point pt2 = e.GetPositionFromCharIndex(index);
// ocotber 23 2008 - not sure what this function
// was meant to do so added this code to catch null situations
if (pt1 == null || pt2 == null)
{
return 0;
}
if (pt1 != pt2)
return 1;
else
return 0;
}
示例4: Column
public static int Column(RichTextBox e, int index1)
{
int correction = GetCorrection(e, index1);
Point p = e.GetPositionFromCharIndex(index1 - correction);
if (p.X == 1)
return 1;
p.X = 0;
int index2 = e.GetCharIndexFromPosition(p);
int col = index1 - index2 + 1;
return col;
}
示例5: GetClippingRectangle
private Rectangle GetClippingRectangle(RichTextBox rtbText)
{
int index = rtbText.GetFirstCharIndexOfCurrentLine();
Point point = rtbText.GetPositionFromCharIndex(index);
return new Rectangle(point, new Size(200, 119));
}
示例6: DrawRTFstring
///<summary>We need this special function to draw strings just like the RichTextBox control does, because sheet text is displayed using RichTextBoxes within FormSheetFillEdit.
///Graphics.DrawString() uses a different font spacing than the RichTextBox control does.</summary>
private void DrawRTFstring(int index,string str,Font font,Brush brush,Graphics g) {
str=str.Replace("\r","");//For some reason '\r' throws off character position calculations. \n still handles the CRs.
SheetFieldDef field=SheetDefCur.SheetFieldDefs[index];
//Font spacing is different for g.DrawString() as compared to RichTextBox and TextBox controls.
//We create a RichTextBox here in the same manner as in FormSheetFillEdit, but we only use it to determine where to draw text.
//We do not add the RichTextBox control to this form, because its background will overwrite everything behind that we have already drawn.
bool doCalc=true;
object[] data=(object[])HashRtfStringCache[index.ToString()];
if(data!=null) {//That field has been calculated
//If any of the following factors change, then that could potentially change text positions.
if(field.FontName.CompareTo(data[1])==0//Has font name changed since last pass?
&& field.FontSize.CompareTo(data[2])==0//Has font size changed since last pass?
&& field.FontIsBold.CompareTo(data[3])==0//Has font boldness changed since last pass?
&& field.Width.CompareTo(data[4])==0//Has field width changed since last pass?
&& field.Height.CompareTo(data[5])==0//Has field height changed since last pass?
&& str.CompareTo(data[6])==0//Has field text changed since last pass?
&& field.TextAlign.CompareTo(data[7])==0)//Has field text align changed since last pass?
{
doCalc=false;//Nothing has changed. Do not recalculate.
}
}
if(doCalc) { //Data has not yet been cached for this text field, or the field has changed and needs to be recalculated.
//All of these textbox fields are set using the same logic as in FormSheetFillEdit, so that text in this form matches exaclty.
RichTextBox textbox=new RichTextBox();
textbox.Visible=false;
textbox.BorderStyle=BorderStyle.None;
textbox.ScrollBars=RichTextBoxScrollBars.None;
textbox.SelectionAlignment=field.TextAlign;
textbox.Location=new Point(field.XPos,field.YPos);
textbox.Width=field.Width;
textbox.Height=field.Height;
textbox.Font=font;
textbox.ForeColor=((SolidBrush)brush).Color;
if(field.Height<textbox.Font.Height+2) {//Same logic as FormSheetFillEdit.
textbox.Multiline=false;
}
else {
textbox.Multiline=true;
}
textbox.Text=str;
Point[] positions=new Point[str.Length];
for(int j=0;j<str.Length;j++) {
positions[j]=textbox.GetPositionFromCharIndex(j);//This line is slow, so we try to minimize calling it by chaching positions each time there are changes.
}
textbox.Dispose();
data=new object[] { positions,field.FontName,field.FontSize,field.FontIsBold,field.Width,field.Height,str,field.TextAlign };
HashRtfStringCache[index.ToString()]=data;
}
Point[] charPositions=(Point[])data[0];
for(int j=0;j<charPositions.Length;j++) { //This will draw text below the bottom line if the text is long. This is by design, so the user can see that the text is too big.
g.DrawString(str.Substring(j,1),font,brush,field.Bounds.X+charPositions[j].X,field.Bounds.Y+charPositions[j].Y);
}
}
示例7: PaintNoSpaceAfterPeriod
void PaintNoSpaceAfterPeriod(PaintEventArgs e, int Start, int End, RichTextBox RichText)
{
Graphics g;
g = RichText.CreateGraphics ();
//Pen myPen = null;// new Pen (Color.FromArgb (60, Color.Yellow)); // Alpha did not seem to work this way
// this gets tricky. The loop is just to find all the [[~scenes]] on the note
// even if offscreen.
// OK: but with th eoptimization to only show on screen stuff, do we need this anymore???
// august 10 - settting it back
// now trying regex
System.Text.RegularExpressions.Regex regex =
new System.Text.RegularExpressions.Regex ("\\.\\w|\\. \\w|\\?\\w|\\? \\w|\\!\\w|\\! \\w|\\;\\w|\\; \\w|\\:\\w|\\: \\w|\\,\\w|\\, \\w",
System.Text.RegularExpressions.RegexOptions.IgnoreCase | RegexOptions.Compiled|
System.Text.RegularExpressions.RegexOptions.Multiline);
System.Text.RegularExpressions.MatchCollection matches = regex.Matches (RichText.Text, Start);
foreach (System.Text.RegularExpressions.Match match in matches) {
if (match.Index > End)
break; // we exit if already at end
Point pos = RichText.GetPositionFromCharIndex (match.Index);
if (pos.X > -1 && pos.Y > -1) {
int testpos = match.Index + 2;
Color colorToUse = Color.FromArgb(175, Color.Red);
// default is [[facts]] and stuff
// pos = CoreUtilities.General.BuildLocationForOverlayText (pos, DockStyle.Bottom, " ");
// System.Drawing.SolidBrush brush1 = new System.Drawing.SolidBrush( colorToUse);
System.Drawing.Pen pen1 = new Pen(colorToUse);
//myPen.Width = 1;
//myPen.Color = Color.Red;
// int scaler = BuildScaler(RichText.ZoomFactor);
Rectangle rec = GetRectangleForSmallRectangle(g, pos, RichText, match.Index, match.Value, true);// new Rectangle (new Point (pos.X+(int)(scaler*1.5), pos.Y -(5+scaler)), new Size ((int)(scaler * 1.5), (int)(scaler * 1.5)));
//Rectangle rec = new Rectangle (new Point (pos.X+scaler, pos.Y-15), new Size ((int)(scaler * 1.5), (int)(scaler * 0.75)));
// Rectangle rec2 = new Rectangle (new Point (pos.X+20, pos.Y - 25), new Size ((int)(scaler * 1), (int)(scaler * 0.65)));
//g.DrawLine(myPen, pos.X, pos.Y -10, pos.X + 50, pos.Y-10);
//g.FillRectangle (brush1, rec);
//g.FillEllipse(brush1, rec);
rec.Height = 1;
g.DrawRectangle(pen1, rec);
// g.FillEllipse(brush1, rec2);
}
/*
locationInText = locationInText + sParam.Length;
if (locationInText > end)
{
// don't search past visible end
pos = emptyPoint;
}
else
pos = GetPositionForFoundText(sParam, ref locationInText);*/
}
// regex matches
g.Dispose ();
//myPen.Dispose ();
}
示例8: PaintHeadings
void PaintHeadings(PaintEventArgs e, int Start, int End, RichTextBox RichText, System.Text.RegularExpressions.Regex regex2, Color color, string text, Font f)
{
//string exp = ".*\\=([^)]+?)\\=";
// this one was fast but inaccurate (very)
//string exp = String.Format (".*?\\=\\w([^)]+?)\\w\\={0}?", Environment.NewLine); // added lazy operations to attempt a speed improvement
Graphics g = e.Graphics;//RichText.CreateGraphics ();
System.Drawing.Pen pen1 = new Pen(color);
System.Text.RegularExpressions.MatchCollection matches = regex2.Matches (RichText.Text, Start);
foreach (System.Text.RegularExpressions.Match match in matches) {
if (match.Index > End)
break; // we exit if already at end
Point pos = RichText.GetPositionFromCharIndex (match.Index);
if (pos.X > -1 && pos.Y > -1) {
pen1.Width = 8;
string stext = text+match.Value+text;
//pos = CoreUtilities.General.BuildLocationForOverlayText (pos, DockStyle.Right,match.Value );
int newWidth = Convert.ToInt32 (g.MeasureString (stext, f).Width * RichText.ZoomFactor);
pos = new Point(pos.X + (newWidth), pos.Y + 10);
g.DrawLine (pen1, pos.X, pos.Y, pos.X + 500, pos.Y);
// Rectangle rec = GetRectangleForSmallRectangle(g, pos, RichText, match.Index, match.Value, true);// new Rectangle (new Point (pos.X+(int)(scaler*1.5), pos.Y -(5+scaler)), new Size ((int)(scaler * 1.5), (int)(scaler * 1.5)));
// rec.Height = 2;
//
// g.DrawRectangle(pen1, rec);
}
}
// g.Dispose (); don't dispose what we are borrowing
}
示例9: DoPaint
public void DoPaint(PaintEventArgs e, int Start, int End, RichTextBox RichText)
{
try {
PaintDoubleSpaces(e, Start, End, RichText);
PaintNoSpaceAfterPeriod(e, Start, End, RichText);
PaintLink(e, Start, End, RichText);
bool doheadings = true;
if (doheadings)
{
Font f = new Font(RichText.Font.FontFamily, RichText.Font.Size, RichText.Font.Style, GraphicsUnit.Pixel, Convert.ToByte(0), false);
Color newColor = TextUtils.InvertColor(RichText.BackColor);
Color colorToUse = Color.FromArgb(175, ControlPaint.Dark(newColor));
PaintHeadings(e, Start, End, RichText, Mainheading, colorToUse,"=",f);
colorToUse = Color.FromArgb(175,ControlPaint.Light(newColor));
PaintHeadings(e, Start, End, RichText, Mainheading2, colorToUse,"==",f);
colorToUse = Color.FromArgb(175, ControlPaint.LightLight(newColor));
PaintHeadings(e, Start, End, RichText, Mainheading3, colorToUse,"===",f);
}
// int locationInText = GetCharIndexFromPosition(new Point(0, 0));
string sParam = "[[~scene]]";
// assuming function only being used for [[~scene]] delimiters
// I've moved this position up to core function as an optimization
// instead of being used in each call to GetPositionForFoundText
// int start =
// if (locationInText < start)
// {
// locationInText = start;
// }
// The loop is what makes this
// Point pos = GetPositionForFoundText(sParam, ref locationInText);
Graphics g;
//g = RichText.CreateGraphics ();
g = e.Graphics;
Pen myPen = new Pen (Color.FromArgb (255, ControlPaint.LightLight (TextUtils.InvertColor(RichText.BackColor))));
// this gets tricky. The loop is just to find all the [[~scenes]] on the note
// even if offscreen.
// OK: but with th eoptimization to only show on screen stuff, do we need this anymore???
// august 10 - settting it back
// now trying regex
// This worked for = match but it was WAAY too slow
// System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex (".*\\=([^)]+)\\=",
// System.Text.RegularExpressions.RegexOptions.IgnoreCase);
// this works just experimenting
System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex ("\\[\\[(.*?)\\]\\]",
System.Text.RegularExpressions.RegexOptions.IgnoreCase |
System.Text.RegularExpressions.RegexOptions.Multiline);
System.Text.RegularExpressions.MatchCollection matches = regex.Matches (RichText.Text, Start);
foreach (System.Text.RegularExpressions.Match match in matches) {
if (match.Index > End)
break; // we exit if already at end
Point pos = RichText.GetPositionFromCharIndex (match.Index);
if (pos.X > -1 && pos.Y > -1) {
// but we only draw the line IF we are onscreen
//if (pos.X > -1 && pos.Y > -1)
//if (sParam == "[[~scene]]")
//test instead to see if pos + 2 is a ~
int testpos = match.Index + 2;
/// November 2012 - only want this to appear for ~scene
if (RichText.Text.Length > (testpos + 1) && RichText.Text [testpos] == '~' && RichText.Text [testpos + 1] == 's') {
if (g != null) {
//myPen.Color = Color.Yellow;
// myPen = new Pen (Color.FromArgb (225, Color.Yellow));
myPen.Width = 8;
pos = CoreUtilities.General.BuildLocationForOverlayText (pos, DockStyle.Right, sParam);
g.DrawLine (myPen, pos.X, pos.Y, pos.X + 500, pos.Y);
}
} else {
Color colorToUse = Color.FromArgb (255, ControlPaint.LightLight (TextUtils.InvertColor(RichText.BackColor)));//Color.FromArgb(255, Color.Green);
// November 2012 - testing to see if there's a period right before which means it will count)
if (match.Index > 0) {
if ('.' == RichText.Text [match.Index - 1]) {
// we have a period so the code .[[f]] will not do anything
// show in a different color
colorToUse = Color.Red;// Alpha worked but because not all areas are redrawn at same time did not look right (May 2013) Color.FromArgb(255, Color.Red);
}
}
// default is [[facts]] and stuff
//pos = CoreUtilities.General.BuildLocationForOverlayText (pos, DockStyle.Bottom, sParam);
// if (e.Graphics. .Contains(pos))
//.........这里部分代码省略.........
示例10: RebuildBottomFxPanelContent
private void RebuildBottomFxPanelContent()
{
_fxArgumentsPanel.Controls.Clear();
_noParamsLabel.Visible = _selectedSub.Children.Count() == 0;
if (_selectedSub.Children.Count() == 0)
{
_fxArgumentsPanel.Controls.Add(_noParamsLabel);
}
else
{
_bottomPanels = new List<Panel>();
var labels = new List<Label>();
_selectedSub.Children.Cast<Expression>().ForEach((e, i) =>
{
var index = String.Format(Resources.ParameterFormat, i + 1);
var text = e.RenderPublicText(_rctx);
var p = new Panel();
p.Padding = new Padding(3, 3, 3, 3);
var l = new Label();
l.Name = "lbot " + i;
l.Width = 57;
l.Text = index;
l.Dock = DockStyle.Left;
// l.Padding = new Padding(3, 3, 0, 0);
l.AutoSize = false;
l.DoubleClick += (o, args) => SelectedSub = e;
l.KeyDown += (o, args) => { if (args.KeyCode == Keys.Enter) { SelectedSub = e; } };
labels.Add(l);
var t = new RichTextBox();
t.ReadOnly = true;
t.Name = "tbot " + i;
t.Dock = DockStyle.Fill;
t.Height = 25;
t.WordWrap = true;
t.AutoSize = true;
t.Text = text;
t.BorderStyle = BorderStyle.None;
t.DoubleClick += (o, args) => SelectedSub = e;
t.Select(0, 0);
t.Width = this.Width - 25; // scrollbar
var pt = t.GetPositionFromCharIndex(text.Length - 1);
p.Controls.Add(t);
p.Controls.Add(l);
p.Dock = DockStyle.Top;
p.Height = pt.Y + 30 + 5;
_bottomPanels.Add(p);
});
((IEnumerable<Panel>)_bottomPanels).Reverse().ForEach(p => _fxArgumentsPanel.Controls.Add(p));
var width = labels.Count == 0 ? 0 : labels.Max(l => l.PreferredWidth);
labels.ForEach(l => { l.AutoSize = false; l.Width = width; });
}
RecalculateTotalHeight();
}
示例11: RebuildTopPanelContent
private void RebuildTopPanelContent()
{
_elfCodePanel.Controls.Clear();
_lineOfCodeButton.DropDownItems.Clear();
var ddr = _lineOfCodeButton.DropDownItems.Add(Resources.LineOfCode_NoCodeSelected);
ddr.Click += (o, args) => SelectedLine = null;
_topPanels = new List<Panel>();
var labels = new List<Label>();
Lines.ForEach((e, i) =>
{
var index = (i + 1).ToString(new string('0', (_root.Children.Count() - 1).ToString().Length)) + ":";
var text = e.RenderPublicText(_rctx);
var p = new Panel();
p.Padding = new Padding(3, 3, 3, 3);
var l = new Label();
l.Name = "ltop " + i;
l.Text = index;
l.Dock = DockStyle.Left;
// l.Padding = new Padding(3, 3, 0, 0);
l.DoubleClick += (o, args) => { SelectedLineIndex = i; };
l.KeyDown += (o, args) => { if (args.KeyCode == Keys.Enter) { SelectedLineIndex = i; } };
labels.Add(l);
var t = new RichTextBox();
t.ReadOnly = true;
t.Name = "ttop " + i;
t.Dock = DockStyle.Fill;
t.WordWrap = true;
t.AutoSize = true;
t.BorderStyle = BorderStyle.None;
t.Text = text;
t.DoubleClick += (o, args) => { SelectedLineIndex = i; };
t.Select(0, 0);
t.Width = this.Width - 25; // scrollbar
var pt = t.GetPositionFromCharIndex(text.Length - 1);
p.Controls.Add(t);
p.Controls.Add(l);
p.Dock = DockStyle.Top;
p.Height = pt.Y + 30 + 5;
_topPanels.Add(p);
var ddi = _lineOfCodeButton.DropDownItems.Add(index + " " + text);
ddi.Click += (o, args) => SelectedLine = e;
});
((IEnumerable<Panel>)_topPanels).Reverse().ForEach(p => _elfCodePanel.Controls.Add(p));
RecalculateTotalHeight();
var width = labels.Count == 0 ? 0 : labels.Max(l => l.PreferredWidth);
labels.ForEach(l => { l.AutoSize = false; l.Width = width + 2; }); // 2 extra pixels for bold
}
示例12: GetCurrentPos
public static void GetCurrentPos(RichTextBox rtfMain, ref int line, ref int col)
{
Point pt;
int index;
index = rtfMain.SelectionStart;
line = rtfMain.GetLineFromCharIndex(index);
pt = rtfMain.GetPositionFromCharIndex(index);
pt.X = 0;
col = index - rtfMain.GetCharIndexFromPosition(pt);
line ++;
col ++;
return;
}