2017-01-25 1 views
0

tabcontrol itemsize와 관련하여 도움이 필요합니다. (winforms)tabControl 파일 이름에 따라 항목 크기가 조절됩니다.

나는 소유자가 그려진 tabcontrol을 가지고 있으며 정상적으로 작동한다. Itemsize는 너비가 0으로 설정되고 sizemode는 tabcontrol 속성에서 고정으로 설정됩니다. 내가 파일 이름을 열 때 나는 더 이상 파일 이름 열 경우 예를 들면있는 test.txt 다음 "버튼을 눌러"다음 "버튼"이

|thisisaverylongfilename.txt        | 
|anotherlongname.txt        | 

처럼 보이는, 그러나, 괜찮이

|test123.txt | 
|Untitled | 

과 같이 보입니다 나는 "버튼"을 만들려고 노력하고있어 파일 이름이 오른쪽의 공간과 맞고 공간이 모두 동일해야합니다. 다음은

는 사용자 그려을 TabControl

 Image cross = imageList1.Images[0]; 
     int xWidth = cross.Width; 

     TabPage tp = tabControl1.TabPages[e.Index]; 

     Size size = tabControl1.ItemSize; 

     Font fntTab; 
     Brush bshBack; 
     Brush bshFore; 

     if (e.Index == this.tabControl1.SelectedIndex) 
     { 
      fntTab = new Font(FontFamily.GenericSansSerif, 8.0F, FontStyle.Regular); 
      bshBack = new System.Drawing.Drawing2D.LinearGradientBrush(e.Bounds, Color.FromName(tabpages_primary_backcolor), Color.FromName(tabpages_secondary_backcolor), System.Drawing.Drawing2D.LinearGradientMode.BackwardDiagonal); 
      bshFore = Brushes.Firebrick; 
     } 
     else 
     { 
      fntTab = new Font(FontFamily.GenericSansSerif, 8.0F, FontStyle.Regular); 

      bshBack = new SolidBrush(Color.Empty); 
      bshFore = new SolidBrush(Color.Black); 
     } 

     string tabName = this.tabControl1.TabPages[e.Index].Text; 
     StringFormat sftTab = new StringFormat(); 
     e.Graphics.FillRectangle(bshBack, e.Bounds); 
     Rectangle recTab = e.Bounds; 
     recTab = new Rectangle(recTab.X + 5, recTab.Y + 4, recTab.Width, recTab.Height - 4); 
     e.Graphics.DrawString(tabName, fntTab, bshFore, recTab, sftTab); 


     if (tabControl1.SelectedTab != null) 
     { 
      currentCrossRect = new Rectangle(
      e.Bounds.Left + size.Width - 22, 3, xWidth, xWidth); 
      e.Graphics.DrawImage(cross, currentCrossRect.X - 2, currentCrossRect.Y + 2); 
     } 

덕분에 내 코드와

+0

예 죄송합니다 - 소유자가 작성한 것으로 winforms를위한 것입니다. –

답변

0

를 사용하여 텍스트의 폭을 얻기에 관하여 :

SizeF s = e.Graphics.MeasureString(myString, myFont); 

을 그리고 당신은 항목의 폭을 설정할 수 있습니다 ~ s.Width!

+0

안녕하세요 Galister. 그러나 나는이 질문을 게시하기 전에 measurestring을 살펴 봤지만, 코드에서 너비를 s.Width로 설정하는 위치가 tabControl 속성에서 가져온 너비와 같은지 잘 모르겠습니다. 추가 할 경우 tabControl1.ItemSize = 새 크기 (s. 폭, 25) 탭 단추는 너비가 변경되지 않고 중단되지 않고 깜박입니다. –

+0

autosize 속성을 변경해 보셨습니까? – galister

관련 문제