• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C#如何设置richTextBoxr的边距

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

 

附件 https://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

 

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap