당신이 믿기를 바란다 : 당신이 필요로하는 것은 단 하나의 일반, CheckBox
!
BackgroundImage
에는 Zoom
의 기능이 있으며 몇 줄이면 멋진 느낌으로 포주 수 있습니다. 첫 번째 행은 원래 방법입니다. 이미지는 패딩하고 그림자가 : 두 번째 행에서 당신은 hoverImage 기능의 효과를 볼 수 있습니다
여기
그것을 만들 수있는 기능입니다 :
CheckBox imageCheckBox(Image img, Size size, string name)
{
CheckBox cbx = new CheckBox();
cbx.AutoSize = false;
cbx.Text = "";
cbx.CheckAlign = ContentAlignment.TopLeft;
cbx.BackgroundImageLayout = ImageLayout.Zoom;
cbx.Size = size;
cbx.BackgroundImage = img;
cbx.Name = name;
cbx.Padding = new System.Windows.Forms.Padding(3, 3, 0, 0);
cbx.BackColor = SystemColors.Control;
cbx.CheckedChanged += (s, e) =>
{
cbx.BackColor = cbx.Checked ? SystemColors.Control : SystemColors.Window;
};
cbx.MouseEnter += (s, e) =>
{
cbx.BackColor = cbx.Checked ? SystemColors.GradientActiveCaption :
SystemColors.GradientInactiveCaption;
};
cbx.MouseLeave += (s, e) =>
{
cbx.BackColor = cbx.Checked ? SystemColors.Control : SystemColors.Window;
};
cbx.Paint += (s, e) =>
{
if (cbx.ClientRectangle.Contains(cbx.PointToClient(Cursor.Position))
|| cbx.Checked))
e.Graphics.DrawRectangle(Pens.DarkGray, 0, 0,cbx.Width-1,cbx.Height-1);
};
return cbx;
}
것은 추가 프로젝트에 FlowLayoutPanel
과 같이 호출 :
size = new Size(200, 80);
flowLayoutPanel1.Controls.Add(imageCheckBox(yourImage1, size, "checkBox1"));
flowLayoutPanel1.Controls.Add(imageCheckBox(yourImage2, size, "checkBox2"));
flowLayoutPanel1.Controls.Add(imageCheckBox(yourImage3, size, "checkBox3"));
당신이 나중에 액세서하는 방법에 따라 보존 할 수 있습니다 CheckBoxes
에 대한 참조가 Dictionary
일 때 FLP
에 추가하기 전에 Name
에 액세스해야합니다.
은 내가 CheckedChanged
, MouseEnter
및 MouseLeave
이벤트를 코딩에 의해 결과가 이미 꽤 좋은 .. 조건부 Paint
이벤트에
을 테두리를 추가하여 약간의 스타일을 추가했습니다. 패딩을 해내하고 세련된 그림자가이 기능을 사용할 수 있습니다
// cbx.BackgroundImage = img;
cbx.BackgroundImage = hoverImage(img, size,new Padding(10, 10, 10, 10)) ;
우리가 새로운 비트 맵을 ceating대로와 함께 삭제 된 이벤트를 추가하십시오 imageCheckBox
코드
Bitmap hoverImage(Image img, Size size, Padding pad)
{
Bitmap bmp = new Bitmap(size.Width, size.Height);
// client rectangle
Rectangle cRect = new Rectangle(pad.Left, pad.Top,
size.Width - pad.Left - pad.Right, size.Height - pad.Top - pad.Bottom);
// image proportion
float fi = 1f * img.Width/img.Height;
// target size
int nw = fi >= 1 ? cRect.Width : (int)(1f * cRect.Width/fi);
int nh = (int)(1f * nw/fi);
if (nh > cRect.Height)
{
nh = cRect.Height;
nw = (int)(1f * nh * fi);
}
Size nSize = new Size(nw, nh);
Point dPoint = new Point(pad.Left + (cRect.Width - nw)/2,
pad.Top + (cRect.Height - nh)/2);
using (Graphics G = Graphics.FromImage(bmp))
{
Rectangle dRect = new Rectangle(dPoint, nSize);
Rectangle sRect = new Rectangle(Point.Empty, img.Size);
Rectangle shRect = new Rectangle(dRect.X - 3, dRect.Y - 3,
dRect.Width + 8, dRect.Height + 8);
for (int i = 0; i < 6; i++)
{
using (Pen pen = new Pen(Color.FromArgb(i * 15, Color.Black)))
G.DrawRectangle(pen, shRect);
shRect.Inflate(-1, -1);
}
G.DrawImage(img, dRect, sRect, GraphicsUnit.Pixel);
}
return bmp;
}
변경이 다른 이벤트는 GDI 리소스를 정리합니다.
cbx.Disposed += (s, e) => {
if (cbx.BackgroundImage != null) cbx.BackgroundImage.Dispose(); };
PictureBox 및 CheckBox가있는 usercontrol이 필요합니다. 너무 열심히하지 마십시오 – dotctor
완성 된 솔루션을 패딩과 그림자로 보았습니다! – TaW