Our Shop 



Saturday, October 2, 2010

Size override and User draw of Gantt Time Items

One feature that may come in handy from time to time is the size override behavior that you can get by implement the event OnTimeItem_UserDrawBounds in the GTP.NET.

It is most commonly used with UserDraw – since most of the standard time items have a standard square form that does not get any real benefit from overriding the perceived size – since the standard square that is defined by Start and Stop in one axis and GanttRow Height (with consideration to Sub columns) in the other direction.

   1: gantt1.OnTimeItem_UserDraw += new TimeItemEvent(gantt1_OnTimeItem_UserDraw);
   2: gantt1.OnTimeItem_UserDrawBounds += new TimeItemEvent(gantt1_OnTimeItem_UserDrawBounds);

And the implementations:

   1: /// <summary>
   2: /// This event can help you override how a time item is perceived in the TimeItemFromXY (the active clickable area )
   3: /// </summary>
   4: void gantt1_OnTimeItem_UserDrawBounds(Gantt aGantt, TimeItemEventArgs e)
   5: {
   6:     GraphicsPath gp = new GraphicsPath();
   7:     gp.AddEllipse(e.Rect);
   8:     e.BoundingRegion = new Region(gp);
   9: }
   1: /// <summary>
   2: /// ...and this event helps you draw something in that area. 
   3: /// Note that the two areas can differ, its up to you, but it will a bit strange...
   4: /// </summary>
   5: void gantt1_OnTimeItem_UserDraw(Gantt aGantt, TimeItemEventArgs e)
   6: {
   7:     GraphicsPath gp = new GraphicsPath();
   8:     gp.AddEllipse(e.Rect);
   9:     e.G.FillPath(new SolidBrush(Color.White), gp);
  10:     e.G.DrawPath(new Pen(Color.Blue), gp);
  11:     string s = "Notice how\r\nclicks in the corners\r\nare ignored";
  12:     SizeF mess=e.G.MeasureString(s, this.gantt1.Font);
  13:     Point pointToPlaceText = new Point((int)(e.Rect.Left + Math.Round((double)e.Rect.Width / 2) - Math.Round(mess.Width / 2)), 
  14:                                        (int)(e.Rect.Top + Math.Round((double)e.Rect.Height / 2) - Math.Round(mess.Height / 2)));
  15:     e.G.DrawString(s, this.gantt1.Font, new SolidBrush(Color.Red), pointToPlaceText);
  17: }

This will render this way:


Now the cool thing is that when you click in the white area the TimeItem will be selected/moved/resized etc, but when you click in the corners, where the Time Item Square should have been drawn if this was a standard time item, this is just like clicking on the GanttRow alone.

I also added some code in the userdraw to center a text – you need to measure the text then consider the available space – then place the text.


Post a Comment

Subscribe to Post Comments [Atom]

<< Home

This page is powered by Blogger. Isn't yours?

Subscribe to Posts [Atom]


Support intro


FAQ phGantTimePackage


FAQ general