WinCE5.0下实现透明背景按钮(.net C#) 个人补充

针对上篇文章有童鞋提出一些疑问:

1.Command”(是否缺少 using 指令或程序集引用

2.没有创建Command枚举类型

3.编译不通过等

 

我先把我成功运行的代码贴出来给童鞋看下,略有改 。

 原作者的思路理解:

初始化参数 路径 背景图片 背景标题 字体颜色 字体类型
设置按钮 属性 将 当前this,路径,坐标,图片名称,显示文字,当前字体颜色,点击后字体颜色,指令传过去 将类中的属性赋值,并计算出文字坐标 点击后文字字体颜色,按钮背景颜色等 相应属性赋值

页面开始重绘
先绘制背景与背景标题
根据按钮是否选择状态 循环绘制按钮

在窗体鼠标移动事件中 不断的判断当前鼠标是否在已经选中按钮 鼠标的坐标在所绘制的按钮区域内

如果在鼠标绘制的区域点击下去 将触发判断 循环判断在那个按钮区域内点击 并激活该按钮的是否选中属性  并将该按钮属性 赋值给当前的临时按钮

在鼠标释放的时候 触发 鼠标坐标是否在点击按钮的区域内 如果是则执行 赋给改按钮的指令,在结构中可以扩展该指令

 

界面后台类:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Reflection;
namespace DZB
{
    public partial class ImageButton : Form
    {
        public enum Command
        {
            cmd1 = 0,//无操作
            cmd2,//第一项操作
            cmd3,//第二项操作
            cmd4,//可自己扩展
            max
        }
        private IList<IButton> btnlist;
        private IButton capturedButton;
        private Font windowFont = new Font(FontFamily.GenericSansSerif, 12, FontStyle.Regular);
        private string CurrentPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);
        private Bitmap bg, title;//背景、标题图片
        public ImageButton()
        {
            InitializeComponent();
        }
        //加载事件 初始化页面元素
        private void ImageButton_Load(object sender, EventArgs e)
        {
            title = new Bitmap(CurrentPath + @"\Resources\title.bmp");
            bg = new Bitmap(CurrentPath + @"\Resources\mainbg.bmp");
            btnlist = new List<IButton>();
            btnlist.Add(new IButton(this, CurrentPath + @"\Resources\", 100, 100, "button.bmp", "按钮1", Color.White, Color.Blue, Command.cmd1));
            btnlist.Add(new IButton(this, CurrentPath + @"\Resources\", 100, 200, "button.bmp", "按钮2", Color.White, Color.Blue, Command.cmd1));
            btnlist.Add(new IButton(this, CurrentPath + @"\Resources\", 100, 300, "button.bmp", "按钮3", Color.White, Color.Blue, Command.cmd2));
            btnlist.Add(new IButton(this, CurrentPath + @"\Resources\", 100, 400, "button.bmp", "按钮4", Color.White, Color.Blue, Command.cmd2));
            btnlist.Add(new IButton(this, CurrentPath + @"\Resources\", 400, 100, "button.bmp", "按钮5", Color.White, Color.Blue, Command.cmd2));
            btnlist.Add(new IButton(this, CurrentPath + @"\Resources\", 400, 200, "button.bmp", "按钮6", Color.White, Color.Blue, Command.cmd2));
            btnlist.Add(new IButton(this, CurrentPath + @"\Resources\", 400, 300, "button.bmp", "按钮7", Color.White, Color.Blue, Command.cmd2));
            btnlist.Add(new IButton(this, CurrentPath + @"\Resources\", 400, 400, "button.bmp", "按钮8", Color.White, Color.Blue, Command.cmd3));
        }
        private void ImageButton_Paint(object sender, PaintEventArgs e)
        {
            // Buttons
            Graphics graphics;
            graphics = e.Graphics;
            graphics.DrawImage(title, 0, 0);
            graphics.DrawImage(bg, 0, 63);
            // Edit line
            foreach (IButton btn in btnlist)
            {
                btn.Render(graphics);
            }
        }
        protected override void OnPaintBackground(PaintEventArgs paintArgs)
        {
            //gr.DrawImage(img, 0, 0);
            base.OnPaintBackground(paintArgs);
        }
        private void ImageButton_MouseDown(object sender, MouseEventArgs e)
        {
            foreach (IButton btn in btnlist)
            {
                if (btn.IsHit(e.X, e.Y))
                {
                    btn.IsSelected = true;
                    capturedButton = btn;
                    break;
                }
            }
        }
        private void ImageButton_MouseMove(object sender, MouseEventArgs e)
        {
            if (capturedButton != null)
            {
                capturedButton.IsSelected = capturedButton.IsHit(e.X, e.Y);
            }
        }
        private void ImageButton_MouseUp(object sender, MouseEventArgs e)
        {
            if (capturedButton != null)
            {
                if (capturedButton.IsHit(e.X, e.Y))
                    DoCommand(capturedButton.Cmd);
                try
                {
                    capturedButton.IsSelected = false;
                    capturedButton = null;
                }
                catch
                {
                }
            }
        }
        private void DoCommand(Command command)
        {
            switch (command)
            {
                case Command.cmd1://0
                    // this.Close();
                    break;
                case Command.cmd2://1
                    MessageBox.Show("cmd2");
                    break;
                case Command.cmd3://2
                    this.Close();
                    break;
                case Command.cmd4://3
                    {
                        //Line nl = new Line();
                        //nl.Show();
                    }
                    break;
                default:
                    break;
            }
        }
        
    }
}

 

 

IButton.cs 类:

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Imaging;
namespace DZB
{
    public class IButton
    {
  
        private Control MainForm;
        private Font sFont = new Font(FontFamily.GenericSansSerif, 16, FontStyle.Bold);
        private Font sFont_ = new Font(FontFamily.GenericSansSerif, 16, FontStyle.Bold);
        private bool IsSelectedValue;
        private DZB.ImageButton.Command ButtonCommand;
        private Rectangle selfRec;
        private string _caption;
        private float _cx, _cy;//画标题的位置
        private Bitmap btnbg, btnbg_;
        private string _CurrentPath;
        Color transpColor = Color.FromArgb(255, 0, 255);//资源文件的透明色
        ImageAttributes imageAttr;
        Color _ncolor, _fcolor;
        Graphics g, g_;
        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="form">容器名称</param>
        /// <param name="CurrentPath">当前程序路径</param>
        /// <param name="x">按钮位置X</param>
        /// <param name="y">按钮位置Y</param>
        /// <param name="bgfilename">按钮图片名称</param>
        /// <param name="caption">标题</param>
        /// <param name="nColor">正常颜色</param>
        /// <param name="fColor">激活颜色</param>
        /// <param name="cmd">指令</param>
        public IButton(Control form, string CurrentPath, int x, int y, string bgfilename, string caption, Color nColor, Color fColor, DZB.ImageButton.Command cmd)
        {
            MainForm = form;
            _ncolor = nColor;
            _fcolor = fColor;
            ButtonCommand = cmd;
            _caption = caption;
            _CurrentPath = CurrentPath;
            string bg = CurrentPath + bgfilename;
            string bg_ = CurrentPath + System.IO.Path.GetFileNameWithoutExtension(bg) + "_.bmp";
            btnbg = new Bitmap(bg);
            selfRec = new Rectangle(x, y, btnbg.Width, btnbg.Height);
            g = Graphics.FromImage(btnbg);
            _cx = (219 - g.MeasureString(_caption, sFont).Width) / 2;
            _cy = (48 - g.MeasureString(_caption, sFont).Height) / 2;
            btnbg_ = new Bitmap(bg_);
            g_ = Graphics.FromImage(btnbg_);
            imageAttr = new ImageAttributes();
            imageAttr.SetColorKey(transpColor, transpColor);
        }
        public void Render(Graphics graphics)
        {
            if (IsSelectedValue)
            {//点下的状态
                g_.DrawString(_caption, sFont_, new SolidBrush(_fcolor), _cx, _cy);
                graphics.DrawImage(btnbg_, selfRec, 0, 0, selfRec.Width, selfRec.Height, GraphicsUnit.Pixel, imageAttr);
            }
            else
            {
                g.DrawString(_caption, sFont, new SolidBrush(_ncolor), _cx, _cy);
                graphics.DrawImage(btnbg, selfRec, 0, 0, selfRec.Width, selfRec.Height, GraphicsUnit.Pixel, imageAttr);
            }
        }
        private static Color GetTransparentColor(Image image)
        {
            return ((Bitmap)image).GetPixel(image.Width - 1, image.Height - 1);
        }
        public bool IsHit(int x, int y)
        {
            return (x >= selfRec.X &&
                    x < selfRec.X + selfRec.Width &&
                    y >= selfRec.Y &&
                    y < selfRec.Y + selfRec.Height);
        }
        public bool IsSelected
        {
            get
            {
                return IsSelectedValue;
            }
            set
            {
                Graphics graphics;
                if (value != IsSelectedValue)
                {
                    IsSelectedValue = value;
                    // Redraw right away
                    graphics = MainForm.CreateGraphics();
                    this.Render(graphics);
                    graphics.Dispose();
                }
            }
        }
        public DZB.ImageButton.Command Cmd
        {
            get
            {
                return (ButtonCommand);
            }
        }
    }
}
 
 

 

 

解决方案试图:

 

最后我将我的工程打包,下载地址为:

http://download.csdn.net/download/sat472291519/4431268

最后补充一点 如果在指定目录下找不到图片 则表明,你未将图片放到PDA目录下面

 

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
WinCE5.0实现透明背景按钮(.net C#) 需要在wince5.0实现图形化界面,用于功能导航,用过GPS导航就知道了,类似的界面。众所周知gif,PNG等图片,都是可以实现透明背景的,在win下这应该不是个问题。但在ce5.0下,无论是透明背景的ICON、PNG、GIF都无法简单实现透明背景,alpha通道会丢失。网上搜索了些资料,当然也有办法解决,大家可以查一下,但性能较低。最终按微软SDK自带的一个计算器的源码思路解决了透明背景按钮问题。 要点: 1.如何画透明背景的BMP 2.如何画按钮实现事件 思路及解决: 1.画按钮的思路:   ImageAttributes imageAttr=new ImageAttributes();   imageAttr.SetColorKey(Color.FromArgb(255, 0, 255),Color.FromArgb(255, 0, 255));   然后使用Graphics.DrawImage(,,,,,imageAttr)函数在指定的位置上画出透明的图片。   2. 事件驱动的思路: 事先定义好各按钮的指令   public enum Command { cmd1 = 0,//无操作 cmd2,//第一项操作 cmd3,//第二项操作 cmd4,//可自己扩展 max } 在创建按钮的同时明确以下几个参数 容器控件、资源存放的目录、X坐标、Y坐标、背景图片、按钮的标题、非激活时的文字颜色、激活时的文字颜色、触发的指令。创建窗口,针对窗体事件做如下定义 •在Form_Load时生成按钮, •在Form_OnPaint时使用按钮自身的Render函数根据自己状态(有没有被点中)重画, •在MouseDown时判断点击位置是否在某个按钮的内部,如果是在它内部就改变它的状态,设置状态的同时调用窗口控件的Graphics局部重画这个按钮,     •在Form_MouseUp时判断现在的位置是不是在按钮内部,如果按下了,抬起时又移出了范围则不处理。如果按下与抬起都是在同一个按钮的边界内部则执行这个按钮所设置的指令。 以下是我使用的资源,将作为按钮的图片需要透明的部分设置成RGB(255,0,255),那种非常刺眼的颜色。以下图片可以另存为BMP使用,设置的分辨是800*600的CE设备。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值