Code Editor similiar to Visual Studio 2005..
I am designing a script editor in C#. For some reason, I just cannot figure out how to properly use the Rich Text Box control properly. I want my code editor to look similar to Visual Studio's code editor. I can do syntax highlighting, what I need to know is how to add line counting on the left side in the same control.
If I try to use a separate control to add line counts, the problem occurs with scrolling. Here is what I want:
http://mack.sidrahq.com/stuff/linecount.jpg.
All I basically want is the line counting, nothing else. I already wrote the code to count lines.. I just can't accomplish that one thing!
Thanks!
Re: Code Editor similiar to Visual Studio 2005..
So, does anyone have a good idea?
Re: Code Editor similiar to Visual Studio 2005..
tMack:
Well, 35 people have viewed your posting and no one has posted any possible solution, so I guess we don't know.
However, I have a real long shot that might work. I can't tell you exactly how to make it work, but maybe you can figure it out or someone else can tell you.
MSWord has a template that is used when you want to write legal pleading. This template has the numbers down the left side and you can set the numbers to be whatever you want.
I don't know if the template would be in the MSWord Object or in a dll or where it would be, but if you could figure out how to hook into that template it should give you exactly what you need.
Maybe if you posted in the section for Microsoft Office someone could tell you how to hook up to it.
Hack seem to be the resident Office Expert.
Good Luck
Re: Code Editor similiar to Visual Studio 2005..
Sounds like a idea, but I found some code to synchronize the positions in a text box. It's VBNET though, so I have to convert it.. but if anyone has any more ideas hit me up.
Re: Code Editor similiar to Visual Studio 2005..
I while ago I have written an editor with line numbers in VB6 (and also assembler). This is how you do it.
First subclass the richtextbox window process and catch the WM_PAINT message
Code:
If uMsg = WM_PAINT Then
SendMessage hwnd, EM_GETRECT, 0, rt
rt.Left = 36
SendMessage hwnd, EM_SETRECT, 0, rt
DrawLineNumbers hwnd
End If
The DrawLineNumbers function is like this
Code:
Private Sub DrawLineNumbers(ByVal hWin As Long)
Dim lineno As Long
Dim linecount As Long
Dim buffer(32) As Byte
Dim hdc As Long
Dim rt As RECT
Dim hRgn As Long
Dim hObjRgn As Long
Dim hObjFont As Long
Dim hObjStock As Long
Dim fheight As Long
GetUpdateRect hWin, rt, False
If rt.Left < 28 Then
rt.Right = 28
ValidateRect hWin, rt
End If
hdc = GetDC(hWin)
SetBkMode hdc, TRANSPARENT
hRgn = CreateRectRgn(rt.Left, rt.Top, rt.Right, rt.Bottom)
hObjRgn = SelectObject(hdc, hRgn)
hObjFont = SelectObject(hdc, hFont)
lineno = SendMessage(hWin, EM_GETFIRSTVISIBLELINE, 0, 0) + 1
GetClientRect hWin, rt
rt.Right = 28
hObjStock = GetStockObject(LTGRAY_BRUSH)
FillRect hdc, rt, hObjStock
linecount = SendMessage(hWin, EM_GETLINECOUNT, 0, 0)
Do While linecount >= lineno
fheight = DrawText(hdc, lineno, -1, rt, DT_RIGHT Or DT_TOP)
rt.Top = rt.Top + fheight
lineno = lineno + 1
Loop
SelectObject hdc, hObjFont
SelectObject hdc, hObjStock
ReleaseDC hWin, hdc
End Sub
In the Form load event I choose font and retrieved the font handle
Code:
Private Sub Form_Load()
Dim lfont As LOGFONT
Me.RichTextBox1.Font.Name = "Courier New"
Me.RichTextBox1.Font.Size = 10
hFont = CreateFontIndirect(lfont)
End Sub
This code will generate a gray field to the left with black numbers.
I would appreciate if you publish your result in C# code.
Re: Code Editor similiar to Visual Studio 2005..
I would look at the Code Editor component of SharpDevelop -- they seem to have this feature along with a lot of others.
http://community.sharpdevelop.net/ph.../original.aspx
Re: Code Editor similiar to Visual Studio 2005..
This works. If you want another color just change the brush. If you want a vertical line you must draw it.
Subclassed richtextbox window process
Code:
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_PAINT)
{
RECT rt = new RECT();
GCHandle gch = GCHandle.Alloc(rt);
SendMessage(this.Handle, EM_GETRECT, 0, (IntPtr)gch);
rt = (RECT)Marshal.PtrToStructure((IntPtr)gch, typeof(RECT));
rt.Left = 36;
Marshal.StructureToPtr(rt, (IntPtr)gch, false);
SendMessage(this.Handle, EM_SETRECT, 0, (IntPtr)gch);
gch.Free();
DrawLineNumber(this.Handle);
}
base.WndProc(ref m);
}
DrawLineNumber function
Code:
private void DrawLineNumber(IntPtr hwnd)
{
RECT rt = new RECT();
GetUpdateRect(hwnd, out rt, false);
if (rt.Left < 28)
{
rt.Right = 28;
ValidateRect(hwnd, ref rt);
}
IntPtr hDC = GetDC(hwnd);
SetBkMode(hDC, TRANSPARENT);
IntPtr hRgn = CreateRectRgn(rt.Left, rt.Top, rt.Right, rt.Bottom);
IntPtr hObjRgn = SelectObject(hDC, hRgn);
IntPtr hObjFont = SelectObject(hDC, this.Font.ToHfont());
int lineno = SendMessage(hwnd, EM_GETFIRSTVISIBLELINE, 0, IntPtr.Zero) + 1;
GetClientRect(hwnd, out rt);
rt.Right = 28;
IntPtr hObjStock = GetStockObject(LTGRAY_BRUSH);
FillRect(hDC, ref rt, hObjStock);
int linecount = SendMessage(hwnd, EM_GETLINECOUNT, 0, IntPtr.Zero);
do
{
int fheight = DrawText(hDC,lineno.ToString(),-1,ref rt,DT_RIGHT + DT_TOP);
rt.Top = rt.Top + fheight;
lineno = lineno + 1;
}
while (linecount>=lineno);
SelectObject(hDC, hObjFont);
SelectObject(hDC, hObjStock);
ReleaseDC(hwnd, hDC);
}
Re: Code Editor similiar to Visual Studio 2005..
Thanks guys. I appreciate it! At least now I got something to work with.