C# Winfrom GDI+ 自定义控件
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+ 自定义控件的更多相关文章
- WinForm GDI+自定义控件总结(一)
前言 由于项目的原因好久没写博客了,也正是项目的原因开始系统的学习WinForm,从而接触到自定义控件的开发.自定义控件的开发有一定的难度,对开发者要求比较高,需要了解Windows运行的机制,熟悉w ...
- WINFROM中自定义控件之绑定数据即时更新
相信在WINFROM中写自定义控件或者用户控件,很多人都多多少少用过点 最近发现一个用户控件,绑定的数据源没办法自动更新,其实以前处理过这类的问题,可是这次遇到又花了1个多小时,所以决定记下来 在用户 ...
- winfrom GDI知识
c#使用GDI+简单绘图 http://blog.csdn.net/smartsmile2012/article/details/30255303 NET3.5 GDI+ 图形操作1 http:// ...
- Winform GDI+ 相关资料
在Visual Studio 2010中定义GDI+自定义控件——自定义控件介绍 http://www.cnblogs.com/zhangdong/archive/2010/05/20/1740177 ...
- C# Winform 实现Ajax效果自定义按钮
技术看点 WinForm自定义控件的使用 自定义控件gif动画的播放 需求及效果 又来一波 C# GDI自定义控件show .这个控件已经使用几年了,最近找出来重构一下.原来是没有边框的,那么导致导航 ...
- C#Winform 自定义透明按钮和单窗体模块化实现
技术看点 WinForm自定义控件的使用 WinForm单窗体应用如何模块化 需求及效果 又来一波 C# GDI自定义控件show .这个控件已经使用几年了,最近找出来重构一下.原来是没有边框的,那么 ...
- C# Winfrom 自定义控件添加图标
Winfrom自定义控件添加自定义图标实现方式: 1.新建UserControl——略 2.寻找合适的图标文件——将文件和控件放置同一目录下(相同目录.自定义控件类名.图标文件名相同) 注:如果路径不 ...
- C# Winfrom 自定义控件——带图片的TextBox
效果: 描述: 本来是想用GDI在左边画图片上去的,文本是居中对齐,如果文本是左对齐,文本会把图片遮住控件长这样: 但这样做,输入框在获取焦点时候,会把图片挡住就像这样: 输入完成之后图片就会显示完整 ...
- 戏说 .NET GDI+系列学习教程(三、Graphics类的应用_自定义控件--主要用于画面拖拽效果)
如题,需求:在某个图片上用户可以手动指定位置. 如下: 中心思想:仿照Visual Studio工具中的控件的做法 如何仿照呢? 1.自定义的控件类继承System.Windows.Forms.Con ...
随机推荐
- 在谷歌中缓存下载视频离线观看,js代码
var download=function(urlInfo) { when(createFile(localFileName)) .then(function (fileInfo) { var dow ...
- K/3 Cloud 单据关联查询
销售出库单 下推 销售退货单,如何获知他们的关联关系?T_SAL_OUTSTOCKENTRY 是销售出库单分录T_SAL_RETURNSTOCKENTRY 是销售退货单分录T_SAL_RETURNST ...
- 【Matlab开发】matlab中bar绘图设置与各种距离度量
[Matlab开发]matlab中bar绘图设置与各种距离度量 标签(空格分隔): [Matlab开发] [机器学习] 声明:引用请注明出处http://blog.csdn.net/lg1259156 ...
- 最新 小米java校招面经 (含整理过的面试题大全)
从6月到10月,经过4个月努力和坚持,自己有幸拿到了网易雷火.京东.去哪儿.小米等10家互联网公司的校招Offer,因为某些自身原因最终选择了小米.6.7月主要是做系统复习.项目复盘.LeetCode ...
- sql语句 两表关联查询计算数量
select sum(a1.`num`) from `order_orderlistrow` as a1 INNER JOIN `order_orderlist` as a2 on a1.`ord ...
- TCP连接可能出现的异常总结
1.java.net.BindException:Address already in use:bind 服务端出错,两次对同一个端口进行启动(会在服务端发生报错,抛出异常,不考虑) 2.java.n ...
- Chrome浏览器控制台报 POST http://*** net::ERR_BLOCKED_BY_CLIENT
开发项目广告模块时,遇到前端提交的请求后台拿不到,好像被什么拦截了,查看了过滤器,拦截器都无错误,且请求也到不了拦截器,chrome浏览器报:ERR_BLOCKED_BY_CLIENT错误 搞腾一半天 ...
- Netty源码剖析-启动服务
参考文献:极客时间傅健老师的<Netty源码剖析与实战>Talk is cheap.show me the code! --1主线分两步: 一:首先在our thread里,如果写在mai ...
- selenium登录实验楼
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.s ...
- golang数据类型
整数类型 Golang各整数类型分:有符号和无符号,int uint 的大小和系统有关. Golang查看一个变量的数据类型: package main import "fmt" ...