API声明: public   class   API 

///   <summary> 
///   公共控件是当控件中发生事件(如用户输入)时,将通知消息发送到父窗口的子窗口。 
///   </summary> 
public   const   int   WM_NOTIFY   =   0x4E; 
///   <summary> 
///   当控件需要改变大小时,将通知消息发送到父窗口的子窗口。 
///   </summary> 
public   const   int   EN_REQUESTRESIZE   =   0x701; 
} ///   <summary> 
///   Contains   information   about   a   notification   message. 
///   </summary> 
public   struct   NMHDR 

///   <summary> 
///   Window   handle   to   the   control   sending   a   message. 
///   </summary> 
public   int   hwndFrom; 
///   <summary> 
///   Identifier   of   the   control   sending   a   message. 
///   </summary> 
public   int   idFrom; 
///   <summary> 
///   Notification   code.   This   member   can   be   a   control-specific   notification   code   or   it   can   be   one   of   the   common   notification   codes. 
///   </summary> 
public   int   code; 

///   <summary> 
///   This   structure   defines   the   coordinates   of   the   upper-left   and   lower-right   corners   of   a   rectangle. 
///   </summary> 
public   struct   RECT 

///   <summary> 
///   Specifies   the   x-coordinate   of   the   upper-left   corner   of   the   rectangle.   
///   </summary> 
public   int   left; 
///   <summary> 
///   Specifies   the   y-coordinate   of   the   upper-left   corner   of   the   rectangle. 
///   </summary> 
public   int   top; 
///   <summary> 
///   Specifies   the   x-coordinate   of   the   lower-right   corner   of   the   rectangle. 
///   </summary> 
public   int   right; 
///   <summary> 
///   Specifies   the   y-coordinate   of   the   lower-right   corner   of   the   rectangle. 
///   </summary> 
public   int   bottom; 

///   <summary> 
///   需要改变大小 
///   </summary> 
public   struct   REQSIZE 

///   <summary> 
///   NMHDR结构 
///   </summary> 
public   NMHDR   nmhdr; 
///   <summary> 
///   RECT结构 
///   </summary> 
public   RECT   rect; 
} 在UserControl里面加一个RichTextBox,重写WndProc方法: 
public class UserControl : RichTextBox
{
protected   override   void   WndProc(ref   Message   m) 

if(m.Msg   ==   API.WM_NOTIFY   &&   autoResize) 

NMHDR   n   =   (NMHDR)Marshal.PtrToStructure(m.LParam,   typeof(NMHDR)); 
if(n.code   ==   API.EN_REQUESTRESIZE) 

REQSIZE   r   =   (REQSIZE)Marshal.PtrToStructure(m.LParam,   typeof(REQSIZE)); 
if(Math.Abs(r.rect.bottom   +   2   *   this.BorderWidth   -   this.Height)   <   2) 
return; 
this.SuspendLayout(); 
this.Height   =   r.rect.bottom   +   2   *   this.BorderWidth; 
this.ResumeLayout(); 


base.WndProc(ref   m); 

}