C# 獲取RICHTEXTBOX所有圖形的位置和大小

 

沒什麼好說的...是是建立了一個RichTextBox. 然後去判斷RichTextBox的類型爲OBJECT

而且 有RTF的OBJ定義字段 objw objh 來獲取的

 

 

全部代碼

 

        public IList<Rectangle> GetRichTextObjRectangle(RichTextBox p_RichTextBox)
        {
            IList<Rectangle> _RectangleList =new List<Rectangle>();
            RichTextBox _RichText = new RichTextBox();
            _RichText.Rtf = p_RichTextBox.Rtf;
            int _Count = p_RichTextBox.Text.Length;

            for (int i = 0; i != _Count; i++)
            {
                if (p_RichTextBox.Text[i] == ' ')
                {
                    _RichText.Select(i, 1);

                    if (_RichText.SelectionType.ToString() == "Object")
                    {
                        Point _StarPoint = p_RichTextBox.GetPositionFromCharIndex(i);

                        System.Text.RegularExpressions.Regex _RegexWidth = new System.Text.RegularExpressions.Regex(@"(?<=//objw)[^//]+");
                        System.Text.RegularExpressions.Regex _RegexHeight = new System.Text.RegularExpressions.Regex(@"(?<=//objh)[^{]+");

                        int _Width = 0;
                        int _Height = 0;

                        if(int.TryParse(_RegexWidth.Match(_RichText.SelectedRtf).Value,out _Width) &&  int.TryParse(_RegexHeight.Match(_RichText.SelectedRtf).Value,out _Height))
                        {
                            //這裏的數字要 /15纔可以
                            _RectangleList.Add(new Rectangle(_StarPoint.X, _StarPoint.Y, _Width/15, _Height/15));                          
                        }                       
                    }
                }      
            }
            _RichText.Dispose();
            return _RectangleList;
        }

 

其實非常的少..

 

 

發佈了95 篇原創文章 · 獲贊 17 · 訪問量 56萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章