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
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