C# 选项卡控件与图片左水平对齐

C# tab control left horizontal alignment with picture

我已经按照 msdn 网站中的说明进行操作: https://msdn.microsoft.com/en-us/library/vstudio/ms404305%28v=vs.100%29.aspx 显示标签控件如下图:

这是代码:

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));
    }

然后当我对结果进行快照时,它是这样的:

一直在刷新

您可以将 ImageList 控件添加到您的项目并向其添加一些图像并设置 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;