Monday, 6 July 2009

Own designed button in Windows Mobile

I hate Windows mobile standard user controls design. If you would like to create a nice application you can't do it with these controls. The worst control is the button, for the point of design. I think, if you would like to make a shiny design, first of all you need a good background and some good buttons. Other controls are not so important. The background is just an image box, everybody can take is to a form. What about with the buttons. Now I show you, how I can create designable buttons. Look at it!

First of all, Create a UserControl, for example BaseButton, or OwnDesignableButton. Then next add a PictureBox and set the next properties. Dock = Fill, SizeMode = StrechImage. These properties sets can take resizable to your button. After you need two pictures. Once for the button image. And once for the pushed button image. Create two picture properties:

private
Image _unpushedImage;


 


public
Image UnpushedImage

{


get { return _unpushedImage; }


set { _unpushedImage = value; }

}


 


private
Image _pushedImage;


 


public
Image PushedImage

{


get { return _pushedImage; }


set { _pushedImage = value; }

}


 

When you take add somewhere to your new ButtonControl, you can find two properties into the property list. A PushedImage, and an UnpushedImage. Next modify the setter of UnpushedImage, and set the _unpushedImage into the image of PictureBox:

set

{

_unpushedImage = value;


if (_unpushedImage != null)

{

ButtonImage.Image = _unpushedImage;

}

}


 

Now, when we are set the UnpushedImage properties, we can find, the controls image can sets. Next Catch two events one the ButtonImage.MouseUp and one the ButtonImage.MouseDown and create the change of image.


 

private
void ButtonImage_MouseUp(object sender, MouseEventArgs e)

{


if(_unpushedImage != null)

ButtonImage.Image = _unpushedImage;

}


 


private
void ButtonImage_MouseDown(object sender, MouseEventArgs e)

{


if (_pushedImage != null)

ButtonImage.Image = _pushedImage;

}


 

Now you have a Control, which looks like a button. Next you can create an event, which rise when somebody push your button. Than the event is created, Invoke this event into the ButtonImage.MouseUp event handler.

public
event
MouseEventHandler ButtonClick;


 


private
void ButtonImage_MouseUp(object sender, MouseEventArgs e)

{


if(_unpushedImage != null)

ButtonImage.Image = _unpushedImage;


 


if(ButtonClick != null)

ButtonClick.Invoke(sender, e);

}


 

If you would like a really nice button, you could make the got focus event, when you show another image like selected control image.

Now you need a cool designer, how create some cool button picture to you.

No comments:

Post a Comment