PC Review Forums Newsgroups Microsoft DotNet Microsoft Dot NET Framework Forms Drawing custom widgets disabled (icons + combobox as example)

Reply

Drawing custom widgets disabled (icons + combobox as example)

 
Thread Tools Rate Thread
Old 14-12-2005, 04:00 PM   #1
=?Utf-8?B?Sm9uIExlZQ==?=
Guest
 
Posts: n/a
Default Drawing custom widgets disabled (icons + combobox as example)


Hello,

A few questions.

I'm trying to refine the OnDrawItem code for drawing a combobox where the
listbox items have icons. I used the ImageCombo component listed in
codeproject.com as the basis of my code (but I use ListViewItem instances for
the items instead of a custom item class). When the combobox is disabled,
however, the appearance of the combobox is inconsistent with the way
System.Windows.Forms.ComboBox looks when it is disabled. Can anyone tell me
what flag or line I'm missing? The code is below:

protected override void OnDrawItem( DrawItemEventArgs e )
{
Console.WriteLine( "draw state: " + e.State );
e.DrawBackground();

int iBuffer = ( m_imageList != null ? m_imageList.ImageSize.Width : 0 );
/// check if it is an item from the Items collection
if ( e.Index < 0 )
/// not an item, draw the text (indented)
e.Graphics.DrawString( this.Text, e.Font,
new SolidBrush( e.ForeColor ), e.Bounds.Left + iBuffer,
e.Bounds.Top );
else
{
/// check if item is an ListViewItem
if ( this.Items[ e.Index ] is ListViewItem )
{
// get item to draw
ListViewItem item = this.Items[ e.Index ] as ListViewItem;
// get forecolor & font
Color forecolor = ( item.ForeColor !=
Color.FromKnownColor( KnownColor.Transparent ) ) ?
item.ForeColor : e.ForeColor;

// -1: no image
if ( item.ImageIndex != -1 || item.ImageKey.Length > 0 )
{
// draw image, then draw text next to it
if ( item.ImageIndex > -1 )
ImageList.Draw( e.Graphics,
e.Bounds.Left, e.Bounds.Top, item.ImageIndex );
else
ImageList.Draw( e.Graphics,
e.Bounds.Left, e.Bounds.Top, ImageList.Images.IndexOfKey(
item.ImageKey ) );
e.Graphics.DrawString( item.Text, e.Font,
new SolidBrush( forecolor ), e.Bounds.Left + iBuffer,
e.Bounds.Top );
}
else
{
if ( item.ImageKey != "" )
// draw text (indented)
e.Graphics.DrawString( item.Text, e.Font,
new SolidBrush( forecolor ), e.Bounds.Left + iBuffer,
e.Bounds.Top );
}
}
else

// it is not an ImageComboItem, draw it
e.Graphics.DrawString( this.Items[ e.Index ].ToString(),
e.Font, new SolidBrush( e.ForeColor ), e.Bounds.Left + iBuffer,
e.Bounds.Top );

}
e.DrawFocusRectangle();

//base.OnDrawItem( e );
}

By the way, the items are added like this (MarkerLvi is a subclass of
ListViewItem):

IEnumerator<Marker> it = getEnumerator();
while ( it.MoveNext() )
{
MarkerLvi lvi = new MarkerLvi( it.Current );
lvi.ForeColor = Color.Transparent;
lvi.BackColor = Color.Transparent;
/// For appearance the user can connect to any marker in the map
m_appearanceCbMarker.Items.Add( lvi );
}

Last question: Some examples I see DrawBackground() and
DrawFocusRectangle() next to each other:

e.DrawBackground()
e.DrawFocusRectangle()
/// custom drawing code

Other times I see the custom drawing code in between:

e.DrawBackground()
/// custom drawing code
e.DrawFocusRectangle()

which one is correct?


I appreciate the assistance,
Jon
  Reply With Quote
Reply



Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off