Dev Express SchedulerControl 实现自定义排期(转)

实现效果图:

整理下需要实现的目标:

1.三种颜色:当前时间之前 灰色; 当天 淡蓝色;当前时间之后,淡绿色,日期格式重写

2.appointment左对齐

3.右键appointment 弹出自定义菜单

4.鼠标移到appointment上,自定义Tooltip

5.禁用双击appointment弹出默认appointment界面,且appointment无法编辑,拖拽

 

代码

//1.禁用双击appointment弹出默认appointment界面,且appointment无法编辑,拖拽

this.OptionsCustomization.AllowAppointmentCreate = DevExpress.XtraScheduler.UsedAppointmentType.None;
this.OptionsCustomization.AllowAppointmentDelete = DevExpress.XtraScheduler.UsedAppointmentType.None;
this.OptionsCustomization.AllowAppointmentEdit = DevExpress.XtraScheduler.UsedAppointmentType.None;
this.OptionsCustomization.AllowAppointmentDrag = DevExpress.XtraScheduler.UsedAppointmentType.None;
this.OptionsCustomization.AllowAppointmentMultiSelect = false;
this.OptionsRangeControl.AllowChangeActiveView = false;
this.Views.MonthView.CompressWeekend = false;
this.OptionsBehavior.ShowRemindersForm = false;

//2.右键菜单--自定义

private void scheduleControl_PopupMenuShowing(object sender, PopupMenuShowingEventArgs e)
{

DXMenuItem item= new DXMenuItem("菜单项");;
if (scheduleControl.ActiveViewType == SchedulerViewType.Day)//不同view展示不同菜单项
{
}
if (e.Menu.Id == SchedulerMenuItemId.DefaultMenu)//右键空白处
{
e.Menu.Items.Clear();

}
e.Menu.Items.Add(item);

}
if (e.Menu.Id == SchedulerMenuItemId.AppointmentMenu)//appointment 上右键
{
e.Menu.Items.Clear();
e.Menu.Items.Add(item);
}
}

//3.自定义Tooltip

ToolTipController toolTipController1=new ToolTipController ();

this.toolTipController1.AllowHtmlText = true;
this.toolTipController1.ShowBeak = true;
this.toolTipController1.BeforeShow += new DevExpress.Utils.ToolTipControllerBeforeShowEventHandler(this.toolTipController1_BeforeShow);

 this.scheduleControl.ToolTipController = this.toolTipController1;//与schedule绑定

private void toolTipController1_BeforeShow(object sender, DevExpress.Utils.ToolTipControllerShowEventArgs e)
{
ToolTipController controller = sender as ToolTipController;
AppointmentViewInfo aptViewInfo = controller.ActiveObject as AppointmentViewInfo;
if (aptViewInfo == null) return;

e.IconType = ToolTipIconType.Information;
e.Title = e.ToolTip;
e.ToolTip = “自定义tip内容” ;
}

//4.Appointment颜色设置

private void scheduleControl_AppointmentViewInfoCustomizing(object sender, AppointmentViewInfoCustomizingEventArgs e)
{
DevExpress.XtraScheduler.Drawing.AppointmentViewInfo viewInfo = e.ViewInfo as DevExpress.XtraScheduler.Drawing.AppointmentViewInfo;
TeamAppointment app = viewInfo.Appointment as TeamAppointment;
if (app == null)
return;


Brush backbr =GetBrush(viewInfo.Interval.Start,datetime.Now());//自定义颜色
Color forecolor = viewInfo.Appearance.ForeColor;


viewInfo.Appearance.BackColor = ((System.Drawing.SolidBrush)(backbr)).Color;//背景色
viewInfo.Appearance.BackColor2 = ((System.Drawing.SolidBrush)(backbr)).Color;
viewInfo.Appearance.ForeColor = forecolor;//前景色
viewInfo.Appearance.TextOptions.HAlignment = HorzAlignment.Near;//左对齐
viewInfo.Appearance.Options.UseBackColor = true;
viewInfo.Appearance.Options.UseForeColor = true;
viewInfo.Appearance.Options.UseTextOptions = true;
}

//5.重绘appointment内容

private void scheduleControl_CustomDrawAppointment(object sender, CustomDrawObjectEventArgs e)
{
try
{
if (e.ObjectInfo.GetType() != typeof(DevExpress.XtraScheduler.Drawing.AppointmentViewInfo) && e.ObjectInfo.GetType() != typeof(DevExpress.XtraScheduler.Drawing.HorizontalAppointmentViewInfo))
return;
DevExpress.XtraScheduler.Drawing.AppointmentViewInfo viewInfo = e.ObjectInfo as DevExpress.XtraScheduler.Drawing.AppointmentViewInfo;
TeamAppointment app = viewInfo.Appointment as TeamAppointment;
if (app == null)
return;

e.Cache.DrawString(Truncate(viewInfo.Appointment, viewInfo.Appearance.Font, viewInfo.Bounds.Width), viewInfo.Appearance.Font, new SolidBrush(viewInfo.Appearance.ForeColor), viewInfo.Bounds, viewInfo.Appearance.TextOptions.GetStringFormat());//超出部分,做处理
e.Handled = true;//不会执行默认操作
}
catch (Exception ex)
{
throw ex;
}
}

//重绘Header部分

private void scheduleControl_CustomDrawDayHeader(object sender, CustomDrawObjectEventArgs e)
{
try
{
if (e.ObjectInfo.GetType() != typeof(DevExpress.XtraScheduler.Drawing.MonthViewTimeCellHeader))
return;
DevExpress.XtraScheduler.Drawing.MonthViewTimeCellHeader header = (DevExpress.XtraScheduler.Drawing.MonthViewTimeCellHeader)e.ObjectInfo;
SelectableIntervalViewInfo viewInfo = e.ObjectInfo as SelectableIntervalViewInfo;

Brush br = QueryHelper.GetBrush(header.Interval.Start, currentYerestday);
if (viewInfo.Selected)
{
br = selectedColor;//设置被选中时颜色
}
e.Cache.FillRectangle(br, new Rectangle(e.Bounds.X + 1, e.Bounds.Y, e.Bounds.Width - 2, e.Bounds.Height - 1));//填充header部分
StringFormat sf = header.Appearance.HeaderCaption.TextOptions.GetStringFormat(TextOptions.DefaultOptionsCenteredWithEllipsis);
e.Cache.DrawString(header.Interval.Start.ToString("MM月dd日"), scheduleControl.MonthView.Appearance.HeaderCaption.Font, Brushes.Black, e.Bounds, sf);//自定义日期格式
e.Handled = true; 
}
catch (Exception ex)
{
throw ex;
}
}

private void scheduleControl_CustomDrawTimeCell(object sender, CustomDrawObjectEventArgs e)
{
try
{
if (e.ObjectInfo.GetType() != typeof(DevExpress.XtraScheduler.Drawing.MonthSingleWeekCell))
return;
DevExpress.XtraScheduler.Drawing.MonthSingleWeekCell cell = ((DevExpress.XtraScheduler.Drawing.MonthSingleWeekCell)(e.ObjectInfo));

SelectableIntervalViewInfo selcell = e.ObjectInfo as SelectableIntervalViewInfo;
Brush br = QueryHelper.GetBrush(cell.Interval.Start,currentYerestday);
if (selcell.Selected)
{
 br =selectedColor;
}
e.Cache.FillRectangle(br, new Rectangle(e.Bounds.X + 1, e.Bounds.Y, e.Bounds.Width - 2, e.Bounds.Height - 1));//填充图示2部分
e.Handled = true;
}
catch (Exception ex)
{
throw ex;
}
}

//自定义颜色

public static Brush GetBrush(DateTime inDate, DateTime currentYerestday)
{
Brush result = null;
if (inDate <= currentYerestday)
{
result = Brushes.LightGray;
}
else if (inDate > currentYerestday && inDate < currentYerestday.AddHours(24))
{
result = Brushes.LightBlue;
}
else
{
result = Brushes.LightGreen;
}

return result;

}

posted @ 2016-10-21 10:58  武道巅峰  阅读(951)  评论(0编辑  收藏  举报