gpt4 book ai didi

c# - Windows 窗体中的自定义选项卡控件

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

我在 Windows XP 和 Windows 7 中遇到过选项卡控件的奇怪行为。在 XP 中,位于选项卡上的文本被换行,而在 7 中则没有。标签怎么了? enter image description here

附言。非英文字母被包裹

更新:这是一个自定义选项卡控件

代码如下:

protected override void OnPaint(PaintEventArgs e)
{
DrawControl(e.Graphics);
}

internal void DrawControl(Graphics g)
{
if (!Visible)
return;

Brush br = new SolidBrush(clr_cl_area);
Brush brTab = new SolidBrush(clr_client);

Rectangle TabControlArea = ClientRectangle;
Rectangle TabArea = DisplayRectangle;

g.FillRectangle(br, TabControlArea);
g.FillRectangle(brTab, TabArea);

br.Dispose();
brTab.Dispose();

for (int i = 0; i < TabCount; i++)
DrawTab(g, TabPages[i], i, false);

if (_mouseTabIndex != null && _mouseTabIndex != _mouseTabIndexSave && _mouseTabIndex != SelectedIndex)
DrawTab(g, TabPages[(int)_mouseTabIndex], (int)_mouseTabIndex, true);

_mouseTabIndexSave = _mouseTabIndex;

}

internal void DrawTab(Graphics g, TabPage tabPage, int nIndex, bool mouseOverTab)
{

var recBounds = GetTabRect(nIndex);


SetBounds(ref recBounds);
var pt = SetPointsForTabFill(recBounds);


DrawTabBounds(g, recBounds);


FillTabl(g, recBounds, pt, false);


DrawTabSeparators(g, recBounds, nIndex, 0 /*y-bottom*/);


if (SelectedIndex == nIndex)
{
DrawTabGradient(g, recBounds, pt, nIndex,0/*width*/,1/*height*/);
DrawTabSeparators(g, recBounds, nIndex, 1 /*y-bottom*/);
}

if (mouseOverTab)
DrawTabGradient(g, recBounds, pt, nIndex, -2/*width*/, 0/*height*/);

DrawText(g, recBounds, tabPage.Text);

}

private void DrawText(Graphics g, Rectangle recBounds, string text)
{
var strFormat = new StringFormat();
strFormat.Alignment = strFormat.LineAlignment = StringAlignment.Center;

g.TextRenderingHint =
System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;

//var fnt = new Font(MsFonts.familyPTSans, 8F, FontStyle.Regular, GraphicsUnit.Point, (byte)204);
var fnt = new Font("Arial", 8.25F, FontStyle.Regular, GraphicsUnit.Point, ((byte)(204)));

RectangleF tabTextArea = recBounds;
var br = new SolidBrush(clr_txt);
g.DrawString(text, fnt, br, tabTextArea, FormatString());

br.Dispose();
strFormat.Dispose();
fnt.Dispose();
}

最佳答案

为什么会这样?我不确定...我相信该框架仅使用底层的 Windows API,因此如果 Windows 版本之间存在差异,.Net 应用程序的外观/行为可能会略有不同。我想知道某处是否有文化/字符串行为设置可以解决这个问题......

无论如何,这是我对你的问题的草率但有效的解决方案:只需在选项卡的文本中插入一个换行符(我不知道你是否可以从设计器中这样做)。您可以通过自己的 C# 代码或设计人员的代码隐藏 C# 执行此操作。您还必须使标签尺寸更大,以容纳额外的文本行。

        // Set the size of the tabs.  I multiply the default height by 2 for 2 lines
tabControl1.ItemSize = new System.Drawing.Size(77, 18 * 2);

// Force a line break within the string
tabControl1.Controls[0].Text = "Hello\r\nWorld";

关于c# - Windows 窗体中的自定义选项卡控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7416062/

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