首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 嵌入开发 > WinCE >

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

2012-07-29 
WinCE5.0下实现透明背景按钮(.net C#) 个人补充针对上篇文章有童鞋提出一些疑问:1.Command”(是否缺少 usin

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

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

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

2.没有创建Command枚举类型

3.编译不通过等

 

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

 

界面后台类:

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);            }        }    }}
 
 

 

 

解决方案试图:

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

 

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

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

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

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

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

 

1楼csfinal9昨天 16:59
图片挂了,c#在wince下的速度如何呢,我用的是mfc,在ce6.0下,贴图还有点慢
Re: sat47229151917分钟前
回复csfinal9n不卡屏,肉眼分辨不出来,界面在不停的重绘。

热点排行