gpt4 book ai didi

c# - 选项卡高度不反射(reflect)自定义/用户绘画 TabControl 上的高 DPI

转载 作者:行者123 更新时间:2023-12-05 05:09:38 24 4
gpt4 key购买 nike

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

enter image description here

显然,TabControl 没有调整选项卡高度以适应较大的字体,因此,实际页面的顶部太高并盖住了我的选项卡。我可以做些什么来强制选项卡进行调整?

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

这是我的自定义 TabControl 代码:

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

最佳答案

我找到了解决方案。在 OnCreateControl() 中,添加:

ItemSize = new Size(ItemSize.Width, ItemSize.Height * DpiRatio);

其中 DpiRatio 是比例因子(例如 2 表示 200% 比例)。

关于c# - 选项卡高度不反射(reflect)自定义/用户绘画 TabControl 上的高 DPI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57443481/

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