现在的位置: 首页 > 综合 > 正文

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

2014年05月06日 ⁄ 综合 ⁄ 共 8302字 ⁄ 字号 评论关闭

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

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目录下面

 

抱歉!评论已关闭.