Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm Controls / December 2007

Tip: Looking for answers? Try searching our database.

using EM_GETRECT vs. using EM_GETLINECOUNT with a RichTextBox

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Bill Woodruff - 20 Dec 2007 14:11 GMT
I develop in C#.

The 'beauty' of using 'SendMessage 'EM_GETLINECOUNT ... with a TextBox or
RichTextBox ... is that it returns not just the lines that are visible in
the RichTextBox at its current size, but effectively the number of lines of
text content (including lines produced by word-wrap, if word-wrap is enabled
for the control).

Thanks to Mick Doherty, on this forum, for the tip on using
'EM_GETLINECOUNT.

I thought I might be able to use 'SendMessage 'EM_GETRECT on a RichTextBox
... after a bunch of richly formatted text was pasted in ... to get the
total size of the Text display in the control, but this is not what
'EM_GETRECT returns. 'EM_GETRECT returns the rectangle of the visible client
area of the control itself.

I am going to hypothesize that there isn't a simple 'SendMessage call to get
the total size of all the text in a RichTextBox (which includes multiple
fonts and font-sizes) outside the visible client area of the control.  My
guess that I will have to "escalate" to a technique of creating an invisible
picture box, drawing into it, and then doing using 'MeasureString on the
'Graphics of the RichTextBox (a technique suggested by Rod Stephens of
VBHelper who was kind enough to send me some source in VB).

I have not implemented/tested Rod's code in C# yet, but I do note that the
'MeasureString call (in all its overloads) requires specification of a 'Font
as second parameter, so I'm assuming that solution is blocked for a
multi-font RichTextBox contents.

So the question is :  how do you measure the height of the contents of a
RichTextBox in the case where richly formatted text in different fonts
and/or font-sizes extends beyond the current visible client-area of the
control ?

thanks !

Bill
Mick Doherty - 21 Dec 2007 18:09 GMT
Catch the RichTextBox's EN_REQUESTRESIZE notifications and modify the
controls bounds according to the information returned in the REQUESTSIZE
structure.

Simple example of autosizing a RichTextBox follows, based upon a standard
RichTextBox with both BorderStyle and Scrollbars set to none.

\\\
public partial class Form1 : Form
{
   public Form1()
   {
       InitializeComponent();
       txtWnd = new NativeTextBox();
       txtWnd.AssignHandle(this.richTextBox1.Handle);

//Simple method to force a Resize on an empty richtextbox
       this.richTextBox1.Text = "a";
       this.richTextBox1.Text = "";
   }

   public NativeTextBox txtWnd;

   [StructLayout(LayoutKind.Sequential)]
   public struct NMHDR
   {
       public IntPtr HWND;
       public uint idFrom;
       public int code;
       public override String ToString()
       {
           return String.Format("Hwnd: {0}, ControlID: {1}, Code: {2}",
HWND, idFrom, code);
       }
   }

   [StructLayout(LayoutKind.Sequential)]
   public struct REQRESIZE
   {
       public NMHDR nmhdr;
       public RECT rc;
   }

   [StructLayout(LayoutKind.Sequential)]
   public struct RECT
   {
       public int Left, Top, Right, Bottom;
       public override string ToString()
       {
           return String.Format("{0}, {1}, {2}, {3}", Left, Top, Right,
Bottom);
       }
       public Rectangle ToRectangle()
       {
           return Rectangle.FromLTRB(Left, Top, Right, Bottom);
       }
   }

   public const int WM_USER = 0x400;
   public const int WM_NOTIFY = 0x4E;
   public const int WM_REFLECT = WM_USER + 0x1C00;
   public const int EN_REQUESTRESIZE = 0x701;

   public class NativeTextBox : NativeWindow
   {
       protected override void WndProc(ref Message m)
       {
           base.WndProc(ref m);
           if (m.Msg == (WM_REFLECT | WM_NOTIFY))
           {
               NMHDR hdr = (NMHDR)(Marshal.PtrToStructure(m.LParam,
typeof(NMHDR)));
               if (hdr.code == EN_REQUESTRESIZE)
               {
                   REQRESIZE rrs =
(REQRESIZE)(Marshal.PtrToStructure(m.LParam, typeof(REQRESIZE)));
                   Control tb = Control.FromHandle(this.Handle);
                   Rectangle rc =rrs.rc.ToRectangle();
                   tb.SetBounds(0,0,tb.Width,rc.Height,
BoundsSpecified.Size);
               }
           }
       }
   }
}
///

Signature

Mick Doherty
http://www.dotnetrix.co.uk/nothing.html

>I develop in C#.
>
[quoted text clipped - 40 lines]
>
> Bill
Bill Woodruff - 25 Dec 2007 10:53 GMT
Mick Doherty wrote :

"Catch the RichTextBox's EN_REQUESTRESIZE notifications and modify the
controls bounds according to the information returned in the REQUESTSIZE
structure.

Simple example of autosizing a RichTextBox follows, based upon a standard
RichTextBox with both BorderStyle and Scrollbars set to none."

... sample code snipped ...

Hi Mick,

Many thanks for your prompt response and this sample code !  I look forward
to testing it very soon. Happy holidays to you and your family :)

best, Bill Woodruff
dotScience
Chiang Mai, Thailand

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.