开发者

tab control, vertically aligned tabs with vertically aligned text

I want to use a tab control and have the tabs displayed on the left, as opposed to at the top. I have set the alignment to the left and the tabs are displayed there. However, how do I get the text to be displayed on the tab, vertically? I have looked at the msdn and it gives an example of a left aligned tab control but the tab label is still displayed horisontally!

The other thing, does anybody know how to use the tab control with left aligned tabs with the default layout so that it looks better?

开发者_运维技巧Please no third party apps unless they are free, and yes I have looked at code project already.

Thanks, R.


It is an age-old bug in the visual styles renderer for the native Windows tab control. It only supports tabs at the top, the Microsoft programmer that worked on it was run over by a bus before she could finish the job, I guess.

The only thing you can do about it is selectively turn off visual styles for the control. Add a new class to your project and paste the code shown below. Compile. Drop the new control from the top of the toolbox onto your form, replacing the original.

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


If you create your own DrawItem event, you can manually write the tab headers. You could use this process:

1) Set the following properties of the TabControl:

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

2) Set the ItemSize.Width property to 25 and the ItemSize.Height property to 100. Adjust these values as you want, but remember that basically, the width is the height and vice versa.

3) Add an event handler for the DrawItem event and add the following code:

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) Profit!

(original source: http://en.csharp-online.net/TabControl)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜