Leadtools.WinForms Send comments on this topic. | Back to Introduction - All Topics | Help Version 15.8.31
DrawItem Event
See Also  Example
Leadtools.WinForms Namespace > RasterImageList Class : DrawItem Event




Occurs when a request is made to draw an item in an owner-drawn RasterImageList.

Syntax

Visual Basic (Declaration) 
Public Event DrawItem() As EventHandler(Of RasterImageListDrawItemEventArgs)
Visual Basic (Usage)Copy Code
Dim instance As RasterImageList
Dim handler As EventHandler(Of RasterImageListDrawItemEventArgs)
 
AddHandler instance.DrawItem, handler
C# 
public event EventHandler<RasterImageListDrawItemEventArgs> DrawItem()
Managed Extensions for C++ 
public: __event EventHandler<RasterImageListDrawItemEventArgs>* DrawItem();
C++/CLI 
public:
event EventHandler<RasterImageListDrawItemEventArgs>^ DrawItem();

Example

This example creates an owner drawn RasterImageList.

Visual BasicCopy Code
Private Class MyForm3 : Inherits Form
   Private imageList As RasterImageList
   Private codecs As RasterCodecs
   Public Sub New(ByVal title As String)
      Text = title
      ' Set the size of the form
      Size = New Size(400, 200)

      ' Create a new RasterImageList control
      imageList = New RasterImageList()
      imageList.Dock = DockStyle.Fill
      imageList.SelectionMode = RasterImageListSelectionMode.Single
      imageList.Size = Size
      Controls.Add(imageList)
      imageList.BringToFront()

      codecs = New RasterCodecs()

      ' Create three items
      Dim imagesPath As String = "C:\program files\LEAD Technologies\LEADTOOLS 15\Images\"

      For i As Integer = 0 To 2
         ' Load the image
         Dim index As Integer = i + 1
         Dim imageFileName As String = imagesPath & "Image" & index.ToString() & ".cmp"
         Dim image As RasterImage = codecs.Load(imageFileName, 0, CodecsLoadByteOrder.BgrOrGray, 1, 1)
         Dim item As RasterImageListItem = New RasterImageListItem(image, 1, "Item" & index.ToString())

         ' Select the first item
         If i = 0 Then
            item.Selected = True
         End If

         ' Add the item to the image list
         imageList.Items.Add(item)
      Next i

      ' Change the item size
      imageList.ItemSize = New Size(200, 200)

      ' Change the item image size
      imageList.ItemImageSize = New Size(120, 120)

      ' We are going to draw the items ourselves
      imageList.ViewStyle = RasterImageListViewStyle.OwnerDraw

      ' Add a handler to the DrawItem event
      AddHandler imageList.DrawItem, AddressOf rasterImageList_DrawItem

      ' Add the RasterImageList to the control collection.
      Controls.Add(imageList)
   End Sub

   Private Sub rasterImageList_DrawItem(ByVal sender As Object, ByVal e As RasterImageListDrawItemEventArgs)
      Dim item As RasterImageListItem = e.Item
      Dim imageList As RasterImageList = item.ImageList
      Dim g As Graphics = e.Graphics

      ' get the item rectangle
      Dim rc As Rectangle = imageList.GetItemRectangle(item)

      ' sanity check
      If rc.IsEmpty Then
         Return
      End If

      ' we want to draw a 1 pixel black rectangle around the item
      ' then we fill the inside of the rectangle with white if the item
      ' is not selected or lightgray if it is

      g.DrawRectangle(Pens.Black, rc.Left, rc.Top, rc.Width - 1, rc.Height - 1)

      ' we used up 1 pixel
      rc.Inflate(-1, -1)

      Dim b As Brush
      If item.Selected Then
         b = Brushes.LightGray
      Else
         b = Brushes.White
      End If
      g.FillRectangle(b, rc)

      ' calculate the rectangles for image and text
      If imageList.ShowItemText Then
         ' text is visible
         ' draw the text at the bottom of the item
         Dim textHeight As Integer = CInt(g.MeasureString("WWW", imageList.Font).Height + 4)
         Dim textRect As Rectangle = Rectangle.FromLTRB(rc.Left, rc.Bottom - textHeight, rc.Right, rc.Bottom)

         If (Not textRect.IsEmpty) Then
            Dim sf As StringFormat = New StringFormat()
            sf.Alignment = StringAlignment.Center
            sf.LineAlignment = StringAlignment.Center
            sf.Trimming = StringTrimming.EllipsisPath
            sf.FormatFlags = StringFormatFlags.NoWrap

            g.DrawString(item.Text, imageList.Font, Brushes.Black, textRect, sf)
            sf.Dispose()

            ' we need to update the item rectangle for the space
            ' we used up to draw the text
            rc.Height -= textRect.Height
         End If
      End If

      ' rc is the image rectangle
      If (Not rc.IsEmpty) Then
         ' now rc holds the rectangle to draw the image into

         ' first, set the correct page
         Dim savePage As Integer = -1
         If item.Image.Page <> item.Page Then
            ' the page is different

            ' save current image page so we can set it back when we are done
            savePage = item.Image.Page

            ' disable the image events, we are going to set the page back,
            ' so we do not want anybody subscribing to this image Changed
            ' event to know we changed it.
            item.Image.DisableEvents()

            ' set new page
            item.Image.Page = item.Page
         End If

         Try
            ' we want to center the image into whatever left of rc
            Dim itemImageSize As Size = imageList.ItemImageSize
            Dim imageRect As Rectangle = New Rectangle(rc.Left + (rc.Width - itemImageSize.Width) \ 2, rc.Top + (rc.Height - itemImageSize.Height) \ 2, itemImageSize.Width, itemImageSize.Height)

            ' we want to keep the aspect ratio
            imageRect = RasterImageList.GetFixedAspectRatioImageRectangle(item.Image.ImageWidth, item.Image.ImageHeight, imageRect)

            ' draw the image
            item.Image.Paint(g, imageRect, imageList.PaintProperties)

            ' finally, draw a black rectangle around the image
            imageRect.Inflate(1, 1)
            g.DrawRectangle(Pens.Black, imageRect.Left, imageRect.Top, imageRect.Width - 1, imageRect.Height - 1)
         Finally
            ' reset the old page
            If savePage <> -1 Then
               item.Image.Page = savePage

               ' re-enable the events
               item.Image.EnableEvents()
            End If
         End Try
      End If
   End Sub
End Class

Public Sub RasterImageList_DrawItem(ByVal title As String)
   RasterCodecs.Startup()
   Dim form As MyForm3 = New MyForm3(title)
   form.ShowDialog()
   RasterCodecs.Shutdown()
End Sub
C#Copy Code
class MyForm3 : Form 

   RasterImageList imageList; 
   RasterCodecs codecs; 
   public MyForm3(string title) 
   { 
      Text = title; 
      // Set the size of the form 
      Size = new Size(400, 200); 
 
      // Create a new RasterImageList control 
      imageList = new RasterImageList(); 
      imageList.Dock = DockStyle.Fill; 
      imageList.SelectionMode = RasterImageListSelectionMode.Single; 
      imageList.Size = Size; 
      Controls.Add(imageList); 
      imageList.BringToFront(); 
 
      codecs = new RasterCodecs(); 
 
      // Create three items 
      string imagesPath = @"C:\Program Files\LEAD Technologies\LEADTOOLS 15\Images\"; 
 
      for(int i = 0; i < 3; i++) 
      { 
         // Load the image 
         int index = i + 1; 
         string imageFileName = imagesPath + "Image" + index.ToString() + ".cmp"; 
         RasterImage image = codecs.Load(imageFileName, 0, CodecsLoadByteOrder.BgrOrGray, 1, 1); 
         RasterImageListItem item = new RasterImageListItem(image, 1, "Item" + index.ToString()); 
 
         // Select the first item 
         if(i == 0) 
            item.Selected = true; 
 
         // Add the item to the image list 
         imageList.Items.Add(item); 
      } 
 
      // Change the item size 
      imageList.ItemSize = new Size(200, 200); 
 
      // Change the item image size 
      imageList.ItemImageSize = new Size(120, 120); 
 
      // We are going to draw the items ourselves 
      imageList.ViewStyle = RasterImageListViewStyle.OwnerDraw; 
 
      // Add a handler to the DrawItem event 
      imageList.DrawItem += new EventHandler<RasterImageListDrawItemEventArgs>(rasterImageList_DrawItem); 
 
      // Add the RasterImageList to the control collection. 
      Controls.Add(imageList); 
   } 
 
   private void rasterImageList_DrawItem(object sender, RasterImageListDrawItemEventArgs e) 
   { 
      RasterImageListItem item = e.Item; 
      RasterImageList imageList = item.ImageList; 
      Graphics g = e.Graphics; 
 
      // get the item rectangle 
      Rectangle rc = imageList.GetItemRectangle(item); 
 
      // sanity check 
      if(rc.IsEmpty) 
         return; 
 
      // we want to draw a 1 pixel black rectangle around the item 
      // then we fill the inside of the rectangle with white if the item 
      // is not selected or lightgray if it is 
 
      g.DrawRectangle(Pens.Black, rc.Left, rc.Top, rc.Width - 1, rc.Height - 1); 
 
      // we used up 1 pixel 
      rc.Inflate(-1, -1); 
 
      Brush b; 
      if(item.Selected) 
         b = Brushes.LightGray; 
      else 
         b = Brushes.White; 
      g.FillRectangle(b, rc); 
 
      // calculate the rectangles for image and text 
      if(imageList.ShowItemText) 
      { 
         // text is visible 
         // draw the text at the bottom of the item 
         int textHeight = (int)(g.MeasureString("WWW", imageList.Font).Height + 4); 
         Rectangle textRect = Rectangle.FromLTRB( 
            rc.Left, 
            rc.Bottom - textHeight, 
            rc.Right, 
            rc.Bottom); 
 
         if(!textRect.IsEmpty) 
         { 
            StringFormat sf = new StringFormat(); 
            sf.Alignment = StringAlignment.Center; 
            sf.LineAlignment = StringAlignment.Center; 
            sf.Trimming = StringTrimming.EllipsisPath; 
            sf.FormatFlags = StringFormatFlags.NoWrap; 
 
            g.DrawString( 
               item.Text, 
               imageList.Font, 
               Brushes.Black, 
               textRect, 
               sf); 
            sf.Dispose(); 
 
            // we need to update the item rectangle for the space 
            // we used up to draw the text 
            rc.Height -= textRect.Height; 
         } 
      } 
 
      // rc is the image rectangle 
      if(!rc.IsEmpty) 
      { 
         // now rc holds the rectangle to draw the image into 
 
         // first, set the correct page 
         int savePage = -1; 
         if(item.Image.Page != item.Page) 
         { 
            // the page is different 
 
            // save current image page so we can set it back when we are done 
            savePage = item.Image.Page; 
 
            // disable the image events, we are going to set the page back, 
            // so we do not want anybody subscribing to this image Changed 
            // event to know we changed it. 
            item.Image.DisableEvents(); 
 
            // set new page 
            item.Image.Page = item.Page; 
         } 
 
         try 
         { 
            // we want to center the image into whatever left of rc 
            Size itemImageSize = imageList.ItemImageSize; 
            Rectangle imageRect = new Rectangle( 
               rc.Left + (rc.Width - itemImageSize.Width) / 2, 
               rc.Top + (rc.Height - itemImageSize.Height) / 2, 
               itemImageSize.Width, 
               itemImageSize.Height); 
 
            // we want to keep the aspect ratio 
            imageRect = RasterImageList.GetFixedAspectRatioImageRectangle( 
               item.Image.ImageWidth, 
               item.Image.ImageHeight, 
               imageRect); 
 
            // draw the image 
            item.Image.Paint(g, imageRect, imageList.PaintProperties); 
 
            // finally, draw a black rectangle around the image 
            imageRect.Inflate(1, 1); 
            g.DrawRectangle( 
               Pens.Black, 
               imageRect.Left, 
               imageRect.Top, 
               imageRect.Width - 1, 
               imageRect.Height - 1); 
         } 
         finally 
         { 
            // reset the old page 
            if(savePage != -1) 
            { 
               item.Image.Page = savePage; 
 
               // re-enable the events 
               item.Image.EnableEvents(); 
            } 
         } 
      } 
   } 

 
public void RasterImageList_DrawItem(string title) 

   RasterCodecs.Startup(); 
   MyForm3 form = new MyForm3(title); 
   form.ShowDialog(); 
   RasterCodecs.Shutdown(); 
}

Remarks

When you set the ViewStyle property to RasterImageListViewStyle.OwnerDraw, the RasterImageList control will fire the DrawItem event for each event that needs to be drawn. The RasterImageListDrawItemEventArgs argument passed to a DrawItem event handler provides a Graphics object that enables you to perform drawing and other graphical operations on the surface of the RasterImageList along with the RasterImageListItem object that needs to be drawn.

Requirements

Target Platforms: Microsoft .NET Framework 2.0, Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also