在上一篇随笔中,添加关闭按钮是可以实现 ,但细心一点就会发现,每次关闭一个选项卡,tableControl都会自动跳到第一个页面,显然 这不是我们想要的,为此,我修改了部分的代码。除此之外,我还添加了一些两个新的方法,用于创建新的tablePage.以下是我实现 的类

 #region 重绘tablecontrol的类(添加关闭页面功能),ablepage的创建,以及窗体的附加
public class DrawTabControl
{
TabControl tabControl1 = null;
Font font1 = null; //打开和关闭的页面索引
int tabindex_show = ;
int tabindex_close = ; public DrawTabControl() { }
/// <summary>
/// 初始化
/// </summary>
/// <param name="tabcontrol">TacControl控件</param>
/// <param name="fo">主程序this.Font</param>
public DrawTabControl(TabControl tabcontrol,Font fo)
{
tabControl1 = tabcontrol;
font1 = fo;
} #region 关于tabcontrol的重绘 const int CLOSE_SIZE = ;//大小
//tabPage标签图片
Bitmap image = new Bitmap("D:\\power_003.png"); public void ClearPage()
{ //清空控件
//this.MainTabControl.TabPages.Clear();
//绘制的方式OwnerDrawFixed表示由窗体绘制大小也一样
this.tabControl1.DrawMode = TabDrawMode.OwnerDrawFixed;
this.tabControl1.Padding = new System.Drawing.Point(CLOSE_SIZE, CLOSE_SIZE - );
this.tabControl1.DrawItem += new DrawItemEventHandler(this.tabControl1_DrawItem);
this.tabControl1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.tabControl1_MouseDown);
} /// <summary>
/// 产生新的窗体时触发
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tabControl1_DrawItem(object sender, DrawItemEventArgs e)
{
try
{
Rectangle myTabRect = this.tabControl1.GetTabRect(e.Index); //先添加TabPage属性
e.Graphics.DrawString(this.tabControl1.TabPages[e.Index].Text, this.font1, SystemBrushes.ControlText, myTabRect.X + , myTabRect.Y + ); //再画一个矩形框
using (Pen p = new Pen(Color.White))
{
myTabRect.Offset(myTabRect.Width - (CLOSE_SIZE + ), );
myTabRect.Width = CLOSE_SIZE;
myTabRect.Height = CLOSE_SIZE;
e.Graphics.DrawRectangle(p, myTabRect);
} ////填充矩形框
Color recColor = e.State == DrawItemState.Selected ? Color.White : Color.White;
using (Brush b = new SolidBrush(recColor))
{
e.Graphics.FillRectangle(b, myTabRect);
} //画关闭符号
using (Pen objpen = new Pen(Color.Black))
{
////=============================================
//自己画X
////"\"线
Point p1 = new Point(myTabRect.X + , myTabRect.Y + );
Point p2 = new Point(myTabRect.X + myTabRect.Width - , myTabRect.Y + myTabRect.Height - );
e.Graphics.DrawLine(objpen, p1, p2);
////"/"线
Point p3 = new Point(myTabRect.X + , myTabRect.Y + myTabRect.Height - );
Point p4 = new Point(myTabRect.X + myTabRect.Width - , myTabRect.Y + );
e.Graphics.DrawLine(objpen, p3, p4); ////=============================================
//使用图片
//Bitmap bt = new Bitmap(image);
//Point p5 = new Point(myTabRect.X, 4);
//e.Graphics.DrawImage(bt, p5);
//e.Graphics.DrawString(this.MainTabControl.TabPages[e.Index].Text, this.font1, objpen.Brush, p5);
}
e.Graphics.Dispose();
}
catch (Exception)
{ } } /// <summary>
/// 鼠标点击选项卡时的触发
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tabControl1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{ int x = e.X, y = e.Y;
//计算关闭区域
Rectangle myTabRect = this.tabControl1.GetTabRect(this.tabControl1.SelectedIndex); myTabRect.Offset(myTabRect.Width - (CLOSE_SIZE + ), );
myTabRect.Width = CLOSE_SIZE;
myTabRect.Height = CLOSE_SIZE; //如果鼠标在区域内就关闭选项卡,如果不在就显示选项卡
//如果关闭的选项卡正好打开着,则关闭当前先项卡,显示下一个选项卡
bool isClose = x > myTabRect.X && x < myTabRect.Right && y > myTabRect.Y && y < myTabRect.Bottom;
if (isClose == true)
{
//判断关闭的页面索引是否比正在显示的页面的索引
//如果关闭的页面的索引大于或等于显示正在显示的页面索引,刚tabindex_close的值为关闭的页面的索引;
//相反 的,刚tabindex_close则为tabindex_show-1; tabindex_close = this.tabControl1.SelectedIndex >= tabindex_show ? this.tabControl1.SelectedIndex : tabindex_close - ;
this.tabControl1.TabPages.Remove(this.tabControl1.SelectedTab);
}
else
{
tabindex_close = this.tabControl1.SelectedIndex;
tabindex_show = this.tabControl1.SelectedIndex;
}
if (tabindex_close < tabindex_show)
{
tabindex_show = tabindex_show - ; }
//显示页面
try { this.tabControl1.SelectedTab = this.tabControl1.TabPages[tabindex_show]; }
catch (Exception ex) { } Console.WriteLine("显示页面" + tabindex_show + "关闭页面" + tabindex_close); }
}
#endregion #region 关于tablepage的创建,以及窗体的附加 /// <summary>
/// 创建新的选项卡
/// </summary>
/// <param name="ParentForm">父窗体</param>
/// <param name="ChildForm">子窗体</param>
/// <param name="Pagename">页面名称</param>
public void CreatePage(Form ParentForm,Form ChildForm, string Pagename)
{ ChildForm.MdiParent = ParentForm;
ChildForm.Text = "第二个窗体";
ChildForm.Dock = DockStyle.Fill;
ChildForm.TopMost = true;
ChildForm.FormBorderStyle = FormBorderStyle.None;
TabPage tb1 = AddPage(Pagename);
tb1.Controls.Add(ChildForm);
this.tabControl1.Controls.Add(tb1);
ChildForm.Show();
}
/// <summary>
/// 添加选项卡
/// </summary>
/// <param name="PageName">页面名称</param>
/// <returns>一个页面</returns>
public TabPage AddPage(string PageName)
{
TabPage tabPage = new TabPage();
tabPage.Location = new System.Drawing.Point(, );
tabPage.Name = PageName;
tabPage.Padding = new System.Windows.Forms.Padding();
tabPage.Size = new System.Drawing.Size(, );
tabPage.Text = PageName;
tabPage.UseVisualStyleBackColor = true;
return tabPage;
} #endregion }
#endregion

调用的方法,和上一篇的一样,

DrawTabControl dtc=null;
dtc=new DrawTabControl(tabControl1,this.Font);
dtc.ClearPage();

上面的代码是实例化了一个类,并调用ClearPage方法,注意,

1、在添加选项卡之前一定要调用ClearPage()这个方法;

2、this.Font 解释为:System.Drawing.Font,它定义字符串的文本格式。具体作用还不太清楚,如果有懂的朋友,还望帮忙讲解一下;

Form3 fo3 = new Form3();
dtc.CreatePage(this,fo3, "窗体");

上面的代码 为创建选项卡,其中this为父窗体 ,fo3为子窗体 ,第三个参数为窗体名

C# 重绘tabControl,添加关闭按钮(续)的更多相关文章

  1. C# 重绘tabControl,添加关闭按钮(页签)

    C# 重绘tabControl,添加关闭按钮(页签) 调用方法 参数: /// <summary> /// 初始化 /// </summary> /// <param n ...

  2. C#重绘TabControl

    C#重绘TabControl的Tabpage标签,添加图片及关闭按钮 Code highlighting produced by Actipro CodeHighlighter (freeware)h ...

  3. WinForm中重绘TabControl选项卡标题

    最近开发WinForm频繁使用了TabControl控件,这个控件的选项卡没有BackgroundImage这个属性,那么如何为其各个选项卡添加背景图片呢?(这里说的是每个TabPage的头部,也就是 ...

  4. 重绘TabControl

    本文转载自:http://blog.csdn.net/conmajia/article/details/7596718 作者:野比 (conmajia@gmail.com) 时间:May, 2012 ...

  5. C# 自定义重绘TabControl

    using System.Drawing; using System.Windows.Forms; using System.Drawing.Drawing2D; using System.Runti ...

  6. TabControl控件重绘

    原文地址:http://www.codeproject.com/Articles/91387/Painting-Your-Own-Tabs-Second-Edition 在网上看到重绘TabContr ...

  7. Windows开发进阶之VC++中如何实现对话框的界面重绘

    技术:Windows 系统+Visual studio 2008   概述 应用程序界面是用户与应用程序之间的交互的桥梁和媒介,用户界面是应用程序中最重要的组成部分,也是最为直观的视觉体现.对用户而言 ...

  8. c#winform自定义窗体,重绘标题栏,自定义控件学习

    c#winform自定义窗体,重绘标题栏 虽然现在都在说winform窗体太丑了,但是我也能尽量让桌面应用程序漂亮那么一点点话不多说,先上图 重绘标题栏先将原生窗体设置成无边框,FormBoderSt ...

  9. 关于DOM的操作以及性能优化问题-重绘重排

     写在前面: 大家都知道DOM的操作很昂贵. 然后贵在什么地方呢? 一.访问DOM元素 二.修改DOM引起的重绘重排 一.访问DOM 像书上的比喻:把DOM和JavaScript(这里指ECMScri ...

随机推荐

  1. 1.7---将矩阵元素为0的行列清零0(CC150)

    答案: import java.util.ArrayList; import java.util.List; public class Solution{ public static void mai ...

  2. PHP程序员如何突破成长瓶颈(php开发三到四年)

    看了这篇博文,我正好处于这个阶段,也有心要突破自己,呵呵! 作为Web开发中应用最广泛的语言之一,PHP有着大量的粉丝,那么你是一名优秀的程序员吗?在进行自我修炼的同时,你是否想过面对各种各样的问题, ...

  3. php中的钩子(hook插件机制)

    对"钩子"这个概念其实不熟悉,最近看到一个php框架中用到这种机制来扩展项目,所以大概来了解下. hook插件机制的基本思想: 在项目代码中,你认为要扩展(暂时不扩展)的地方放置一 ...

  4. poj 2153

    题意:题目还是很简单的,就是求Li Ming 在班上的排名,而且成绩是相加的. 思路:用map就行.不然好像用qsort+二分也可以,不过我在那里碰到了一些状况,然后就没用这种方法了,简单的map就可 ...

  5. 【转】.so兼容32位和64位

    本文转自:http://blog.csdn.net/fwt336/article/details/51700300 安卓的兼容性是一个很令人头疼的问题,这几天又遇到了,还好还是解决了. 我遇到的问题是 ...

  6. UESTC 250

    windy数 基本的数位DP,需要判断当前位是否为起始位. #include <cstdio> #include <cmath> #include <cstring> ...

  7. win8开发wpf程序遇到的无语问题

    在设置wpf程序全屏后,点击某个listbox列表,发现程序下面出现了任务栏. 查找解决答案未果.仔细一想可能是win8系统的问题. 最后试着把listbox的滚动条去掉了,问题解决. 原因:当程序中 ...

  8. centos6.5kvm虚拟化安装部署

    一.走进云计算 云计算:云计算是一种按使用量付费的模式,这种模式提供可用的.便捷的.按需的网络访问, 进入可配置的计算资源共享池(资源包括网络,服务器,存储,应用软件,服务),这些资源能够被快速提供, ...

  9. JPA查询语句(转载)

    JPQL就是一种查询语言,具有与SQL 相类似的特征,JPQL是完全面向对象的,具备继承.多态和关联等特性,和hibernate HQL很相似.   查询语句的参数 JPQL语句支持两种方式的参数定义 ...

  10. 在微信浏览器中如何让他自动关闭当前页面回到会话框js

    <script type="text/javascript"> wx.config(jssdkconfig); require(['jquery', 'util'], ...