Jens Kühner

Geek stuff about the .NET, Compact and Micro Framework.

List box with a background (image) for the .NET Micro Framework

The development boards and devices for the .NET Micro Framework have great color lcd displays. So a list box menu with a white or solid background looks quite boring. Why not add a background to a listbox?

Therefore I derived my custom list box called BackgroundImageListBox from ListBox. It has a property BackgroundIBitmap to specify a certain bitmap as background. If StretchImage is true the bitmap will be stretched to fill the whole list box, if false the bitmap will be centered. If no bitmap is specified, a color gradient background is drawn. This is done by overriding the OnRender method.

List box with color gradient background

And here the custom list box control:

using System;

using Microsoft.SPOT;

using Microsoft.SPOT.Presentation.Controls;

using Microsoft.SPOT.Presentation.Media;

 

namespace Kuehner.SPOT.Presentation.Controls

{

    public class BackgroundImageListBox : ListBox

    {

        private Bitmap backgroundBitmap;

        private bool stretchImage;

 

        public BackgroundImageListBox()

        {

            ScrollViewer v = base.LogicalChildren[0] as ScrollViewer;

            v.Background = null;

            this.Background = null;

        }

 

        public override void OnRender(DrawingContext dc)

        {

            base.OnRender(dc);

            if (this.backgroundBitmap != null)

            {

                if (this.stretchImage)

                    dc.Bitmap.StretchImage(0, 0, this.backgroundBitmap, this.Width, this.Height, 0xFF); //stretch to fit

                else

                {

                    //center

                    int x = (this.Width - this.backgroundBitmap.Width) / 2;

                    int y = (this.Height - this.backgroundBitmap.Height) / 2;

                    dc.DrawImage(this.backgroundBitmap, x, y); //draw unscaled

                }

            }

            else

            {

                //draw gradient if no bitmap specified

                dc.Bitmap.DrawRectangle(Color.Black, 0,

                                        0, 0, this.Width, this.Height, 0, 0,

                                        ColorUtility.ColorFromRGB(255, 96, 96), 0, 0,

                                        ColorUtility.ColorFromRGB(96, 96, 255), 0, this.Height,

                                        0xFF);

            }

        }

 

        public Bitmap BackgroundBitmap

        {

            get { return this.backgroundBitmap; }

            set { this.backgroundBitmap = value; }

        }

 

        public bool StretchImage

        {

            get { return this.stretchImage; }

            set { this.stretchImage = value; }

        }

    }

}

When you polulate your list box you need to set the Background of a list box item to null so that it is painted transparently.

Heres some code to populate your list box. 

        public Window CreateWindow()

        {

            // Create a window object and set its size to the

            // size of the display.

            mainWindow = new Window();

            mainWindow.Height = SystemMetrics.ScreenHeight;

            mainWindow.Width = SystemMetrics.ScreenWidth;

 

            BackgroundImageListBox listBox = new BackgroundImageListBox();

            listBox.BackgroundBitmap = Resources.GetBitmap(Resources.BitmapResources.MFsnowflake);

            //listBox.StretchImage = true;

            listBox.Width = mainWindow.Width;

            listBox.Height = mainWindow.Height;

 

            Font font = Resources.GetFont(Resources.FontResources.small);

            foreach (String s in new String[] { "One", "Two", "Three",

                                                "Four", "Five", "Six", "Seven", "Eight" })

            {

                Text text = new Text(font, "Menu " + s);

                text.Width = listBox.Width;

                text.Height = 20;

                text.TextAlignment = TextAlignment.Center;

                ListBoxItem item = new ListBoxItem();

                item.Child = text;

                item.Background = null; //important

                listBox.Items.Add(item);

            }

 

            mainWindow.Child = listBox;

 

            // Connect the button handler to all of the buttons.

            mainWindow.AddHandler(Buttons.ButtonUpEvent, new ButtonEventHandler(OnButtonUp), false);

 

            // Set the window visibility to visible.

            mainWindow.Visibility = Visibility.Visible;

 

            // Attach the button focus to the window.

            Buttons.Focus(mainWindow);

 

            return mainWindow;

        }

Comments

Dennis van der Stelt said:

Awesome! We really have to talk some time about the compact and micro framework. I'm getting more and more interested. And as I'm developing a training for Class-A I want to know more and more about it all... Haven't really looked at Micro though, but I will have to, now that you're triggering me! :)

# October 10, 2007 9:47 AM

Aart Matsinger said:

Tried it out: looks very good.

I am missing the highlighting (focus) of the selected item. Is that a restriction of the Micro Framework ?

 

It is possible. You need to derive your custom ListBoxItem and override the OnIsSelectedChanged and set a different text color or brush.

# October 16, 2007 11:28 AM

aart said:

Thanks: I have implemented this highlighting succesfully according to your suggestion.

# October 18, 2007 10:43 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)