C# 重绘tabControl,添加关闭按钮(续)
在上一篇随笔中,添加关闭按钮是可以实现 ,但细心一点就会发现,每次关闭一个选项卡,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,添加关闭按钮(续)的更多相关文章
- C# 重绘tabControl,添加关闭按钮(页签)
C# 重绘tabControl,添加关闭按钮(页签) 调用方法 参数: /// <summary> /// 初始化 /// </summary> /// <param n ...
- C#重绘TabControl
C#重绘TabControl的Tabpage标签,添加图片及关闭按钮 Code highlighting produced by Actipro CodeHighlighter (freeware)h ...
- WinForm中重绘TabControl选项卡标题
最近开发WinForm频繁使用了TabControl控件,这个控件的选项卡没有BackgroundImage这个属性,那么如何为其各个选项卡添加背景图片呢?(这里说的是每个TabPage的头部,也就是 ...
- 重绘TabControl
本文转载自:http://blog.csdn.net/conmajia/article/details/7596718 作者:野比 (conmajia@gmail.com) 时间:May, 2012 ...
- C# 自定义重绘TabControl
using System.Drawing; using System.Windows.Forms; using System.Drawing.Drawing2D; using System.Runti ...
- TabControl控件重绘
原文地址:http://www.codeproject.com/Articles/91387/Painting-Your-Own-Tabs-Second-Edition 在网上看到重绘TabContr ...
- Windows开发进阶之VC++中如何实现对话框的界面重绘
技术:Windows 系统+Visual studio 2008 概述 应用程序界面是用户与应用程序之间的交互的桥梁和媒介,用户界面是应用程序中最重要的组成部分,也是最为直观的视觉体现.对用户而言 ...
- c#winform自定义窗体,重绘标题栏,自定义控件学习
c#winform自定义窗体,重绘标题栏 虽然现在都在说winform窗体太丑了,但是我也能尽量让桌面应用程序漂亮那么一点点话不多说,先上图 重绘标题栏先将原生窗体设置成无边框,FormBoderSt ...
- 关于DOM的操作以及性能优化问题-重绘重排
写在前面: 大家都知道DOM的操作很昂贵. 然后贵在什么地方呢? 一.访问DOM元素 二.修改DOM引起的重绘重排 一.访问DOM 像书上的比喻:把DOM和JavaScript(这里指ECMScri ...
随机推荐
- MySQL客户端Workbench
MySQL客户端除了Navicat之外,还有官方推出的MySQL Workbench,能看到数据库包含的存储过程,而Navicate不能. 下载链接: 32位:http://cdn.mysql.com ...
- 自带openJDK,如何切换成Oracle JDK
1.去Oracle官网下载最新的jdk,解压,配置环境变量(直接改 /etc/profile) 参考:http://www.cnblogs.com/aaronhoo/p/5293118.html 2. ...
- iOS学习笔记(2)--Xcode6.1创建仅xib文件无storyboard的hello world应用
http://www.mamicode.com/info-detail-514151.html 由于Xcode6之后,默认创建storyboard而非xib文件,而作为初学,了解xib的加载原理很重要 ...
- How to keep Environment Variables when Using SUDO
The trick is to add environment variables to sudoers file via sudo visudo command and add these line ...
- C# 毕业证书打印《一》
最近一直在做证书打印的项目,好久都没写日志了.今天将代码整理了一下,希望将自己做证书打印的一些心得写出来,也希望能和大家一起交流. 首先,证书打印必须实现打印的功能.了解打印功能是怎么实现的,打印关键 ...
- How do I get ASP.NET Web API to return JSON instead of XML using Chrome
public static class WebApiConfig { public static void Register(HttpConfiguration config) { config.Ro ...
- IPC---信号量
一.什么是信号量 信号量的使用主要是用来保护共享资源,使得资源在一个时刻只有一个进程(线程) 所拥有. 信号量的值为正的时候,说明它空闲.所测试的线程可以锁定而使用它.若为0,说明 它被占用,测试的线 ...
- ACM/ICPC 之 DFS求解欧拉通路路径(POJ2337)
判断是欧拉通路后,DFS简单剪枝求解字典序最小的欧拉通路路径 //Time:16Ms Memory:228K #include<iostream> #include<cstring& ...
- 算法手记 之 数据结构(堆)(POJ 2051)
一篇读书笔记 书籍简评:<ACM/ICPC 算法训练教程>这本书是余立功主编的,代码来自南京理工大学ACM集训队代码库,所以小编看过之后发现确实很实用,适合集训的时候刷题啊~~,当时是听了 ...
- Mac会给你一些欣喜
Mac会给你一些欣喜 以前一直没有用过Mac,一直都是用Windows的电脑,只是偶尔会去用Ubuntu这样的Linux系统.Mac OS 确实是一只可以给你欣喜的系统. 上周拿到公司分发的Mac,到 ...