附件 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 += ;
rect.Top += ;
rect.Right = + 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 = ;
rect.Top = ;
rect.Right = ;
rect.Bottom = ; 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));
}

放在自定义的类里面

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices; namespace System.Windows.Forms
{
public class CustomRichTextBox:RichTextBox
{
public CustomRichTextBox()
{
richTextBox1=this;
}
private System.Windows.Forms.RichTextBox richTextBox1; private 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; /// <summary>
/// 当没设置的时候结果多出了L T R +2
/// </summary>
private Rect RichTextBoxMargin
{
get
{
Rect rect = new Rect();
SendMessage(richTextBox1.Handle, EM_GETRECT, IntPtr.Zero, ref rect);
rect.Left += ;
rect.Top += ;
rect.Right = + richTextBox1.DisplayRectangle.Width - rect.Right;
rect.Bottom = richTextBox1.DisplayRectangle.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);
} } public int LeftMargin
{
get
{
return RichTextBoxMargin.Left;
}
set
{
Rect rect1;
rect1 = RichTextBoxMargin; Rect rect;
rect.Left = value;
rect.Top = rect1.Top;
rect.Right = rect1.Right;
rect.Bottom = rect1.Bottom; RichTextBoxMargin = rect;
}
} public int RightMargin
{
get
{
return RichTextBoxMargin.Right;
}
set
{
Rect rect1;
rect1 = RichTextBoxMargin; Rect rect;
rect.Left = rect1.Left;
rect.Top = rect1.Top;
rect.Right = value;
rect.Bottom = rect1.Bottom; RichTextBoxMargin = rect;
}
} public int TopMargin
{
get
{
return RichTextBoxMargin.Top;
}
set
{
Rect rect1;
rect1 = RichTextBoxMargin; Rect rect;
rect.Left = rect1.Left;
rect.Top = value;
rect.Right = rect1.Right;
rect.Bottom = rect1.Bottom; RichTextBoxMargin = rect;
}
} public int BottomMargin
{
get
{
return RichTextBoxMargin.Bottom;
}
set
{
Rect rect1;
rect1 = RichTextBoxMargin; Rect rect;
rect.Left = rect1.Left;
rect.Top = rect1.Top;
rect.Right = rect1.Right;
rect.Bottom = value;
RichTextBoxMargin = rect;
}
} }
}

CustomRichTextBox.cs

使用

         private void button1_Click(object sender, EventArgs e)
{
customRichTextBox1.LeftMargin = ;
customRichTextBox1.TopMargin = ;
customRichTextBox1.RightMargin = ;
customRichTextBox1.BottomMargin = ;
} private void button2_Click(object sender, EventArgs e)
{
Text = string.Format("Left={0} Right={1} Top={2} Bottom={3}",
customRichTextBox1.LeftMargin,
customRichTextBox1.TopMargin,
customRichTextBox1.RightMargin,
customRichTextBox1.BottomMargin);
}

C# 如何设置 richTextBoxr的边距的更多相关文章

  1. iOS 设置UILabel 的内边距

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

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

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

  3. Epplus 设置excel 页边距 及多文件合并

    1:使用epplus合并多个excel文件到同一excel的不同sheet页中 private static bool MergeExcel(string _stFilePath, List<s ...

  4. label_设置行距、字距及计算含有行间距的label高度

    // //  ViewController.m //  CNBlogs // //  Created by PXJ on 16/5/27. //  Copyright © 2016年 PXJ. All ...

  5. 设置textfield 文字左边距

    默认情况下,当向textField输入文字时,文字会紧贴在textField左边框上.我们可以通过设置textField的leftView,设置一个只有宽度的leftView.这样还不够,因为默认le ...

  6. POI设置Word页边距

    参考资料:http://stackoverflow.com/questions/17787176/spacing-and-margin-settings-in-word-document-using- ...

  7. textField设置输入文字距左边的距离

    1.设置tetxField的内边距 [self.yourTextField setValue:[NSNumber numberWithInt:5] forKey:@"paddingTop&q ...

  8. [iOS]UIButton内、外边距设置

    - (void)viewDidLoad {        [super viewDidLoad];        /*         UIButton设置对应的边距image跟title的边距属性  ...

  9. 探究负边距(negative margin)原理

    W3C规范在介绍margin时有这样一句话: Negative values for margin properties are allowed, but there may be implement ...

随机推荐

  1. 4070: [Apio2015]雅加达的摩天楼

    Description 印尼首都雅加达市有 N 座摩天楼,它们排列成一条直线,我们从左到右依次将它们编号为 0 到 N−1.除了这 N 座摩天楼外,雅加达市没有其他摩天楼.   有 M 只叫做 “do ...

  2. SDWebImage 清除缓存

    1.找到SDImageCache类 2.添加如下方法: - (float)checkTmpSize { float totalSize = 0; NSDirectoryEnumerator *file ...

  3. Java基础类型自动装箱(autoboxing)

    Java SE 1.5 版本及之后,开始提供自动装箱功能. 自动装箱,将基础类型“包装”成一种类型: 基本类型 -->  包装器类 如下过程可触发自动装箱过程: Integer count = ...

  4. 【转载】ASP.NET获取路径的方法

    HttpContext.Current.Request.PhysicalPath;    // 获得当前页面的完整物理路径.比如 F:\XFU.NSQS\project\website\Default ...

  5. Discuz <= 7.2 SQL注入漏洞详情

    在<高级PHP应用程序漏洞审核技术>[1]一文里的"魔术引号带来的新的安全问题"一节里,有 提到通过提取魔术引号产生的“\”字符带来的安全问题,同样这个问题在这里又一次 ...

  6. 火车车次查询-余票查询--Api接口

    1.来自12306的火车车次数据 使用12306网站的接口,查询余票.此接口采集自 这里. 全国火车站代号字典,下载 . 火车票余票查询 http://dynamic.12306.cn/otsquer ...

  7. iOS -view横向变成竖向

    -------

  8. Library Cache Lookup

    Libraey Cache Data Access library cache是关于SQL语句的SGA中的一系列的链表, library cache是通过访问一系列的hash buckets,实现使用 ...

  9. USACO3.41Closed Fences(几何)

    这题水的真不易..300多行 累死了 对着数据查错啊 枚举每个边上的点到源点 是否中间隔着别的边  每条边划分500份就够了  注意一下与源点在一条直线上的边不算 几何 啊,,好繁琐 参考各种模版.. ...

  10. Android Loader详解三:重启与回调

    重启装载器 当你使用initLoader()时,如果指定ID的装载器已经存在,则它使用这个装载器.如果不存在呢,它将创建一个新的.但是有时你却是想丢弃旧的然后开始新的数据. 要想丢弃旧数据,你应使用r ...