[Windows Mobile]在表單中顯示背景圖片

  • 8043
  • 0
  • 2013-04-15

[Windows Mobile]在表單中顯示背景圖片

1. 問題描述

在之前的文章中,[Windows Mobile]小技巧(4)-Splash Screen 中,Splash Screen 表單顯示圖片是使用 PictureBox,

是否有辦法直接在表單中顯示背景圖片

 

2. 方法

在 Windows Forms 應用程式中,Form 的屬性中有 BackgroundImage,如下圖所示,可以用來設定背景圖片,

但在智慧型裝置專案的表單中,並沒有此屬性

image

 

在此想到的方法是寫程式將圖片描繪上去,首先我們想把圖片加入專案中,並將圖片的屬性,複製到輸出目錄改為永遠複製。

image  image image

 

接著撰寫以下程式碼,將圖片讀入,並且覆寫 OnPaintBackground 方法,其功能為繪製控制項的背景;將圖片顯示部分撰寫於其中。 

using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace SmartDeviceBI
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        Bitmap bmpBackgroundImage;
        Rectangle RectBackgroundImage;

        private void Form1_Load(object sender, EventArgs e)
        {
            bmpBackgroundImage = new Bitmap(@"\Program Files\SmartDeviceBI\Water.jpg");
            RectBackgroundImage = new Rectangle(0, 0, bmpBackgroundImage.Width, bmpBackgroundImage.Height);
        }

        // 覆寫 OnPaintBackground 事件
        protected override void OnPaintBackground(PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            g.DrawImage(bmpBackgroundImage, this.ClientRectangle, RectBackgroundImage, GraphicsUnit.Pixel);
        }
    }
}

3. 執行結果

image