附件 http://files.cnblogs.com/xe2011/richTextBox_EM_SETRECT.rar

using System.Runtime.InteropServices; public struct Rect{public int Left;public int Top;public int Right;public int Bottom;}[DllImport("user32.dll")]private static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, ref Rect lParam);//
private const int EM_GETRECT = 0x00b2;private const int EM_SETRECT = 0x00b3;public Rect RichTextBoxMargin{get{Rect rect = new Rect();SendMessage(richTextBox1.Handle, EM_GETRECT, IntPtr.Zero, ref rect);rect.Left += 1;rect.Top += 1;rect.Right = 1 + richTextBox1.ClientSize.Width - rect.Right;rect.Bottom = richTextBox1.ClientSize.Height - rect.Bottom;return rect;}set{Rect rect;rect.Left =  richTextBox1.ClientRectangle.Left + value.Left;rect.Top =   richTextBox1.ClientRectangle.Top + value.Top;rect.Right =  richTextBox1.ClientRectangle.Right - value.Right;rect.Bottom =  richTextBox1.ClientRectangle.Bottom - value.Bottom;SendMessage(richTextBox1.Handle, EM_SETRECT, IntPtr.Zero, ref rect);}}//设置private void button1_Click(object sender, EventArgs e){Rect rect;rect.Left = 50;rect.Top = 50;rect.Right = 50;rect.Bottom = 50;RichTextBoxMargin = rect;}//获得private void button2_Click(object sender, EventArgs e){Rect rect;rect = RichTextBoxMargin;MessageBox.Show( string.Format("Left = {0} top={1} right={2} bottom={3}",rect.Left,rect.Top,rect.Right,rect.Bottom));}

放在自定义的类里面

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 using System.Runtime.InteropServices;
  6
  7 namespace System.Windows.Forms
  8 {
  9     public class CustomRichTextBox:RichTextBox
 10     {
 11         public CustomRichTextBox()
 12         {
 13             richTextBox1=this;
 14         }
 15         private System.Windows.Forms.RichTextBox richTextBox1;
 16
 17         private struct Rect
 18         {
 19             public int Left;
 20             public int Top;
 21             public int Right;
 22             public int Bottom;
 23         }
 24
 25         [DllImport("user32.dll")]
 26         private static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, ref Rect lParam);
 27
 28         private const int EM_GETRECT = 0x00b2;
 29         private const int EM_SETRECT = 0x00b3;
 30
 31         /// <summary>
 32         /// 当没设置的时候结果多出了L T R +2
 33         /// </summary>
 34         private Rect RichTextBoxMargin
 35         {
 36             get
 37             {
 38                 Rect rect = new Rect();
 39                 SendMessage(richTextBox1.Handle, EM_GETRECT, IntPtr.Zero, ref rect);
 40                 rect.Left += 1;
 41                 rect.Top += 1;
 42                 rect.Right = 1 + richTextBox1.DisplayRectangle.Width - rect.Right;
 43                 rect.Bottom = richTextBox1.DisplayRectangle.Height - rect.Bottom;
 44                 return rect;
 45             }
 46             set
 47             {
 48                 Rect rect;
 49                 rect.Left = richTextBox1.ClientRectangle.Left + value.Left;
 50                 rect.Top = richTextBox1.ClientRectangle.Top + value.Top;
 51                 rect.Right = richTextBox1.ClientRectangle.Right - value.Right;
 52                 rect.Bottom = richTextBox1.ClientRectangle.Bottom - value.Bottom;
 53
 54                 SendMessage(richTextBox1.Handle, EM_SETRECT, IntPtr.Zero, ref rect);
 55             }
 56
 57         }
 58
 59
 60         public int LeftMargin
 61         {
 62             get
 63             {
 64                 return RichTextBoxMargin.Left;
 65             }
 66             set
 67             {
 68                 Rect rect1;
 69                 rect1 = RichTextBoxMargin;
 70
 71                 Rect rect;
 72                 rect.Left = value;
 73                 rect.Top = rect1.Top;
 74                 rect.Right = rect1.Right;
 75                 rect.Bottom = rect1.Bottom;
 76
 77                 RichTextBoxMargin = rect;
 78             }
 79         }
 80
 81
 82         public int RightMargin
 83         {
 84             get
 85             {
 86                 return RichTextBoxMargin.Right;
 87             }
 88             set
 89             {
 90                 Rect rect1;
 91                 rect1 = RichTextBoxMargin;
 92
 93                 Rect rect;
 94                 rect.Left = rect1.Left;
 95                 rect.Top = rect1.Top;
 96                 rect.Right = value;
 97                 rect.Bottom = rect1.Bottom;
 98
 99                 RichTextBoxMargin = rect;
100             }
101         }
102
103
104         public int TopMargin
105         {
106             get
107             {
108                 return RichTextBoxMargin.Top;
109             }
110             set
111             {
112                 Rect rect1;
113                 rect1 = RichTextBoxMargin;
114
115                 Rect rect;
116                 rect.Left = rect1.Left;
117                 rect.Top = value;
118                 rect.Right = rect1.Right;
119                 rect.Bottom = rect1.Bottom;
120
121                 RichTextBoxMargin = rect;
122             }
123         }
124
125         public int BottomMargin
126         {
127             get
128             {
129                 return RichTextBoxMargin.Bottom;
130             }
131             set
132             {
133                 Rect rect1;
134                 rect1 = RichTextBoxMargin;
135
136                 Rect rect;
137                 rect.Left = rect1.Left;
138                 rect.Top = rect1.Top;
139                 rect.Right = rect1.Right;
140                 rect.Bottom = value;
141                 RichTextBoxMargin = rect;
142             }
143         }
144
145
146     }
147 }

CustomRichTextBox.cs

使用

 1         private void button1_Click(object sender, EventArgs e)
 2         {
 3             customRichTextBox1.LeftMargin = 2;
 4             customRichTextBox1.TopMargin = 2;
 5             customRichTextBox1.RightMargin = 30;
 6             customRichTextBox1.BottomMargin = 30;
 7         }
 8
 9         private void button2_Click(object sender, EventArgs e)
10         {
11             Text = string.Format("Left={0} Right={1} Top={2} Bottom={3}",
12                 customRichTextBox1.LeftMargin,
13                 customRichTextBox1.TopMargin,
14                 customRichTextBox1.RightMargin,
15                 customRichTextBox1.BottomMargin);
16         }

View Code

转载于:https://www.cnblogs.com/xe2011/p/3469614.html

C# 如何设置 richTextBoxr的边距相关推荐

  1. iOS 设置UILabel 的内边距

    iOS 设置UILabel 的内边距 - (void)drawTextInRect:(CGRect)rect {UIEdgeInsets insets = {0, 5, 0, 5};[super dr ...

  2. Foxmail记事插入的表格怎么设置单元格边距

    Foxmail在使用记事时候,插入了表格,该怎么设置单元格的边距呢?下面我们就来看看详细的教程. Foxmail记事插入的表格怎么设置单元格边距? 1.下载并安装Foxmail邮件客户端. Foxma ...

  3. java word 纸张大小_Java 设置Word页边距、页面大小、页面方向、页面边框

    本文将通过java示例介绍如何设置word页边距(包括上.下.左.右).页面大小(可设置letter/a3/a4/a5/a6/b4/b5/b6/envelop dl/half letter/lette ...

  4. Java 设置Word页边距、页面大小、页面方向、页面边框

    本文将通过Java示例介绍如何设置Word页边距(包括上.下.左.右). 页面大小(可设置Letter/A3/A4/A5/A6/B4/B5/B6/Envelop DL/Half Letter/Lett ...

  5. 如何设置UILabel的内边距?

    最近在项目中,有个地方需要设置UILabel的内边距,即字体和Label控件之间的间隙.UILabel不像UIButton那样,有个contentEdgeInsets.titleEdgeInsets. ...

  6. 在html中怎么设置页面边距,在打印网页时怎么设置调整页边距

    在打印网页时怎么设置调整页边距 今天给大家介绍一下在打印网页时怎么设置调整页边距的具体操作步骤. 1. 首先打开电脑,找到想要打印的网页打开. 2. 点打开之后,在页面右上角点击三横图标. 3. 在弹 ...

  7. CSS边框设置以及内外边距的使用

    1.边框 该border属性为以下属性的简写属性.例如:border:1px solid black; border-width表示边框的粗细,可以是medium,thick等,一般使用数字加单位组合 ...

  8. HTML5系列代码:设置边框的边距

    marginheight 属性规定框架内容与框架的上方和下方之间的高度,以像素计. <html ><head><title>设置边框的边距</title> ...

  9. html设置word页边距,怎么设置Word的页边距

    在内侧和外侧框中. 另外一种情况:单击文件页面设置页边距选项卡, word页边距怎么设置呢? word默认页边距分2003和2007版,Word 2007的上下边距是:2.54厘米;左右边距是:3.1 ...

最新文章

  1. 一文详解非线性优化算法:保姆级教程-基础理论
  2. jquery toggle方法使用出错?请看这里-遁地龙卷风
  3. javafx 自定义控件_JavaFX技巧10:自定义复合控件
  4. 西贝莜面村员工手册_西贝那达慕草原美食节 引领文化生活新消费
  5. (最短路径算法整理)dijkstra、floyd、bellman-ford、spfa算法模板的整理与介绍
  6. Java中Object转Map类型,Map转Object类型
  7. Hive基本查询语法
  8. oracle快速解析,教你用Oracle解析函数快速检查序列间隙
  9. Html5 Canvas 百行代码实现扫雷
  10. 严格约束选股条件 能否找到跑赢市场的好公司?
  11. Elasticsearch:管理悬空(dangling)索引
  12. 使用excel校验身份证号码是否正确
  13. cmd命令行使用ffmpeg合并mp4格式视频文件
  14. 基于Proteus学习单片机系列(一)——点亮LED
  15. go IO操作-文件读
  16. 解决阿里oss远程图片html2canvas生成海报时跨域问题(附代码)
  17. 关于京东畅读卡的盈利模式猜想
  18. ipad打开网页无法播放视频
  19. 一文带你深入理解Redis中的底层数据结构,再也不怕不懂数据类型的底层了
  20. 使用IDEA 远程调试功能,服务器代码远程调试

热门文章

  1. 6Y叔的clusterProfiler-book阅读 Chapter 6 KEGG analysis
  2. Java关键字fymd,JAVA程式碼一直被foritfy檢出有path manipulation
  3. php调用python导出excel_如何使用Django导出Excel代码问题
  4. 人工智能语言python招聘_编程语言这么多为什么选Python
  5. 运筹优化(七)--动态规划解析
  6. 主存储器的技术指标有哪些?其含义是什么?
  7. Linux开发_最全在Ubnutu环境下为你的程序设置快捷启动项和启动时管理员权限
  8. OSGI嵌入tomcat应用服务器(gem-web)——资源下载
  9. CukeTest新版本公测邀请-Windows应用自动化
  10. java虚拟机之垃圾回收器