WindowMobile之设置窗体背景图片
本文的开发环境
操作系统:xp
程序开发环境vss2008+windowMobile6.1Professional
概述:
在实际手机开发过程中,对于UI窗体背景设计时,我们常常想到是一张和应用相关的背景图,或者渐变的素雅的图片,然而默认的WindowMobile的背景设置仅仅提供了单色背景色配置项。下面介绍的是我们如何通过GDi+进行背景图的绘制。
设置WindowMobile窗体的背景图的原理是在窗体进行OnPaint时,对当前的窗体的Graphics进行背景图片会话
举例:
设置Form的背景图片
代码
/// <summary>
/// 重新渲染背景
/// </summary>
/// <param name="e"></param>
protected override void OnPaint(PaintEventArgs e)
{
//背景图片路径
string strExeDir = Assembly.GetExecutingAssembly().GetName().CodeBase; //获得可执行文件的全路径
string strDir = Path.GetDirectoryName(strExeDir);
string imagePath = Path.Combine(strDir, @"Picture/bru.gif");
using (Image backGroupImage = new Bitmap(imagePath))
{
e.Graphics.Clear(Color.Black);
//SourceRect 为整个图片
Rectangle srcRect = new Rectangle(0, 0, backGroupImage.Width, backGroupImage.Height);
Rectangle desRect = new Rectangle(0, 0, e.ClipRectangle.Width, e.ClipRectangle.Height);
desRect.Location = new Point(0, 0);
e.Graphics.DrawImage(backGroupImage, desRect, srcRect, GraphicsUnit.Pixel);
}
/// 重新渲染背景
/// </summary>
/// <param name="e"></param>
protected override void OnPaint(PaintEventArgs e)
{
//背景图片路径
string strExeDir = Assembly.GetExecutingAssembly().GetName().CodeBase; //获得可执行文件的全路径
string strDir = Path.GetDirectoryName(strExeDir);
string imagePath = Path.Combine(strDir, @"Picture/bru.gif");
//开始进行渲染窗口的背景
using (Image backGroupImage = new Bitmap(imagePath))
{
//清空当前的背景
e.Graphics.Clear(Color.Black);
//绘制图片并缩放.
//SourceRect 为整个图片
Rectangle srcRect = new Rectangle(0, 0, backGroupImage.Width, backGroupImage.Height);
//设置目标区域
Rectangle desRect = new Rectangle(0, 0, e.ClipRectangle.Width, e.ClipRectangle.Height);
desRect.Location = new Point(0, 0);
//绘制背景图片.
e.Graphics.DrawImage(backGroupImage, desRect, srcRect, GraphicsUnit.Pixel);
}
//base.OnPaint(e);
}