首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >选项卡高度不反映自定义/用户画图TabControl上的高DPI值

选项卡高度不反映自定义/用户画图TabControl上的高DPI值
EN

Stack Overflow用户
提问于 2019-08-10 15:33:48
回答 1查看 443关注 0票数 3

我已经编写了一个自定义的TabControl类,但是我无法使选项卡适应它们在高DPI屏幕上的高度。在200%缩放的屏幕上,实际选项卡页及其控件覆盖了一半,如下所示:

显然,TabControl没有调整标签高度以适应更大的字体,因此,实际页面的顶部太高,覆盖了我的选项卡。我能做些什么来加强标签来适应呢?

表单将AutoScaleMode设置为Dpi,其他一切看起来都很好,除了这个。我的目标是.NET 4.5.2,dpiAware设置在清单文件中设置为true。

以下是自定义TabControl的代码:

代码语言:javascript
复制
/// <summary>
/// A TabControl without 3D borders and other annoyances. Taken from
/// https://stackoverflow.com/questions/27469886/change-color-of-unused-space-of-tabcontrol/27472230
/// and modified.
/// </summary>
public class CleanTabControl : TabControl
{
    private class NativeMethods
    {
        [DllImport("user32.dll")] public static extern IntPtr SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);
    }
    private const int WM_SETFONT = 0x30;
    private const int WM_FONTCHANGE = 0x1d;

    private int hoverTab = -1;

    public event MouseEventHandler CloseClick;

    public CleanTabControl()
    {
        // Take over the painting completely, we want transparency and double-buffering
        SetStyle(ControlStyles.UserPaint | ControlStyles.SupportsTransparentBackColor, true);
        DoubleBuffered = ResizeRedraw = true;
    }

    protected override void OnCreateControl()
    {
        // Necessary to give tabs the correct width
        base.OnCreateControl();
        OnFontChanged(EventArgs.Empty);
    }

    protected override void OnFontChanged(EventArgs e)
    {
        // Necessary to give tabs the correct width
        base.OnFontChanged(e);
        IntPtr hFont = Font.ToHfont();
        NativeMethods.SendMessage(Handle, WM_SETFONT, hFont, (IntPtr)(-1));
        NativeMethods.SendMessage(Handle, WM_FONTCHANGE, IntPtr.Zero, IntPtr.Zero);
        UpdateStyles();
    }

    public override Color BackColor
    {
        // Override TabControl.BackColor, we need transparency
        get { return Color.Transparent; }
        set { base.BackColor = Color.Transparent; }
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        // ... lot of painting code
    }

    protected override void OnMouseClick(MouseEventArgs e)
    {
        base.OnMouseClick(e);
        if (SelectedTab != null)
        {
            if (GetImageRectangle(SelectedIndex).Contains(e.Location))
                CloseClick(this, e);
        }
    }

    protected override void OnMouseMove(MouseEventArgs e)
    {
        base.OnMouseMove(e);
        hoverTab = -1;
        for (int i = 0; i < TabCount; i++)
        {
            if (GetTabRect(i).Contains(e.Location))
            {
                if (GetImageRectangle(i).Contains(e.Location))
                    TabPages[i].ImageIndex = 1;
                else
                {
                    hoverTab = i;
                    TabPages[i].ImageIndex = 0;
                }
            }
            else
                TabPages[i].ImageIndex = 0;
        }
        Invalidate();
    }

    protected override void OnMouseLeave(EventArgs e)
    {
        base.OnMouseLeave(e);
        hoverTab = -1;
        for (int i = 0; i < TabCount; i++)
        {
            TabPages[i].ImageIndex = 0;
        }
        Invalidate();
    }

    private Rectangle GetImageRectangle(int index)
    {
        Rectangle r = GetTabRect(index);
        int width = ImageList.ImageSize.Width;
        int height = ImageList.ImageSize.Height;
        int x = r.Right - width - Padding.X;
        int y = (r.Top + r.Height - height) / 2 + 1;
        if (index != SelectedIndex)
            y += 1;
        return new Rectangle(x, y, width, height);
    }
}

}

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-08-11 20:39:16

我找到了解决办法。在OnCreateControl()中添加:

代码语言:javascript
复制
ItemSize = new Size(ItemSize.Width, ItemSize.Height * DpiRatio);

其中,DpiRatio是标度因子(例如,200%缩放的2)。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57443481

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档