Development Environment of this Article
Operating System: xp
Program Development Environment vss2008 + windowMobile6.1Professional
Overview:
During WindowMobile development, especially for UI design with high requirements, we need to implement tags with custom background images.
For such a requirement, we usually think of inheriting the lable control and the OnPaint method of the Override control. However, the odd phenomenon is that the inherited control does not use the OnPaint method in any way. Many people on the network also encounter this problem. The following describes how to implement a custom control.
Customize the lable of the background imageWidget.
The solution is as follows:
Define a Control inheritance and Control,
Override OnPaint method.
Do the following in the OnPaint method:
L first clear the current background
L draw background
L display the current label text. Note that Color. Transparent is used for the text background when drawing the text,
Example:
The following is an application in the actual project.
Displays a control of the current game player's blood, the background is the current blood volume picture, the displayed text is the current blood value
The specific effect is as follows:
The Code is as follows:
Code
/// <Summary>
/// Inherit and reset the background
/// Set according to the ratio of the current blood
/// </Summary>
/// <Param name = "e"> </param>
Protected override void OnPaintBackground (PaintEventArgs e)
{
Base. OnPaintBackground (e );
// Step One clears the background
E. Graphics. Clear (Color. White );
// Step Two re-paint the current background
Int length = (BloodNum/MaxBloodNum) * this. Width;
Using (SolidBrush brush = new SolidBrush (this. BackColor ))
{
E. Graphics. FillRectangle (brush, new Rectangle (0, 0, length, this. Height ));
}
// Step Three: Fill the unfilled part.
Using (SolidBrush brush = new SolidBrush (Color. DarkOliveGreen ))
{
E. Graphics. FillRectangle (brush, new Rectangle (length, 0, (this. Width-length), this. Height ));
}
// Step Four shows the current blood value
Using (Pen pen = new Pen (this. ForeColor ))
{
Using (SolidBrush brush = new SolidBrush (Color. Transparent ))
{
// Set the text area
E. Graphics. DrawString (this. BloodNum. ToString (), this. Font, brush, (this. Width/2)-5), 0 );
}
}
}