图片GUI C#(使用图片框)
是否有方法在图片框中的图片上执行单击事件?? (这样我就可以点击图片,让事情发生。) 我试过使用标签,但很难使用。 谢谢你的帮助 --标签代码--图片GUI C#(使用图片框),c#,user-interface,graphics,picturebox,C#,User Interface,Graphics,Picturebox,是否有方法在图片框中的图片上执行单击事件?? (这样我就可以点击图片,让事情发生。) 我试过使用标签,但很难使用。 谢谢你的帮助 --标签代码-- 简单的回答是从Visual Studio的设计视图双击PictureBox控件,以便Visual Studio创建一个事件处理程序,然后订阅控件的单击事件 或者,您可以从编码中使用: 试试这个: namespace Today { public partial class Form1 : Form { public F
简单的回答是从Visual Studio的
设计视图
双击PictureBox
控件,以便Visual Studio创建一个事件处理程序
,然后订阅控件的单击事件
或者,您可以从编码中使用:
试试这个:
namespace Today
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
pictureBox1.Click += pictureBox1_Click;
}
private void pictureBox1_Click(object sender, EventArgs e)
{
//Code Here
}
}
}
您可以为图片框的单击事件创建事件处理程序。首先,在构造函数中“订阅”事件:
public Form1()
{
InitializeComponent();
pictureBox1.Click += pictureBox1_Click;
}
然后,通过插入以下内容,可以创建一个名为pictureBox1\u Click
的方法:
private void pictureBox1_Click(object sender, EventArgs e)
{
//Code
}
编辑:当您单击图片框内显示的图像时,单击
事件将触发。或者,您可以订阅MouseDown
或MouseUp
有两种可能,您的PictureBox
可以显示比PictureBox
小的图像,在这种情况下,您希望仅单击图像的矩形将触发事件单击
。另一种可能性是,当您的图像是透明图像(如车轮?)时,在这种情况下,仅单击图像的非透明部分将触发事件,换句话说,单击透明区域不会触发事件。下面的代码假设您的情况是后者,它当然比前者更难实现:
public class PictureBoxX : PictureBox
{
static PropertyInfo ImageRectangle;
static PictureBoxX() {
ImageRectangle = typeof(PictureBox).GetProperty("ImageRectangle",
BindingFlags.NonPublic | BindingFlags.Instance);
}
protected override void WndProc(ref Message m)
{
if (m.Msg == 0x202 || m.Msg == 0x201) {
var imageRect = (Rectangle)ImageRectangle.GetValue(this, null);
using (var bm = new Bitmap(imageRect.Width, imageRect.Height,
System.Drawing.Imaging.PixelFormat.Format32bppPArgb))
using(var g = Graphics.FromImage(bm)) {
int x = (int) (m.LParam.ToInt64() & 0xffff) - imageRect.X;
int y = (int) (m.LParam.ToInt64() >> 16) - imageRect.Y;
var rect = imageRect;
rect.Location = Point.Empty;
g.DrawImage(Image, rect);
if (!imageRect.Contains(x, y)) return;
else {
Color c = bm.GetPixel(x, y);
//I choose the threshold 30 for the alpha channel
//the ideal threshold is 0, it's up to you.
if (c.A < 30) return;
}
}
}
base.WndProc(ref m);
}
}
尝试上面的代码,您将看到仅单击图像的不透明部分将触发单击
事件,并显示消息“单击图像!”
。请注意,BindingFlags
位于命名空间System.Reflection
,您可以使用System.Reflection添加代码>为了方便。我的意思是像点击屏幕上的图片一样picturebox@MahchinLizard:是的,当您单击picturebox上的图片时,会触发此单击事件。我知道有没有方法单击picturebox中的图片并执行某些操作。(对于gui)@MahchinLizard图片盒包含您在其中显示的图片。如果您单击该图片,将触发此事件,因此您可以使用方法pictureBox1\u click
。但是您是否可以选择一个图像,以便如果单击它,您可以重新表述它?如果要在单击PictureBox
时选择其他图像:((PictureBox)发件人)。image=//要设置的图像
。如果要获取单击的图像:image clickedImage=((PictureBox)sender).image代码>您的问题确实有点让人困惑。这是一个固定的图像,你想打破热点,以执行特定的功能或。。。。
public class PictureBoxX : PictureBox
{
static PropertyInfo ImageRectangle;
static PictureBoxX() {
ImageRectangle = typeof(PictureBox).GetProperty("ImageRectangle",
BindingFlags.NonPublic | BindingFlags.Instance);
}
protected override void WndProc(ref Message m)
{
if (m.Msg == 0x202 || m.Msg == 0x201) {
var imageRect = (Rectangle)ImageRectangle.GetValue(this, null);
using (var bm = new Bitmap(imageRect.Width, imageRect.Height,
System.Drawing.Imaging.PixelFormat.Format32bppPArgb))
using(var g = Graphics.FromImage(bm)) {
int x = (int) (m.LParam.ToInt64() & 0xffff) - imageRect.X;
int y = (int) (m.LParam.ToInt64() >> 16) - imageRect.Y;
var rect = imageRect;
rect.Location = Point.Empty;
g.DrawImage(Image, rect);
if (!imageRect.Contains(x, y)) return;
else {
Color c = bm.GetPixel(x, y);
//I choose the threshold 30 for the alpha channel
//the ideal threshold is 0, it's up to you.
if (c.A < 30) return;
}
}
}
base.WndProc(ref m);
}
}
PictureBoxX pic = new PictureBoxX();
pic.Image = someTransparentImage;
pic.Click += (s,e) => {
MessageBox.Show("Clicked on image!");
};