简体   繁体   English

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

[英]C# tab control left horizontal alignment with picture

I have followed the instructions in the msdn website: https://msdn.microsoft.com/en-us/library/vstudio/ms404305%28v=vs.100%29.aspx to display a tab control as the following picture:我已经按照msdn网站上的说明操作: https : //msdn.microsoft.com/en-us/library/vstudio/ms404305%28v=vs.100%29.aspx显示如下图的选项卡控件:

在此处输入图片说明

And this is the code:这是代码:

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

Now I want to add a picture to the left of the tab control buttons?现在我想在选项卡控件按钮的左侧添加图片? I mean, how to show a picture to the left of the word Book for example?我的意思是,例如,如何在单词 Book 的左侧显示图片?

After trying some answers from below I ended up with the code:在尝试了下面的一些答案后,我最终得到了代码:

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

Then when I take a snapshot to the results it comes like this:然后当我对结果进行快照时,它是这样的: 在此处输入图片说明

在此处输入图片说明

Refreshing all the time时刻刷新

You could add an ImageList control to your project and add some images to it and set the ImageIndex property of your TabPages .您可以向您的项目添加一个ImageList控件并向其中添加一些图像并设置您的TabPagesImageIndex属性。 Then just use DrawImage() method in your DrawItem event.然后只需在DrawItem事件中使用DrawImage()方法。

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

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

You could also use ImageKey instead of ImageIndex .您也可以使用ImageKey而不是ImageIndex

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

If you add the ImageList and the ImageIndex programmatically take a look:如果您以编程方式添加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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM