C# 如何设置 richTextBoxr的边距
附件 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的边距的更多相关文章
- iOS 设置UILabel 的内边距
iOS 设置UILabel 的内边距 - (void)drawTextInRect:(CGRect)rect { UIEdgeInsets insets = {, , , }; [super draw ...
- Java 设置Word页边距、页面大小、页面方向、页面边框
本文将通过Java示例介绍如何设置Word页边距(包括上.下.左.右).页面大小(可设置Letter/A3/A4/A5/A6/B4/B5/B6/Envelop DL/Half Letter/Lette ...
- Epplus 设置excel 页边距 及多文件合并
1:使用epplus合并多个excel文件到同一excel的不同sheet页中 private static bool MergeExcel(string _stFilePath, List<s ...
- label_设置行距、字距及计算含有行间距的label高度
// // ViewController.m // CNBlogs // // Created by PXJ on 16/5/27. // Copyright © 2016年 PXJ. All ...
- 设置textfield 文字左边距
默认情况下,当向textField输入文字时,文字会紧贴在textField左边框上.我们可以通过设置textField的leftView,设置一个只有宽度的leftView.这样还不够,因为默认le ...
- POI设置Word页边距
参考资料:http://stackoverflow.com/questions/17787176/spacing-and-margin-settings-in-word-document-using- ...
- textField设置输入文字距左边的距离
1.设置tetxField的内边距 [self.yourTextField setValue:[NSNumber numberWithInt:5] forKey:@"paddingTop&q ...
- [iOS]UIButton内、外边距设置
- (void)viewDidLoad { [super viewDidLoad]; /* UIButton设置对应的边距image跟title的边距属性 ...
- 探究负边距(negative margin)原理
W3C规范在介绍margin时有这样一句话: Negative values for margin properties are allowed, but there may be implement ...
随机推荐
- HTTP状态码——对照表
ASCII码介绍: HTTP状态码(HTTP Status Code)用来表示web服务器响应客户端的HTTP状态.主要有一下5种状态类型.1xx 消息2xx 成功3xx 重定向4x ...
- socket.io
http://www.cnblogs.com/fullhouse/archive/2011/07/18/2109936.html http://www.cnblogs.com/fullhouse/ar ...
- find+*的问题
转自find+*的问题 不久前做移植的时候想把某个目录下的C文件都找出来,然后拷贝下,结果一直报错,我用的是*.c作为pattern.今天看论坛的时候知道为什么了. $ ls test2.c tes ...
- 分析ECMall的注册与登录机制
ecmall的注册流程index.php?app=member&act=register. 首先app是member,act是register方法. index.php中.通过ecmall的s ...
- POJ 1286 Necklace of Beads(Polya定理)
点我看题目 题意 :给你3个颜色的n个珠子,能组成多少不同形式的项链. 思路 :这个题分类就是polya定理,这个定理看起来真的是很麻烦啊T_T.......看了有个人写的不错: Polya定理: ( ...
- PNG在IE6下背景问题
png24.min.js 源代码: var DD_belatedPNG={ns:"DD_belatedPNG",imgSize:{},delay:10,nodesFixed:0,c ...
- Android 实现ListView异步加载图片
ListView异步加载图片是非常实用的方法,凡是是要通过网络获取图片资源一般使用这种方法比较好,用户体验好,下面就说实现方法,先贴上主方法的代码: package cn.wangmeng.test; ...
- TDBGrideh表头自动排序设置
自动显示标题行的升降排序标志符(▽降序△升序)并做相应排序DBGridEh组件可以在标题行单元格中显示小三角形升.降排序标志符图片,在运行时可点击标题行,图片自动切换并做相应排序. 具体属性设置如下: ...
- [cocos2dx]计算scrollview元素的index
scrollview的原生代码没有提供元素对齐功能 通过下面介绍的index计算方法以及scrollview自带的设置位置方法 void setContentOffsetInDuration(CCPo ...
- Android上按钮解决快速点击问题
//代码2 public abstract class NoDoubleClickListener implements OnClickListener { ...