⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 imagebutton.cs

📁 PDA上的透明解决方法,找了很久才找到的,WINDOW MOIBLE 5.0以上才支持
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Imaging;


namespace AlphaBlendTest
{
    public class ImageButton
    {
        private Rectangle clientArea;
        private Form owner;
        private Bitmap image;
        private Bitmap imageDown;
        private bool pushed = false;
        private Point location;
        private Point start;
        int mousePos = 0;
        private int leftLimit = 16;
        private int rightLimit = 220;

        public ImageButton(Form owner, Point location)
        {
            this.location = location;
            this.clientArea = new Rectangle(location.X, location.Y, 30, 30);
            this.owner = owner;
            this.Attach(owner);
        }

        public Bitmap Image
        {
            get { return image;}
            set 
            { 
                image = value;
                if (image != null)
                {
                    clientArea = new Rectangle(location.X, location.Y, image.Width, image.Height);
                }
            }
        }

         public Bitmap ImageDown
        {
            get { return imageDown;}
            set { imageDown = value; }
        }

        private void Attach(Form owner)
        {            
            owner.MouseDown += new MouseEventHandler(owner_MouseDown);
            owner.MouseMove += new MouseEventHandler(owner_MouseMove);
            owner.MouseUp += new MouseEventHandler(owner_MouseUp);
        }

        void owner_MouseUp(object sender, MouseEventArgs e)
        {
            if (pushed)
            {
                using (Graphics gx = owner.CreateGraphics())
                {
                    this.pushed = false;
                    this.Paint(gx);
                }
                pushed = false;
            }
        }

        void owner_MouseMove(object sender, MouseEventArgs e)
        {
            int shift = e.X - start.X;
            int newX = (this.clientArea.X + shift);// -mousePos;
            if (newX <= leftLimit)
            {
                newX = leftLimit;
            }
            if (newX + this.clientArea.Width >= rightLimit)
            {
                newX = rightLimit - this.clientArea.Width;
            }

            this.clientArea = new Rectangle(newX, clientArea.Y, this.clientArea.Width, this.clientArea.Height);
            owner.Invalidate(new Rectangle(0, 258, 240, 70));
            start.X = e.X;
        }

        void owner_MouseDown(object sender, MouseEventArgs e)
        {
            if (this.HitTest(e.X, e.Y))
            {
                using (Graphics gx = owner.CreateGraphics())
                {
                    this.pushed = true;
                    this.Paint(gx);
                }
                start = new Point(e.X, e.Y);
                mousePos = start.X - clientArea.X; 
            }
        }

        public bool HitTest(int x, int y)
        {
            return clientArea.Contains(x, y);
        }

        public void Paint(Graphics gx)
        {
            if (!pushed || imageDown == null)
            {
                ImageAttributes attrib = new ImageAttributes();
                Color color = GetTransparentColor(image);
                attrib.SetColorKey(color, color);
                gx.DrawImage(image, clientArea, 0, 0, clientArea.Width, clientArea.Height, GraphicsUnit.Pixel, attrib);                
            }
            else
            {
                ImageAttributes attrib = new ImageAttributes();
                Color color = GetTransparentColor(imageDown);
                attrib.SetColorKey(color, color);
                gx.DrawImage(imageDown, clientArea, 0, 0, clientArea.Width, clientArea.Height, GraphicsUnit.Pixel, attrib);                
            }
        }

        private Color GetTransparentColor(Bitmap image)
        {
            return image.GetPixel(0, 0);
        }
    
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -