?? imagebutton.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 ImageButton
{
internal Rectangle clientArea; //控件區域
internal Form owner; //控件所屬窗體
internal Bitmap image; //未點擊前圖案
internal Bitmap imageDown; //點擊后的圖案
internal bool pushed = false; //是否摁下的標志位
internal Point location; //該控件所處的位置
internal Point start; //鼠標點擊的位置
internal bool pushedOneTime = false; //是否點擊過一次
public bool IsPressedOneTime
{
get { return pushedOneTime; }
set { pushedOneTime = value; }
}
public ImageButton(Form owner,Point location)//所屬窗口,位置來構造
{
this.owner = owner;
Attach(owner); //掛按鍵響應的事件處理
this.location = location;
this.clientArea = new Rectangle(location.X, location.Y, 30, 30);
}
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; }
}
public bool HitTest(int x, int y)//確定一個點在不在clientArea的范圍內
{
return clientArea.Contains(x, y);
}
private void Attach(Form owner)
{
owner.MouseDown += new MouseEventHandler(owner_MouseDown);
owner.MouseUp += new MouseEventHandler(owner_MouseUp);
}
public virtual void owner_MouseUp(object sender, MouseEventArgs e)//鼠標彈起事件處理
{
if (pushed)
{
using (Graphics gx = owner.CreateGraphics())
{
this.pushed = false;
this.Paint(gx);
}
pushed = false;
}
// SendMessage(ButtonCode, "Button Pressed");
}
public virtual void owner_MouseDown(object sender, MouseEventArgs e)//鼠標按下事件處理
{
if (this.HitTest(e.X, e.Y))
{
using (Graphics gx = owner.CreateGraphics())
{
this.pushed = true;
IsPressedOneTime = true;
this.Paint(gx);
}
start = new Point(e.X, e.Y);
}
}
public void Paint(Graphics gx)//把這個按鍵畫出來,參數傳進來外面graphics對象
{
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);
}
internal Color GetTransparentColor(Bitmap image)
{
return image.GetPixel(0, 0);
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -