2010-06-29 121 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