2010-06-29 134 views
2

我想要使用选项卡控件,并在左侧显示选项卡,而不是在顶部。我已将对齐设置在左侧,并且标签显示在那里。但是,如何让文本垂直显示在选项卡上?我已经看过msdn,它给出了一个左对齐的选项卡控件的示例,但标签标签仍然是水平显示的!选项卡控件,与垂直对齐文本垂直对齐的选项卡

另一件事,是否有人知道如何使用与默认布局的左对齐选项卡的选项卡控件,以便它看起来更好?

请不要第三方应用程序,除非他们是免费的,是的,我已经看过代码项目。

感谢,R.

回答

3

它在视觉样式渲染器本机Windows标签控件一个古老的错误。它只支持顶部的选项卡,我猜,微软的程序员在完成这项工作之前已经由公交车运行了。

您可以做的唯一一件事是选择性地关闭控件的视觉样式。为您的项目添加一个新类并粘贴下面显示的代码。编译。将新控件从工具箱的顶部放到表单上,替换原来的。

using System; 
using System.Windows.Forms; 
using System.Runtime.InteropServices; 

public class FixedTabControl : TabControl { 
    [DllImportAttribute("uxtheme.dll")] 
    private static extern int SetWindowTheme(IntPtr hWnd, string appname, string idlist); 

    protected override void OnHandleCreated(EventArgs e) { 
     SetWindowTheme(this.Handle, "", ""); 
     base.OnHandleCreated(e); 
    } 
} 
+0

太棒了。现在我只需要重新设置它...... :) – flavour404 2010-06-30 18:17:22

+0

汉斯,请你帮忙,告诉我如何重新安装它?我想使它看起来像默认的选项卡控件。任何建议的帮助将不胜感激,因为这个东西是新的,我觉得我正在围绕圈子,或谷歌圈无论如何... ... – flavour404 2010-06-30 20:33:11

1

如果您创建自己的DrawItem事件,则可以手动编写选项卡标题。您可以使用此过程:

1)设定的TabControl的以下属性:

Property | Value 
----------|---------------- 
Alignment | Right (or left, depending on what you want) 
SizeMode | Fixed 
DrawMode | OwnerDrawFixed 

2)ItemSize.Width属性设置为25和ItemSize.Height财产100调整这些值你想要的,但请记住,基本上,宽度是高度,反之亦然。

3)添加的事件处理DrawItem事件,并添加以下代码:

private void tabControl1_DrawItem(object sender, DrawItemEventArgs e) 
{ 
    Graphics g = e.Graphics; 
    Brush _TextBrush; 

    // Get the item from the collection. 
    TabPage _TabPage = tabControl1.TabPages[e.Index]; 

    // Get the real bounds for the tab rectangle. 
    Rectangle _TabBounds = tabControl1.GetTabRect(e.Index); 

    if(e.State == DrawItemState.Selected) 
    { 
    // Draw a different background color, and don't paint a focus rectangle. 
    _TextBrush = new SolidBrush(Color.Red); 
    g.FillRectangle(Brushes.Gray, e.Bounds); 
    } 
    else 
    { 
    _TextBrush = new System.Drawing.SolidBrush(e.ForeColor); 
    e.DrawBackground(); 
    } 

    // Use our own font. Because we CAN. 
    Font _TabFont = new Font("Arial", 10, FontStyle.Bold, GraphicsUnit.Pixel); 

    // Draw string. Center the text. 
    StringFormat _StringFlags = new StringFormat(); 
    _StringFlags.Alignment = StringAlignment.Center; 
    _StringFlags.LineAlignment = StringAlignment.Center; 
    g.DrawString(_TabPage.Text, _TabFont, _TextBrush, 
       _TabBounds, new StringFormat(_StringFlags)); 
} 

4)利润!

(原始来源:http://en.csharp-online.net/TabControl