| 1 |
using System; |
| 2 |
using System.Collections; |
| 3 |
using System.ComponentModel; |
| 4 |
using System.Drawing; |
| 5 |
using System.Data; |
| 6 |
using System.Diagnostics; |
| 7 |
using System.Windows.Forms; |
| 8 |
|
| 9 |
namespace nft.controls |
| 10 |
{ |
| 11 |
public enum ButtonStateImage :int{ Normal, Hover, Pressed, Disabled }; |
| 12 |
/// <summary> |
| 13 |
/// ImageButton の概要の説明です。 |
| 14 |
/// </summary> |
| 15 |
public class ImageButton : System.Windows.Forms.UserControl |
| 16 |
{ |
| 17 |
const int ButtonStateCounts = 4; |
| 18 |
/// <summary> |
| 19 |
/// 必要なデザイナ変数です。 |
| 20 |
/// </summary> |
| 21 |
protected ImageList imgList; |
| 22 |
protected Bitmap bmpStrip; |
| 23 |
protected Color colTransKey; |
| 24 |
private ImageList imgListInternal; |
| 25 |
protected bool imgListDispose = false; |
| 26 |
protected readonly short[] stateIndices = new short[4]; |
| 27 |
|
| 28 |
public ImageButton(){ |
| 29 |
this.Size = new Size(16,16); |
| 30 |
this.BackColor = Color.Transparent; |
| 31 |
} |
| 32 |
|
| 33 |
public ImageList ImageList { |
| 34 |
get{ return imgList; } |
| 35 |
set{ |
| 36 |
if (imgList != value){ |
| 37 |
if (imgListInternal != null && imgListInternal != imgList) { |
| 38 |
imgListInternal.Dispose(); |
| 39 |
} |
| 40 |
imgList = value; |
| 41 |
imgListInternal = imgList; |
| 42 |
colTransKey = imgList.TransparentColor; |
| 43 |
} |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
public Bitmap StripSource { |
| 48 |
get { return bmpStrip; } |
| 49 |
set { |
| 50 |
if (bmpStrip != value) { |
| 51 |
if (imgListInternal != null && imgListInternal != imgList) { |
| 52 |
imgListInternal.Dispose(); |
| 53 |
} |
| 54 |
bmpStrip = value; |
| 55 |
if (value != null) { |
| 56 |
imgListInternal = new ImageList(); |
| 57 |
int w = bmpStrip.Width >> 2; |
| 58 |
imgListInternal.ImageSize = new Size(w, bmpStrip.Height); |
| 59 |
imgListInternal.Images.AddStrip(bmpStrip); |
| 60 |
imgListInternal.TransparentColor = colTransKey; |
| 61 |
} |
| 62 |
imgList = null; |
| 63 |
} |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
public Color TransparentKey { |
| 68 |
get { |
| 69 |
if (imgListInternal != null) { |
| 70 |
return imgListInternal.TransparentColor; |
| 71 |
} else { |
| 72 |
return colTransKey; |
| 73 |
} |
| 74 |
} |
| 75 |
set { |
| 76 |
this.colTransKey = value; |
| 77 |
if (imgListInternal != null) { |
| 78 |
imgListInternal.TransparentColor = colTransKey; |
| 79 |
} |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
public bool DisposeImageList { |
| 84 |
get{ return imgListDispose; } |
| 85 |
set{ imgListDispose = value; } |
| 86 |
} |
| 87 |
|
| 88 |
public void SetModeImageIndices(int normal, int hover, int press, int disable) { |
| 89 |
stateIndices[(int)ButtonStateImage.Normal] = (short)normal; |
| 90 |
stateIndices[(int)ButtonStateImage.Hover] = (short)hover; |
| 91 |
stateIndices[(int)ButtonStateImage.Pressed] = (short)press; |
| 92 |
stateIndices[(int)ButtonStateImage.Disabled] = (short)disable; |
| 93 |
} |
| 94 |
|
| 95 |
public int IdxNormalImage { |
| 96 |
get { return stateIndices[(int)ButtonStateImage.Normal]; } |
| 97 |
set { stateIndices[(int)ButtonStateImage.Normal]=(short)value; } |
| 98 |
} |
| 99 |
public int IdxHoverImage { |
| 100 |
get { return stateIndices[(int)ButtonStateImage.Hover]; } |
| 101 |
set { stateIndices[(int)ButtonStateImage.Hover]=(short)value; } |
| 102 |
} |
| 103 |
public int IdxPressedImage { |
| 104 |
get { return stateIndices[(int)ButtonStateImage.Pressed]; } |
| 105 |
set { stateIndices[(int)ButtonStateImage.Pressed]=(short)value; } |
| 106 |
} |
| 107 |
public int IdxDisabledImage { |
| 108 |
get { return stateIndices[(int)ButtonStateImage.Disabled]; } |
| 109 |
set { stateIndices[(int)ButtonStateImage.Disabled]=(short)value; } |
| 110 |
} |
| 111 |
|
| 112 |
public Image GetImage(ButtonStateImage state){ |
| 113 |
try { |
| 114 |
return imgListInternal.Images[stateIndices[(int)state]]; |
| 115 |
}catch{ |
| 116 |
return SystemIcons.Error.ToBitmap(); |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
protected override void OnLoad(EventArgs e) { |
| 121 |
base.OnLoad(e); |
| 122 |
} |
| 123 |
|
| 124 |
/// <summary> |
| 125 |
/// 使用されているリソースに後処理を実行します。 |
| 126 |
/// </summary> |
| 127 |
protected override void Dispose( bool disposing ) |
| 128 |
{ |
| 129 |
base.Dispose( disposing ); |
| 130 |
if (imgListDispose && imgListInternal != null) |
| 131 |
imgListInternal.Dispose(); |
| 132 |
} |
| 133 |
|
| 134 |
protected override void OnPaint(PaintEventArgs e) { |
| 135 |
base.OnPaint (e); |
| 136 |
Graphics g = e.Graphics; |
| 137 |
ButtonStateImage bs = ButtonStateImage.Normal; |
| 138 |
|
| 139 |
if(Enabled){ |
| 140 |
Point mp = PointToClient(MousePosition); |
| 141 |
if(mp.X>=0 && mp.X<Width && mp.Y>=0 && mp.Y<Height){ |
| 142 |
if(Control.MouseButtons==MouseButtons.Left) |
| 143 |
bs = ButtonStateImage.Pressed; |
| 144 |
else |
| 145 |
bs = ButtonStateImage.Hover; |
| 146 |
} |
| 147 |
} else |
| 148 |
bs = ButtonStateImage.Disabled; |
| 149 |
g.DrawImage(GetImage(bs),0,0,Width,Height); |
| 150 |
} |
| 151 |
|
| 152 |
protected override void OnMouseEnter(EventArgs e) { |
| 153 |
base.OnMouseEnter(e); |
| 154 |
Invalidate(); |
| 155 |
} |
| 156 |
|
| 157 |
protected override void OnMouseLeave(EventArgs e) { |
| 158 |
base.OnMouseLeave(e); |
| 159 |
Invalidate(); |
| 160 |
} |
| 161 |
|
| 162 |
protected override void OnMouseDown(MouseEventArgs e) { |
| 163 |
base.OnMouseDown(e); |
| 164 |
if (e.Button == MouseButtons.Left) |
| 165 |
Invalidate(); |
| 166 |
} |
| 167 |
|
| 168 |
protected override void OnMouseUp(MouseEventArgs e) { |
| 169 |
base.OnMouseUp(e); |
| 170 |
if (e.Button == MouseButtons.Left) |
| 171 |
Invalidate(); |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
} |
| 176 |
} |