[Windows Mobile]使用 AlphaMobileControls 實現透明控制項

本文介绍如何在智能设备项目中使用AlphaMobileControls实现透明控件。通过下载并使用AlphaMobileControls,配合特定的编程技巧,可在Windows Mobile环境中创建带有透明效果的按钮和标签等控件。

1. 簡介

在 Windows Form Application 中,想要讓控制項透明,只要設定一些簡單的屬性 ( 例如 BackColor = Color.Transparent ) 即可,但在智慧型裝置專案中,就不是簡單的事情,使用 AlphaBlend API 是最常見的作法,而本文介紹使用 AlphaMobileControls 控制項來達成。

2. 方法

2.1 下載與使用

此處 下載 AlphaMobileControls

image

新增智慧型裝置專案,並且加入現有專案,把 AlphaMobileControls 專案

image image image

將 AlphaMobileControls 編譯後,在工具箱中就會看到 AlphaMobileControls 元件,接著只要把元件拖曳到 Form 上就可以使用了

image image

不過由於程式碼中,會用到 AlphaImage 類別,因此將 AlphaMobileControls.dll 加入參考中

image image

並於程式碼中 using

  1. using AlphaMobileControls;
using AlphaMobileControls;

2.2 加入圖檔

將背景透明的圖檔加入專案內,並且在屬性裡,設定為嵌入資源

image

2.3 程式撰寫

表單配置如下圖所示,由上往下為 三個為 AlphaImageButton,三個 PictureBox,一個 AlphaImageButton,一個 AlphaLabel

image

以下程式碼功能為 三個為 AlphaImageButton,三個 PictureBox 顯示相同的圖檔,而最底下的 AlphaImageButton 點選後,AlphaLabel 顯示 Button Clicked!

  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using AlphaMobileControls;
  10. namespace SmartDeviceProject1
  11. {
  12. public partial class Form1:AlphaForm //將Form1繼承AlphaForm
  13. {
  14. public Form1()
  15. {
  16. InitializeComponent();
  17. //表單中最上面三個alphaImageButton載入圖檔
  18. alphaImageButton2.BackgroundImage=AlphaImage.CreateFromResource("SmartDeviceProject1.Resources.1.png" );
  19. alphaImageButton3.BackgroundImage=AlphaImage.CreateFromResource("SmartDeviceProject1.Resources.2.png" );
  20. alphaImageButton4.BackgroundImage=AlphaImage.CreateFromResource("SmartDeviceProject1.Resources.3.png" );
  21. alphaLabel1.Text="AlphaMobileControls" ;
  22. //表單最底下alphaImageButton載入圖檔
  23. alphaImageButton1.BackgroundImage=AlphaImage.CreateFromResource("SmartDeviceProject1.Resources.Btn1.png" );
  24. alphaImageButton1.ActiveBackgroundImage=AlphaImage.CreateFromResource("SmartDeviceProject1.Resources.Btn1_Pushed.png" );
  25. alphaImageButton1.DisabledBackgroundImage=AlphaImage.CreateFromResource("SmartDeviceProject1.Resources.Btn1_Disabled.png" );
  26. }
  27. //當alphaImageButton1Click時
  28. private void alphaImageButton1_Click( object sender,EventArgse)
  29. {
  30. alphaLabel1.Text="ButtonClicked!" ; //alphaLabel1顯示ButtonClicked!
  31. }
  32. }
  33. }
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;

using AlphaMobileControls;

namespace SmartDeviceProject1
{
    public partial class Form1 : AlphaForm  // 將 Form1 繼承 AlphaForm
    {
        public Form1()
        {
            InitializeComponent();
            // 表單中最上面三個 alphaImageButton 載入圖檔
            alphaImageButton2.BackgroundImage = AlphaImage.CreateFromResource("SmartDeviceProject1.Resources.1.png");
            alphaImageButton3.BackgroundImage = AlphaImage.CreateFromResource("SmartDeviceProject1.Resources.2.png");
            alphaImageButton4.BackgroundImage = AlphaImage.CreateFromResource("SmartDeviceProject1.Resources.3.png");

            alphaLabel1.Text = "Alpha Mobile Controls";
            // 表單最底下 alphaImageButton 載入圖檔
            alphaImageButton1.BackgroundImage = AlphaImage.CreateFromResource("SmartDeviceProject1.Resources.Btn1.png");
            alphaImageButton1.ActiveBackgroundImage = AlphaImage.CreateFromResource("SmartDeviceProject1.Resources.Btn1_Pushed.png");
            alphaImageButton1.DisabledBackgroundImage = AlphaImage.CreateFromResource("SmartDeviceProject1.Resources.Btn1_Disabled.png");
        }

        // 當 alphaImageButton1 Click 時
        private void alphaImageButton1_Click(object sender, EventArgs e)
        {
            alphaLabel1.Text = "Button Clicked!";  // alphaLabel1 顯示 Button Clicked!
        }
    }
}

執行結果

最上面三個 AlphaImageButton,圖檔透明的部份顯示透明,但三個 PictureBox 則顯示白色。下方AlphaImageButton 點選後,AlphaLabel 顯示 Button Clicked!

image image

3. 結語

本文提供 AlphaMobileControls 實現透明控制項,而實際上常見的使用方式為 AlphaBlend 與 Imaging API,以下提供一些網址,讓對透明控制項興趣的可以參考

Imaging Structures

AlphaBlend

Alphablending with NETCF

Transparent label in compact framework

UI Framework for .NET Compact Framework 3.5

iPhone UI

4. 參考

Windows Mobile实现透明控件

C#實現label和按鈕透明

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值