📄 slidebutton.cs
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Imaging;
//自定义控件,滑动按钮
namespace iPhoneUI
{
public class SlideButton
{
private Rectangle clientArea; //按钮矩形区域
private Form owner; //父窗体
private Bitmap image; //按钮图片
private Bitmap imageDown; //摁下后的按钮
public bool pushed = false; //摁没摁下的标志
private Point location; //按钮位置
private Point start; //鼠标摁下时的位置
int mousePos = 0; //鼠标在按钮之上的位置
private int leftLimit = 16; //按钮滑行的左边界
private int rightLimit = 220; //按钮滑行的右边界
private System.Windows.Forms.Timer timeAnimation = new Timer(); //定时器
public bool unLock = false; //解锁标识
public SlideButton(Form owner, Point location)
{
this.location = location;
this.clientArea = new Rectangle(location.X, location.Y, 30, 30);
this.owner = owner;
this.Attach(owner);
timeAnimation.Interval = 1; //计时器的时间间间隔
timeAnimation.Enabled = false; // false; 初始的时候计时器不动
//this.timeAnimation.Tick += new System.EventHandler(this.timeAnimation_Tick);
}
private void timeAnimation_Tick(object sender, EventArgs e)//这个函数没有用到
{
int x = (start.X - 20);//阶段性的前进
Move(x); //向起点移动
}
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;
}
timeAnimation.Enabled = true;//松手之后让滑动按钮往回退
}
void Move(int x)//按钮滑动,完成拖拽的动作,不需要重绘就可以显示吗? x:目标移动点
{
int shift = x - start.X;
int newX = (this.clientArea.X + shift);// -mousePos; 得到一个绝对的坐标
if (newX <= leftLimit)//位置不能小于左边界
{
newX = leftLimit;
timeAnimation.Enabled = false;
}
if (newX + this.clientArea.Width >= rightLimit)//位置不能大于右边界
{
newX = rightLimit - this.clientArea.Width;
unLock = true;
}
this.clientArea = new Rectangle(newX, clientArea.Y, this.clientArea.Width, this.clientArea.Height);
//owner.Invalidate(new Rectangle(0, 258, 240, 70));
start.X = x;
}
void owner_MouseMove(object sender, MouseEventArgs e)//鼠标移动时的事件响应
{
Move(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 (timeAnimation.Enabled)
{
int x = (start.X - 20);//倒退回去的计时
Move(x);
}
ImageAttributes attrib = new ImageAttributes();
Color color = GetTransparentColor(image);
attrib.SetColorKey(color, color);
if (!pushed || imageDown == null)
gx.DrawImage(image, clientArea, 0, 0, clientArea.Width, clientArea.Height, GraphicsUnit.Pixel, attrib);
else
gx.DrawImage(imageDown, clientArea, 0, 0, clientArea.Width, clientArea.Height, GraphicsUnit.Pixel, attrib);
}
private Color GetTransparentColor(Bitmap image)
{
return image.GetPixel(0, 0);
}
internal void ResetPosition()
{
timeAnimation.Enabled = true;
Move(start.X - 20);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -