winform如何用tabControl控件实现纵向选项卡效果?
将TabControl 的Alignment 改成Left 或者 Right 就可以了 左右两侧的纵向选项卡
改成left/right后选项卡的文字显示任然有问题
需要自己重写以下drawitem方法,如下:
View Code
1 using
System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Linq;
7 using System.Text;
8 using System.Windows.Forms;
9
10 namespace WinForm
11 {
12 public partial class Form1 : Form
13 {
14 public Form1()
15 {
16 InitializeComponent();
17 }
18
19 private void Form1_Load(object sender, EventArgs e)
20 {
21 //设置DrawMode 为 OwnerDrawFixed 可以再可视化编辑里设置
22 this.tabControl1.DrawMode = System.Windows.Forms.TabDrawMode.OwnerDrawFixed;
23
24 //设置Alignment 为 Left/Right 可以再可视化编辑里设置
25 this.tabControl1.Alignment = System.Windows.Forms.TabAlignment.Left;
26
27 //将tabcontrol的drawitem 重写 交给自己写的DrawItem方法
28 this.tabControl1.DrawItem += new System.Windows.Forms.DrawItemEventHandler
29
30 (this .tabControl1_DrawItem);
31
32 }
33 private void tabControl1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
34 {
35 StringFormat sf = new StringFormat();
36
37 // 设置文字是居中的
38 sf.LineAlignment = StringAlignment.Center;
39 sf.Alignment = StringAlignment.Center;
40 //画出选项卡文字
41
42 e.Graphics.DrawString(((TabControl)sender).TabPages[e.Index].Text,
43 System.Windows.Forms.SystemInformation.MenuFont,
44 new SolidBrush(Color.Black),
45 e.Bounds,
46 sf);
47 }
48 }
49 }
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Linq;
7 using System.Text;
8 using System.Windows.Forms;
9
10 namespace WinForm
11 {
12 public partial class Form1 : Form
13 {
14 public Form1()
15 {
16 InitializeComponent();
17 }
18
19 private void Form1_Load(object sender, EventArgs e)
20 {
21 //设置DrawMode 为 OwnerDrawFixed 可以再可视化编辑里设置
22 this.tabControl1.DrawMode = System.Windows.Forms.TabDrawMode.OwnerDrawFixed;
23
24 //设置Alignment 为 Left/Right 可以再可视化编辑里设置
25 this.tabControl1.Alignment = System.Windows.Forms.TabAlignment.Left;
26
27 //将tabcontrol的drawitem 重写 交给自己写的DrawItem方法
28 this.tabControl1.DrawItem += new System.Windows.Forms.DrawItemEventHandler
29
30 (this .tabControl1_DrawItem);
31
32 }
33 private void tabControl1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
34 {
35 StringFormat sf = new StringFormat();
36
37 // 设置文字是居中的
38 sf.LineAlignment = StringAlignment.Center;
39 sf.Alignment = StringAlignment.Center;
40 //画出选项卡文字
41
42 e.Graphics.DrawString(((TabControl)sender).TabPages[e.Index].Text,
43 System.Windows.Forms.SystemInformation.MenuFont,
44 new SolidBrush(Color.Black),
45 e.Bounds,
46 sf);
47 }
48 }
49 }