I have found that the best way to learn MinsFusion is by reading the old posts here (and of course, asking Meppy!). So I thought it would be useful to share the code I created from Meppy's sample so that others can benefit and also, ask for any suggestion to improve it.
My need is to color cells according to whether they are work hours or not. I also want to allow for work days that may include week ends. And to handle work days that span calendar dates (ie. go past midnight). My code colors the non-working cells a light gray. The code below works for my needs with a couple of short comings - (1) I have to have each calendar start on the whole hour closest to the start time of the work day (this to get the time line grouping to display as I want it to (see my earlier posts). This does not color the cell or cells at the start of the day which are not part of the work day due to the hourly start requirement. (2) If the cell time is larger than the minutes of the work time (e.g. cell time 60 minutes and the work time ends at 7:30, it does not allow for coloring half a cell). This is one I would like to fix if anyone can tell me how.
Here is my code in VB.Net: Private Sub calendar_Draw(ByVal sender As Object, _ ByVal e As MindFusion.Scheduling.WinForms.DrawEventArgs) Handles calendar.Draw If e.Element = CustomDrawElements.TimetableCell Then Dim cellStart As DateTime = e.Date.Add(e.StartTime) Dim cellEnd As DateTime = cellStart.Add(_calendar.TimetableSettings.CellTime) Dim localWorkEnd As New TimeSpan ' allow for day run past midnight If workEndTime < workStartTime Then localWorkEnd = workEndTime.Add(New TimeSpan(24, 0, 0)) Else localWorkEnd = workEndTime End If Dim cellEndLimit As DateTime = e.Date.Add(localWorkEnd) If cellStart < cellEndLimit AndAlso _ theWeekDayCode.IndexOf(e.Date.DayOfWeek) >= 0 Then ' a work period so no coloring Exit Sub End If Dim b As Rectangle = e.Bounds Dim g As MindFusion.Drawing.IGraphics = e.Graphics Dim p As New Pen(e.Style.LineColor, 0) Dim b2 As New SolidBrush(Color.WhiteSmoke) g.FillRectangle(b2, b) g.DrawLine(p, b.Left, b.Top, b.Left, b.Bottom - 1) g.DrawLine(p, b.Right - 1, b.Top, b.Right - 1, b.Bottom - 1) g.DrawLine(p, b.Left, b.Top, b.Right - 1, b.Top) g.DrawLine(p, b.Left, b.Bottom - 1, b.Right - 1, b.Bottom - 1) End If End Sub
And the code to activiate the custom draw event: calendar.CustomDraw = MindFusion.Scheduling.WinForms.CustomDrawElements.TimetableCell
Any comments or improvements greatly appreciated. I also encourage others to post key code so we can all benefit. This is a great tool but the learning curve can be difficult.
John
|