2015-10-21 15 views
2

水平对齐我按照在MSDN网站上的说明: https://msdn.microsoft.com/en-us/library/vstudio/ms404305%28v=vs.100%29.aspx 来显示选项卡控制,如下图:C#标签控制左与图象

enter image description here

而这是代码:

private void tcMain_DrawItem(Object sender, DrawItemEventArgs e) 
{ 
    Graphics g = e.Graphics; 
    Brush _textBrush; 

    TabPage _tabPage = tcMain.TabPages[e.Index]; 
    Rectangle _tabBounds = tcMain.GetTabRect(e.Index); 

    if (e.State == DrawItemState.Selected) 
    { 
     _textBrush = new SolidBrush(Color.Red); 
     g.FillRectangle(Brushes.Gray, e.Bounds); 
    } 
    else 
    { 
     _textBrush = new System.Drawing.SolidBrush(e.ForeColor); 
     e.DrawBackground(); 
    } 

    Font _tabFont = new Font("Times New Roman", (float)22, FontStyle.Regular, GraphicsUnit.Pixel); 

    StringFormat _stringFlags = new StringFormat(); 
    _stringFlags.Alignment = StringAlignment.Near; 
    _stringFlags.LineAlignment = StringAlignment.Center; 
    g.DrawString(_tabPage.Text, _tabFont, _textBrush, _tabBounds, new StringFormat(_stringFlags)); 
} 

现在我想添加一个图片到选项卡控制按钮的左侧?我的意思是,如何在单词Book的左侧显示图片,例如?

从下面尝试一些答案后,我结束了与代码:

private void tcMain_DrawItem(Object sender, DrawItemEventArgs e) 
    { 
     Graphics g = e.Graphics; 
     Brush _textBrush; 

     TabPage _tabPage = tcMain.TabPages[e.Index]; 
     Rectangle _tabBounds = tcMain.GetTabRect(e.Index); 

     if (e.State == DrawItemState.Selected) 
     { 
      _textBrush = new SolidBrush(Color.Red); 
      g.FillRectangle(Brushes.Gray, e.Bounds); 
     } 
     else 
     { 
      _textBrush = new System.Drawing.SolidBrush(e.ForeColor); 
      e.DrawBackground(); 
     } 

     tcMain.ImageList = imgList; 
     tcMain.TabPages[0].ImageIndex = 1; 
     tcMain.TabPages[1].ImageIndex = 0; 
     tcMain.TabPages[2].ImageIndex = 3; 
     tcMain.TabPages[3].ImageIndex = 2; 

     Rectangle tabImage = tcMain.GetTabRect(e.Index); 
     tabImage.Size = new Size(40, 40); 

     g.DrawImage(tcMain.ImageList.Images[_tabPage.ImageIndex], tabImage); 

     Font _tabFont = new Font("Times New Roman", (float)22, FontStyle.Regular, GraphicsUnit.Pixel); 

     StringFormat _stringFlags = new StringFormat(); 
     _stringFlags.Alignment = StringAlignment.Center; 
     _stringFlags.LineAlignment = StringAlignment.Center; 
     g.DrawString(_tabPage.Text, _tabFont, _textBrush, _tabBounds, new StringFormat(_stringFlags)); 
    } 

后来,当我拍摄快照的结果时,就这样产生了: enter image description here

enter image description here

刷新所有的时间

+2

您已经处理绘制自己。使用'图形'对象绘制你喜欢的任何图片,无论你喜欢什么。 –

+0

你能否做一些编码?我应该把它放在哪里? –

+1

你为什么不尝试自己写一些代码?显然,您将DrawImage与DrawString一起使用。 – TaW

回答

1

你可以添加一个ImageList控制到你的p roject并添加一些图像,并设置您的TabPagesImageIndex属性。然后在DrawItem事件中使用DrawImage()方法。

Rectangle tabImage = tcMain.GetTabRect(e.Index); 
tabImage.Size = new Size(16, 16); 

g.DrawImage(tcMain.ImageList.Images[_tabPage.ImageIndex], tabImage); 

你也可以使用ImageKey代替ImageIndex

g.DrawImage(tcMain.ImageList.Images[_tabPage.ImageKey], tabImage); 

如果添加ImageListImageIndex编程来看看:

ImageList imageList = new ImageList(); 
imageList.Images.Add("key1", Image.FromFile("pathtofile")); 
imageList.Images.Add("key2", Image.FromFile("pathtofile")); 

tcMain.ImageList = imageList; 
tcMain.TabPages[0].ImageIndex = 1; 
tcMain.TabPages[1].ImageIndex = 0; 
+0

这真是太棒了,除了现在的选项卡控制不稳定之外,它工作了90%。我的意思是,如果它是一个独立的视频显示和隐藏。此外,文字已消失 –

+0

您对“单独显示和隐藏视频”有何意义? – Roman

+0

实际上,我只有第一个标签的图片,它总是从图片列表中的每张图片改变而来! –