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;

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM