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();
        }
    }
}  

运行效果:

 

原文地址:https://www.cnblogs.com/YYkun/p/11579142.html

时间: 2024-11-03 00:37:09

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

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

前言 由于项目的原因好久没写博客了,也正是项目的原因开始系统的学习WinForm,从而接触到自定义控件的开发.自定义控件的开发有一定的难度,对开发者要求比较高,需要了解Windows运行的机制,熟悉win32Api和GDI+.下面是我收集的一些资料,挺不错的. 资料 .NET组件编程http://www.cnblogs.com/mapserver/category/57177.html .NET组件编程(1) 基础.NET组件编程(2) PropertyAttribute和EventAttrib

winfrom GDI知识

 c#使用GDI+简单绘图 http://blog.csdn.net/smartsmile2012/article/details/30255303 NET3.5 GDI+ 图形操作1 http://www.cnblogs.com/niunan/archive/2009/04/18/1438562.html

Winform GDI+ 相关资料

在Visual Studio 2010中定义GDI+自定义控件——自定义控件介绍 http://www.cnblogs.com/zhangdong/archive/2010/05/20/1740177.html C#发现之旅第一讲 C#-XML开发C#发现之旅第二讲 C#-XSLT开发C#发现之旅第三讲 使用C#开发基于XSLT的代码生成器C#发现之旅第四讲 Windows图形开发入门C#发现之旅第五讲 图形开发基础篇C#发现之旅第六讲 C#图形开发中级篇C#发现之旅第七讲 C#图形开发高级篇C

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

Winfrom自定义控件添加自定义图标实现方式: 1.新建UserControl——略 2.寻找合适的图标文件——将文件和控件放置同一目录下(相同目录.自定义控件类名.图标文件名相同) 3.选择图标——右键属性(生成操作==>嵌入的资源) 4.引入属性,如下图所示: 5.重新编译自定义控件——注意:编译后,自定义控件图标有可能不会改变(手动添加选项卡==>选择项==>浏览动态库添加引用==>自定义图标可见) 原文地址:https://www.cnblogs.com/YYkun/p/

winfrom自定义控件

c#TabControl控件左边选项卡左边显示,文字横向显示 http://blog.csdn.net/kasama1953/article/details/52133713 WinForm中,给TextBox文本框增加Ctrl+A全选快捷键. http://blog.csdn.net/wszhoho/article/details/7107693

C#自定义控件开发

自定义控件开发 一般而言,Visual Studio 2005中自带的几十种控件已经足够我们使用了,但是,在一些特殊的需求中,可能需要一些特殊的控件来与用户进行交互,这时,就需要我们自己开发新的.满足用户需求的控件. 要开发自己的控件,有几种方法: ?复合控件(Composite Controls):将现有的各种控件组合起来,形成一个新的控件,来满足用户的需求. ?扩展控件(Extended Controls):就是在现有的控件基础上,派生出一个新的控件,增加新的功能,或者修改原有功能,来满足用

winfrom 水晶按钮

闲来无事,从网上找了不少自定义控件,然后整理了一下,做了一个水晶按钮 /// <summary> /// 表示 Windows 的按钮控 /// </summary> [Description("表示 Windows 的按钮控件"), DefaultEvent("Click"), ToolboxBitmap(typeof (System.Windows.Forms.Button))] public class Button : Control

winfrom LED时钟

public sealed class Clock : PictureBox { public Clock() { SetStyle(ControlStyles.AllPaintingInWmPaint, true); SetStyle(ControlStyles.DoubleBuffer, true); SetStyle(ControlStyles.ResizeRedraw, true); SetStyle(ControlStyles.Selectable, true); SetStyle(C

自定义控件VS用户控件

1 自定义控件与用户控件区别 WinForm中, 用户控件(User Control):继承自 UserControl,主要用于开发 Container 控件,Container控件可以添加其他Controls控件 自定义控件(Custom Control):继承自 Control,主要用于开发windows控件的最基本的类,比如 Text,Button 控件 2 要开发自己的控件的几种方法[1] 复合控件(Composite Controls):将现有的各种控件组合起来,形成一个新的控件,来满