先自制甘特图控件:
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.ComponentModel;
namespace GanttControls
{
/// <summary>
/// 甘特图控件
/// zgke@sina.com
/// qq:116149
/// </summary>
public class ProjectViewBox : UserControl
{
#region 容器
#region 设计器代码
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region 组件设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.hScrollBar1 = new System.Windows.Forms.HScrollBar();
this.vScrollBar1 = new System.Windows.Forms.VScrollBar();
this.MainPictureBox = new System.Windows.Forms.PictureBox();
((System.ComponentModel.ISupportInitialize)(this.MainPictureBox)).BeginInit();
this.SuspendLayout();
//
// hScrollBar1
//
this.hScrollBar1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.hScrollBar1.LargeChange = 1;
this.hScrollBar1.Location = new System.Drawing.Point(1, 111);
this.hScrollBar1.Maximum = 60;
this.hScrollBar1.Name = "hScrollBar1";
this.hScrollBar1.Size = new System.Drawing.Size(120, 16);
this.hScrollBar1.TabIndex = 0;
//
// vScrollBar1
//
this.vScrollBar1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Right)));
this.vScrollBar1.LargeChange = 1;
this.vScrollBar1.Location = new System.Drawing.Point(121, 0);
this.vScrollBar1.Maximum = 60;
this.vScrollBar1.Name = "vScrollBar1";
this.vScrollBar1.Size = new System.Drawing.Size(16, 111);
this.vScrollBar1.TabIndex = 1;
//
// MainPictureBox
//
this.MainPictureBox.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.MainPictureBox.Location = new System.Drawing.Point(0, 0);
this.MainPictureBox.Name = "MainPictureBox";
this.MainPictureBox.Size = new System.Drawing.Size(120, 109);
this.MainPictureBox.TabIndex = 2;
this.MainPictureBox.TabStop = false;
//
// ProjectBox
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.SystemColors.Control;
this.Controls.Add(this.vScrollBar1);
this.Controls.Add(this.hScrollBar1);
this.Controls.Add(this.MainPictureBox);
this.Name = "ProjectBox";
this.Size = new System.Drawing.Size(139, 129);
this.Load += new System.EventHandler(this.ProjectBox_Load);
this.SizeChanged += new System.EventHandler(this.ProjectBox_SizeChanged);
((System.ComponentModel.ISupportInitialize)(this.MainPictureBox)).EndInit();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.HScrollBar hScrollBar1;
private System.Windows.Forms.VScrollBar vScrollBar1;
private System.Windows.Forms.PictureBox MainPictureBox;
#endregion
public ProjectViewBox()
{
InitializeComponent();
}
#region 私有成员
/// <summary>
/// 最小单元格
/// </summary>
private int m_Size = 18;
/// <summary>
/// 日期索引 记录当前绘制的位置
/// </summary>
private int m_DayIndex = 0;
/// <summary>
/// 开始绘制位置
/// </summary>
private int m_JobIndex = 0;
#endregion
/// <summary>
/// 大小改变
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ProjectBox_SizeChanged(object sender, EventArgs e)
{
if (MainPictureBox.Image != null) PaintProjectImage();
}
private MyScroll Hscroll;
private MyScroll Vscroll;
private void ProjectBox_Load(object sender, EventArgs e)
{
MainPictureBox.Image = new Bitmap(1, 1);
PaintProjectImage();
Hscroll = new MyScroll();
Hscroll.Click += new MyScroll.ScrollClick(Hscroll_Click);
Hscroll.AssignHandle(hScrollBar1.Handle);
Vscroll = new MyScroll();
Vscroll.Click += new MyScroll.ScrollClick(Vscroll_Click);
Vscroll.AssignHandle(vScrollBar1.Handle);
m_ProjectSelect.ProjectBox = this;
}
#region 滚动条