Draw on rich text box  
Author Message
Karrar





PostPosted: Windows Forms General, Draw on rich text box Top

Hi Every one,
I'd like to know that it is possiable to draw on the richtext box control or not
and if it's possiable how can I do it
thanx



Windows Forms32  
 
 
Mubshir Raza Ali





PostPosted: Windows Forms General, Draw on rich text box Top

Hi Karrar,

 

Yes you can do that.

For example here is code to draw a rectangle in the rich text box.

 


{\rtf1\fbidis\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\froman\fprq2\fcharset0 Times New Roman;}}

\viewkind4\uc1\pard\ltrpar\f0\fs24\par

\trowd\trgaph108\trleft-108\trbrdrt\brdrs\brdrw10 \trbrdrl\brdrs\brdrw10 \trbrdrb\brdrs\brdrw10 \trbrdrr\brdrs\brdrw10 \clbrdrt\brdrw15\brdrs\clbrdrl\brdrw15\brdrs\clbrdrb\brdrw15\brdrs\clbrdrr\brdrw15\brdrs \cellx1802\pard\intbl\ltrpar\cell\row

\pard\ltrpar\par

\par

}



 

What you have to do is use GDI+ show drawings when user trying to draw rectangle (etc) on rich text box and then manually generate RFT for that to actually create rectangle in the rich text box.

But you must have complete knowledge of RTF specification that you can find at given below link.

 

http://www.microsoft.com/downloads/thankyou.aspx familyId=ac57de32-17f0-4b46-9e4e-467ef9bc5540&displayLang=en&oRef=


You can also get few RTF’s from Word, for example draw a 4x4 table in word then past it in the rich text box and then check what’s is the RTF of rich text box that should be RTF of a 4x4 table.

This is not an easy task by doing this practice soon you will be familiar with RTF and find it easy.

Hope this help.

 



 
 
Karthik Krishnaswami





PostPosted: Windows Forms General, Draw on rich text box Top

Do you want to use the Graphics object to do custom painting or do one like how Mushbir has done

Both are possible.


 
 
Karrar





PostPosted: Windows Forms General, Draw on rich text box Top

Hi Mubshir.
i can't tell you how happy i'm to know that the drawing on the rich text box is possiable and greatfull for your concern but i need a code for VB .NET 2003 , i'm sorry but i forgot to say that in my question ,
can you and all of our forums members please help me with this issue
thanx to all of you for your time


 
 
Mubshir Raza Ali





PostPosted: Windows Forms General, Draw on rich text box Top

Hi Karrar,

All the members are realy nice here, if i am not some one other will be helping you. You will get the answer what ever you ask.

In my previouse post i have shown the RTF code that needed to draw a rectangle RTF has no concern with VB or C# . Rather than code i should say RTF required to draw a rectangle.

So If you put that RTF in text box and then on some button press assing that text to richtexbox it will draw a rectanlge for example..

Add a text box and put following RTF in that textbox then

---------------------


{\rtf1\fbidis\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\froman\fprq2\fcharset0 Times New Roman;}}

\viewkind4\uc1\pard\ltrpar\f0\fs24\par

\trowd\trgaph108\trleft-108\trbrdrt\brdrs\brdrw10 \trbrdrl\brdrs\brdrw10 \trbrdrb\brdrs\brdrw10 \trbrdrr\brdrs\brdrw10 \clbrdrt\brdrw15\brdrs\clbrdrl\brdrw15\brdrs\clbrdrb\brdrw15\brdrs\clbrdrr\brdrw15\brdrs \cellx1802\pard\intbl\ltrpar\cell\row

\pard\ltrpar\par

\par

}


-------------------------

Add a button and

On button click you write following code

richtextBox1.RTF=textBox1.text

then you will see rectangle in the richtext box

Read the RTF specification and you will learn more how to deal with different type of data and formats...

Hope this help...



 
 
Karthik Krishnaswami





PostPosted: Windows Forms General, Draw on rich text box Top

Here is the code in C# for an extended RichTextBox,which allows you to draw on a richtextBox.You can convert this code to VB.Net.

using System;

using System.Drawing;

using System.Runtime.InteropServices;

using System.Windows.Forms;

namespace WindowsApplication1

{

/// <summary>

/// Summary description for RichTextBoxEx.

/// </summary>

public class RichTextBoxEx : RichTextBox

{

private const int WM_PAINT = 0xF;

public RichTextBoxEx()

{

}

protected override void WndProc(ref Message m)

{

base.WndProc (ref m);

switch (m.Msg)

{

case WM_PAINT:

RePaint();

break;

}

}

[StructLayout(LayoutKind.Sequential)]

private struct RECT

{

public int left;

public int top;

public int right;

public int bottom;

}

[DllImport("user32")]

private extern static IntPtr GetDC(

IntPtr hwnd);

[DllImport("user32")]

private extern static int GetClientRect(

IntPtr hwnd,

ref RECT rc);

private void RePaint()

{

RECT rcClient = new RECT();

IntPtr hdc = GetDC(this.Handle);

Graphics g = Graphics.FromHdc(hdc);

GetClientRect(Handle, ref rcClient);

g.FillRectangle(Brushes.Azure, rcClient.left, rcClient.top, rcClient.right - rcClient.left, rcClient.bottom - rcClient.top);

g.DrawEllipse(new Pen(Color.Green,1), rcClient.left, rcClient.top, rcClient.right - rcClient.left, rcClient.bottom - rcClient.top);

}

}

}