using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Globalization;
using System.Windows.Forms; namespace KUN.CONTROL.LIB.UC
{
public partial class KFillTime : UserControl
{
#region 属性定义
[Browsable(true)]
[Description("字体"), Category("自定义属性")]
private Font timeFont = new Font("微软雅黑", 10, FontStyle.Bold);
public Font TimeFont
{
get { return this.timeFont; }
set { this.timeFont = value; }
}
[Browsable(true)]
[Description("时间字体颜色"), Category("自定义属性")]
/// <summary>
/// 外部申明画笔和画刷工具
/// </summary>
private Color fontColor = Color.LightCoral;
public Color FontColor
{
get { return this.fontColor; }
set { this.fontColor = value; }
}
[Browsable(true)]
[Description("占用时间填充颜色"), Category("自定义属性")]
private Color fillColor = Color.OldLace;
public Color FillColor
{
get { return this.fillColor; }
set { this.fillColor = value; }
}
[Browsable(true)]
[Description("开始小时数"), Category("自定义属性")]
private int beginHour = 8;
public int BeginHour
{
get { return beginHour; }
set { beginHour = value; }
}
[Browsable(true)]
[Description("显示几个小时"), Category("自定义属性")]
private int hourCount = 10;
public int HourCount
{
get { return hourCount; }
set { hourCount = value; }
} /// <summary>
/// 时间段字典表
/// </summary>
public Dictionary<DateTime, DateTime> TimeParts = new Dictionary<DateTime, DateTime>();
/// <summary>
/// 时间格式
/// </summary>
DateTimeFormatInfo DateFormat = new DateTimeFormatInfo();
/// <summary>
/// 边距
/// </summary>
private int PaddingLeft = 50;//左边距
private int PaddingRight = 10;//右边距
private int PaddingButtom = 25; // 下边距
#endregion public KFillTime()
{
InitializeComponent(); DateFormat.ShortTimePattern = "HH:mm:ss";
} private void KFillTime_Paint(object sender, PaintEventArgs e)
{
InitXYData(beginHour + "", hourCount);
} private void InitXYData(string beginTime, int hourCount)
{
Graphics gra = this.CreateGraphics();
Pen p = new Pen(Color.SeaGreen, 1);
Pen p1 = new Pen(Color.LightSeaGreen, 1);
float onecolwidth = (this.Width - PaddingRight - PaddingLeft) / 60;//每分钟宽度
float hourwidth = (this.Height - 2 * PaddingButtom) / hourCount;
Brush fontBursh = new SolidBrush(fontColor); //纵坐标
for (int i = 0; i <= 60; i++)
{
if (i % 5 == 0)//分钟
{
gra.DrawString(i + "", timeFont, fontBursh, onecolwidth * i + PaddingLeft, this.Height - PaddingButtom + 5);
gra.DrawLine(p, PaddingLeft + i * onecolwidth, this.Height - PaddingButtom - 5, PaddingLeft + i * onecolwidth, this.Height - PaddingButtom + 5);
gra.DrawLine(p, PaddingLeft + i * onecolwidth, PaddingButtom, PaddingLeft + i * onecolwidth, this.Height - PaddingButtom);
}
else gra.DrawLine(p1, PaddingLeft + i * onecolwidth, PaddingButtom, PaddingLeft + i * onecolwidth, this.Height - PaddingButtom);
}
gra.DrawString(" 分钟", timeFont, fontBursh, onecolwidth * 60 + PaddingLeft + 12, this.Height - PaddingButtom + 5);
gra.DrawLine(p, PaddingLeft, this.Height - PaddingButtom, this.Width - PaddingLeft, this.Height - PaddingButtom); // 画横坐标标尺
for (int j = 0; j < hourCount + 1; j++)
{
gra.DrawLine(p1, PaddingLeft, this.Height - PaddingButtom - j * hourwidth, onecolwidth * 60 + PaddingLeft, this.Height - PaddingButtom - j * hourwidth);
if (j < hourCount) gra.DrawString(Convert.ToInt32(beginTime) + j + ":00", timeFont, fontBursh, 0, this.Height - PaddingButtom - j * hourwidth - hourwidth / 2);
} gra.DrawString("小时", timeFont, fontBursh, 0, this.Height - PaddingButtom - hourCount * hourwidth);
//画横坐标线
gra.DrawLine(p, PaddingLeft, PaddingButtom, PaddingLeft, this.Height - PaddingButtom);
} public void FillUsedTimes()
{
this.Refresh();
if (TimeParts == null || TimeParts.Count == 0) return; foreach (KeyValuePair<DateTime, DateTime> temppair in TimeParts)
{
FillTimeColor(temppair.Key, temppair.Value);
}
} public void FillUsedTimes(Dictionary<DateTime, DateTime> usedTimeDict)
{
this.Refresh();
foreach (KeyValuePair<DateTime, DateTime> temppair in usedTimeDict)
{
FillTimeColor(temppair.Key, temppair.Value);
}
} private void FillTimeColor(DateTime beginTime, DateTime endTime)
{
try
{
float oneminlwidth = (this.Width - PaddingRight - PaddingLeft) / 60;//每分钟宽度
float onehourwidth = (this.Height - 2 * PaddingButtom) / this.hourCount;//没小时宽度
Pen pen = new Pen(fillColor, 1); // 已占用时间段画笔
Brush brush = new SolidBrush(fillColor);
Graphics gra = this.CreateGraphics();
//在同一小时内
if (beginTime.Hour == endTime.Hour)
{
float startX = beginTime.Minute * oneminlwidth + PaddingLeft;
float startY = this.Height - PaddingButtom - (beginTime.Hour - Convert.ToInt32(beginHour) + 1) * onehourwidth;
RectangleF rect = new RectangleF(startX, startY, oneminlwidth * (endTime.Minute - beginTime.Minute), onehourwidth);
gra.DrawRectangle(pen, startX, startY, oneminlwidth * (endTime.Minute - beginTime.Minute), onehourwidth);
gra.FillRectangle(brush, rect);
}
else
{
float startX1 = beginTime.Minute * oneminlwidth + PaddingLeft;
float startY1 = this.Height - PaddingButtom - (beginTime.Hour - Convert.ToInt32(beginHour) + 1) * onehourwidth;
RectangleF rect1 = new RectangleF(startX1, startY1, oneminlwidth * (60 - beginTime.Minute), onehourwidth);
gra.DrawRectangle(pen, startX1, startY1, oneminlwidth * (60 - beginTime.Minute), onehourwidth);
gra.FillRectangle(brush, rect1); float startY2 = this.Height - PaddingButtom - (endTime.Hour - Convert.ToInt32(beginHour) + 1) * onehourwidth;
RectangleF rect2 = new RectangleF(PaddingLeft, startY2, oneminlwidth * endTime.Minute, onehourwidth);
gra.DrawRectangle(pen, PaddingLeft, startY2, oneminlwidth * endTime.Minute, onehourwidth);
gra.FillRectangle(brush, rect2);
}
}
catch (Exception) { }
} }
}
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Windows.Forms; namespace KUN.CONTROL
{
public partial class KFillTimeForm : Form
{
public KFillTimeForm()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
NewMethod();
} private void NewMethod()
{
try
{
Dictionary<DateTime, DateTime> dic = new Dictionary<DateTime, DateTime>();
dic.Add(Convert.ToDateTime("8:00"), Convert.ToDateTime("8:10"));
dic.Add(Convert.ToDateTime("8:50"), Convert.ToDateTime("9:40"));
dic.Add(Convert.ToDateTime("13:12"), Convert.ToDateTime("13:34"));
this.kFillTime1.TimeParts = dic;
this.kFillTime1.FillUsedTimes();
}
catch { return; }
} private void button2_Click(object sender, EventArgs e)
{
try
{
Dictionary<DateTime, DateTime> dic = new Dictionary<DateTime, DateTime>();
dic.Add(Convert.ToDateTime("9:00"), Convert.ToDateTime("9:10"));
dic.Add(Convert.ToDateTime("10:15"), Convert.ToDateTime("10:40"));
this.kFillTime1.TimeParts = dic;
this.kFillTime1.FillUsedTimes();
}
catch { return; }
} private void KFillTimeForm_Activated(object sender, EventArgs e)
{
// this.button1.PerformClick();
}
}
}  

运行效果:

 

C# Winfrom GDI+ 自定义控件的更多相关文章

  1. WinForm GDI+自定义控件总结(一)

    前言 由于项目的原因好久没写博客了,也正是项目的原因开始系统的学习WinForm,从而接触到自定义控件的开发.自定义控件的开发有一定的难度,对开发者要求比较高,需要了解Windows运行的机制,熟悉w ...

  2. WINFROM中自定义控件之绑定数据即时更新

    相信在WINFROM中写自定义控件或者用户控件,很多人都多多少少用过点 最近发现一个用户控件,绑定的数据源没办法自动更新,其实以前处理过这类的问题,可是这次遇到又花了1个多小时,所以决定记下来 在用户 ...

  3. winfrom GDI知识

     c#使用GDI+简单绘图 http://blog.csdn.net/smartsmile2012/article/details/30255303 NET3.5 GDI+ 图形操作1 http:// ...

  4. Winform GDI+ 相关资料

    在Visual Studio 2010中定义GDI+自定义控件——自定义控件介绍 http://www.cnblogs.com/zhangdong/archive/2010/05/20/1740177 ...

  5. C# Winform 实现Ajax效果自定义按钮

    技术看点 WinForm自定义控件的使用 自定义控件gif动画的播放 需求及效果 又来一波 C# GDI自定义控件show .这个控件已经使用几年了,最近找出来重构一下.原来是没有边框的,那么导致导航 ...

  6. C#Winform 自定义透明按钮和单窗体模块化实现

    技术看点 WinForm自定义控件的使用 WinForm单窗体应用如何模块化 需求及效果 又来一波 C# GDI自定义控件show .这个控件已经使用几年了,最近找出来重构一下.原来是没有边框的,那么 ...

  7. C# Winfrom 自定义控件添加图标

    Winfrom自定义控件添加自定义图标实现方式: 1.新建UserControl——略 2.寻找合适的图标文件——将文件和控件放置同一目录下(相同目录.自定义控件类名.图标文件名相同) 注:如果路径不 ...

  8. C# Winfrom 自定义控件——带图片的TextBox

    效果: 描述: 本来是想用GDI在左边画图片上去的,文本是居中对齐,如果文本是左对齐,文本会把图片遮住控件长这样: 但这样做,输入框在获取焦点时候,会把图片挡住就像这样: 输入完成之后图片就会显示完整 ...

  9. 戏说 .NET GDI+系列学习教程(三、Graphics类的应用_自定义控件--主要用于画面拖拽效果)

    如题,需求:在某个图片上用户可以手动指定位置. 如下: 中心思想:仿照Visual Studio工具中的控件的做法 如何仿照呢? 1.自定义的控件类继承System.Windows.Forms.Con ...

随机推荐

  1. nginx服务报错解决

    403禁止访问解决 . 重要:修改配置文件使用虚拟机,否则怎么配置都不生效,添加如下用户 [root@host---- html]# ll /etc/nginx/nginx.conf -rw-r--r ...

  2. 【Web】利用jquery实现百度新闻导航菜单滑动动画

    前言 前两天,群里有人问百度新闻导航是如何实现的,当时由于忙于工作,没有来得及细看,恰好今天有空闲时间,索性就实现一下这个效果吧: 思路与步骤 1.利用UL创建简单横向导航: <!DOCTYPE ...

  3. 【Fiori系列】浅谈SAP Fiori的设计美感与发展历程

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[Fiori系列]浅谈SAP Fiori的设计美 ...

  4. Grep---linux系统三剑客(一)

    grep .sed.awk被称为linux中的"三剑客". grep 更适合单纯的查找或匹配文本 sed  更适合编辑匹配到的文本 awk  更适合格式化文本,对文本进行较复杂格式 ...

  5. 《精通并发与Netty》学习笔记(09 - Java中流的概念)

    Java中流的概念 java程序通过流来完成输入/输出.流是生产或消费信息的抽象.流通过java的输入/输出与物理设备链接.尽管与它们链接的物理设备不尽相同,所有流的行为具有同样的方式.这样,相同的输 ...

  6. http与https的主要区别

    HTTP与HTTPS的主要区别如下: 1.https协议需要到ca申请证书,一般免费证书较少,因而需要一定费用. 2.http是超文本传输协议,信息是明文传输,https则是具有安全性的ssl加密传输 ...

  7. JavaScript 短路值

    了解表达式中的短路值. 逻辑运算从左到右.逻辑或运算,当左边的条件成立时,后面的条件将不再参与运算.因此在逻辑或运算中,尽量将条件结果为true的放第一位.而在逻辑与运算中,尽量将条件结果为false ...

  8. 36.HTTP协议

    HTTP简介 HTTP协议是Hyper Text Transfer Protocol(超文本传输协议)的缩写,是用于从万维网(WWW:World Wide Web )服务器传输超文本到本地浏览器的传送 ...

  9. Python基础总结之第七天开始【总结字符串、列表、元组的常用方法】(新手可相互督促)

    前面的笔记说,python中的一切数据类型都是对象 我们在细化下就是:对象可由两部分组成:对象数据和对象方法 针对不同类型的数据对象,有不同的操作对象的方法. 那么我们开始看下字符串对象的常用方法: ...

  10. [CF132C] Logo Turtle

    [CF132C] Logo Turtle , Luogu A turtle moves following by logos.(length is \(N\)) \(F\) means "m ...