对于权的TabPages到左的TabControl C#关闭按钮 [英] Close button for TabPages of Right To Left TabControl c#

查看:1026
本文介绍了对于权的TabPages到左的TabControl C#关闭按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想关闭按钮添加到的TabPages 的TabControl 。我尝试此代码,它工作正常使用从左到右的TabControl:

I want to add a close button to TabPages of a TabControl. I try this code and it works fine with a Left To Right TabControl:

private Point _imageLocation = new Point(13, 5);
private Point _imgHitArea = new Point(13, 2);

this.tabControl2.DrawMode = System.Windows.Forms.TabDrawMode.OwnerDrawFixed;

tabControl2.DrawItem += TabControl2_DrawItem;

private void TabControl2_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
{
    try
    {
        Image img = new Bitmap(GestionP.Properties.Resources.Close);
        Rectangle r = e.Bounds;
        r = this.tabControl2.GetTabRect(e.Index);
        r.Offset(2, 2);
        Brush TitleBrush = new SolidBrush(Color.Black);
        Font f = this.Font;
        string title = this.tabControl2.TabPages[e.Index].Text;
        e.Graphics.DrawString(title, f, TitleBrush, new PointF(r.X, r.Y));

        if (tabControl2.SelectedIndex >= 1)
        {
            e.Graphics.DrawImage(img, new Point(r.X + (this.tabControl2.GetTabRect(e.Index).Width - _imageLocation.X), _imageLocation.Y));
        }

    }
        catch (Exception) { }
}

    private void tabControl2_MouseClick(object sender, MouseEventArgs e)
    {
        TabControl tc = (TabControl)sender;
        Point p = e.Location;
        int _tabWidth = 0;
        _tabWidth = this.tabControl2.GetTabRect(tc.SelectedIndex).Width - (_imgHitArea.X);
        Rectangle r = this.tabControl2.GetTabRect(tc.SelectedIndex);
        r.Offset(_tabWidth, _imgHitArea.Y);
        r.Width = 16;
        r.Height = 16;
        if (tabControl2.SelectedIndex >= 1)
        {
            if (r.Contains(p))
            {
                TabPage TabP = (TabPage)tc.TabPages[tc.SelectedIndex];
                tc.TabPages.Remove(TabP);

            }
        }
    }



但是,当我设置的属性 RightToLeftLayout = TRUE 从右至左= TRUE 这是行不通的,的TabPage 标题不出现,也关闭按钮。

But when I set the property RightToLeftLayout = true and RightToLeft = true it doesn't work, TabPage titles don't appear and also close button.

因此,如何修复代码中接受的方式从右至左属性?

So how to fix the code in a way that accepts RightToLeft property?

推荐答案

要RTL的坐标,您可以创建一个函数来转换一个矩形的坐标集装箱:

You can create a function to translate coordinates of a rectangle to RTL coordinates in a container:

public static Rectangle GetRTLCoordinates(Rectangle container, Rectangle drawRectangle)
{
    return new Rectangle(
        container.Width - drawRectangle.Width - drawRectangle.X,
        drawRectangle.Y,
        drawRectangle.Width,
        drawRectangle.Height);
}



而在RTL模式绘制时,计算坐标,是这样的:

And when painting in RTL mode, calculate coordinates this way:

tabRect = GetRTLCoordinates(this.tabControl2.ClientRectangle, tabRect);



此外,你应该使用绘制字符串中的的StringFormat 并将其设置为使用 StringFormatFlags.DirectionRightToLeft 当你在RTL模式,并使用字符串格式转换的矩形绘制字符串:

Also you should draw your strings using an StringFormat and set it to use StringFormatFlags.DirectionRightToLeft when you are in RTL mode and draw string in the translated rectangle using the string format:

e.Graphics.DrawString(this.tabControl2.TabPages[e.Index].Text, 
                      this.Font, Brushes.Black, tabRect, sf);

您可以封装在所有代码的 CustomTabControl 继承的TabControl

You can encapsulate all codes in a CustomTabControl inheriting TabControl.

屏幕截图


整代码可以是:

我假设你有一个近距离图像像 Properties.Default.Close 某处并将其分配给 this.CloseImage 。下面是我使用的图像:

I suppose you have a close image somewhere like Properties.Default.Close and assign it to this.CloseImage. Here is the image I used:

我还设置了 this.tabControl2.Padding =新点(10,3); 来绘制图像提供更多的自由空间。

I also set the this.tabControl2.Padding = new Point(10, 3); to provide additional free space for drawing the image.

你也可以简单地添加不打烊首先您的标准。 。标签

Also you can simply add your criteria of not closing first tab.

Image CloseImage;

private void Form1_Load(object sender, EventArgs e)
{
    this.tabControl2.DrawMode = System.Windows.Forms.TabDrawMode.OwnerDrawFixed;
    tabControl2.DrawItem += TabControl2_DrawItem;
    tabControl2.MouseClick += tabControl2_MouseClick;
    CloseImage = Properties.Resources.Close;
    this.tabControl2.Padding = new Point(10, 3);
}


private void TabControl2_DrawItem(object sender, 
                                  System.Windows.Forms.DrawItemEventArgs e)
{
    try
    {
        var tabRect = this.tabControl2.GetTabRect(e.Index);
        tabRect.Inflate(-2, -2);
        var imageRect = new Rectangle(tabRect.Right - CloseImage.Width,
                                 tabRect.Top + (tabRect.Height - CloseImage.Height) / 2,
                                 CloseImage.Width,
                                 CloseImage.Height);

        var sf = new StringFormat(StringFormat.GenericDefault);
        if (this.tabControl2.RightToLeft == System.Windows.Forms.RightToLeft.Yes &&
            this.tabControl2.RightToLeftLayout == true)
        {
            tabRect = GetRTLCoordinates(this.tabControl2.ClientRectangle, tabRect);
            imageRect = GetRTLCoordinates(this.tabControl2.ClientRectangle, imageRect);
            sf.FormatFlags |= StringFormatFlags.DirectionRightToLeft;
        }

        e.Graphics.DrawString(this.tabControl2.TabPages[e.Index].Text,
                              this.Font, Brushes.Black, tabRect, sf);
        e.Graphics.DrawImage(CloseImage, imageRect.Location);

    }
    catch (Exception) { }
}

private void tabControl2_MouseClick(object sender, MouseEventArgs e)
{

    for (var i = 0; i < this.tabControl2.TabPages.Count; i++)
    {
        var tabRect = this.tabControl2.GetTabRect(i);
        tabRect.Inflate(-2, -2);
        var imageRect = new Rectangle(tabRect.Right - CloseImage.Width,
                                 tabRect.Top + (tabRect.Height - CloseImage.Height) / 2,
                                 CloseImage.Width,
                                 CloseImage.Height);
        if (imageRect.Contains(e.Location))
        {
            this.tabControl2.TabPages.RemoveAt(i);
            break;
        }
    }
}

public static Rectangle GetRTLCoordinates(Rectangle container, Rectangle drawRectangle)
{
    return new Rectangle(
        container.Width - drawRectangle.Width - drawRectangle.X,
        drawRectangle.Y,
        drawRectangle.Width,
        drawRectangle.Height);
}

这篇关于对于权的TabPages到左的TabControl C#关闭按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆