gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 12:57:59 25 4
gpt4 key购买 nike

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

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

一直在刷新

最佳答案

您可以将 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;

关于C# 选项卡控件与图片左水平对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33251214/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com