一:效果,放到图片上会显示文字,单击触发一个事件
二:实现原理
将两张图片重合,点击是另一张图片,如下图,根据颜色来判断点击的是那个图片对象
三:代码
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO; namespace image { public partial class Form1 : Form { Bitmap showBm; //原始图片 Bitmap hideBm; //参考图片 Label lbInfo;//标签 public Form1() { InitializeComponent(); this.pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize; this.pictureBox1.BorderStyle = BorderStyle.None; this.pictureBox1.Location = new Point(0, 0); } #region 运行加载事件 private void Form1_Load(object sender, EventArgs e) { showBm = (Bitmap)global::image.Properties.Resources.ResourceManager.GetObject("show");//得到图片 hideBm = (Bitmap)global::image.Properties.Resources.ResourceManager.GetObject("hide"); pictureBox1.Image = showBm; //this.Width = pictureBox1.Width + (this.Bounds.Width - this.ClientSize.Width);//窗体的宽度 lbInfo = new Label(); this.Controls.Add(lbInfo);//在form中加一个lable lbInfo.Parent = pictureBox1;//在picturebox范围内才能显示这个label(需要指定控件显示位置,如果没有就不显示) lbInfo.BackColor = Color.FromArgb(150, 0, 0, 0);//背景色(透明度,0,0,0) lbInfo.ForeColor = Color.White;//前景色 lbInfo.TextAlign = ContentAlignment.MiddleCenter;//文字布局,中间显示 } #endregion #region 鼠标移动事件 private void pic_Dh_MouseMove(object sender, MouseEventArgs e) { try { if (e.X <= 1010 && e.Y <= 630)//图片大小 { Color clr = hideBm.GetPixel(e.X, e.Y); //获取在参考图对应坐标像素点的颜色信息 if (clr.ToArgb() != Color.Black.ToArgb())//比较argb颜色值设置文本显示 { this.Cursor = Cursors.Hand;//设置手形 if (clr.ToArgb() == Color.FromArgb(255, 254, 0, 0).ToArgb())//对颜色进行判断 lbInfo.Text = "春光油田"; else if (clr.ToArgb() == Color.FromArgb(255, 200, 160, 0).ToArgb()) lbInfo.Text = "本布图油田"; else if (clr.ToArgb() == Color.FromArgb(255, 0, 200, 50).ToArgb()) lbInfo.Text = "宝浪油田"; else if (clr.ToArgb() == Color.FromArgb(255, 220, 255, 151).ToArgb()) lbInfo.Text = "双河油田"; else if (clr.ToArgb() == Color.FromArgb(255, 255, 192, 63).ToArgb()) lbInfo.Text = "下二门油田"; else if (clr.ToArgb() == Color.FromArgb(255, 113, 250, 255).ToArgb()) lbInfo.Text = "魏岗油田"; else if (clr.ToArgb() == Color.FromArgb(255, 1, 139, 61).ToArgb()) lbInfo.Text = "赵凹油田"; else if (clr.ToArgb() == Color.FromArgb(255, 255, 83, 97).ToArgb()) lbInfo.Text = "张店油田"; else if (clr.ToArgb() == Color.FromArgb(255, 90, 220, 160).ToArgb()) lbInfo.Text = "北马庄油田"; else if (clr.ToArgb() == Color.FromArgb(255, 254, 0, 246).ToArgb()) lbInfo.Text = "东庄油田"; else if (clr.ToArgb() == Color.FromArgb(255, 118, 146, 255).ToArgb()) lbInfo.Text = "井楼油田"; else if (clr.ToArgb() == Color.FromArgb(255, 20, 10, 109).ToArgb()) lbInfo.Text = "古城油田"; else if (clr.ToArgb() == Color.FromArgb(255, 134, 178, 29).ToArgb()) lbInfo.Text = "新庄油田"; else if (clr.ToArgb() == Color.FromArgb(255, 1, 155, 163).ToArgb()) lbInfo.Text = "王集油田"; else if (clr.ToArgb() == Color.FromArgb(255, 0, 140, 101).ToArgb()) lbInfo.Text = "杨楼油田"; else if (clr.ToArgb() == Color.FromArgb(255, 128, 119, 0).ToArgb()) lbInfo.Text = "付湾油田"; else if (clr.ToArgb() == Color.FromArgb(255, 154, 0, 182).ToArgb()) lbInfo.Text = "杜坡油田"; else lbInfo.Text = "有待开发"; } else { pictureBox1.Cursor = Cursors.Arrow; lbInfo.Text = "有待开发"; } //设置label位置 if (e.X + 20 + lbInfo.Width >= pictureBox1.Width) lbInfo.Left = e.X - 20 - lbInfo.Width; else lbInfo.Left = e.X + 20; if (e.Y + 20 + lbInfo.Height >= pictureBox1.Height) lbInfo.Top = e.Y - 20 - lbInfo.Height; else lbInfo.Top = e.Y + 20; } } catch { MessageBox.Show(e.X.ToString() + "," + e.Y.ToString()); } } #endregion #region 鼠标单击事件 private void pic_Dh_Click(object sender, EventArgs e) { switch (lbInfo.Text) { case "春光油田": //ConstInfo.Yt_Name = lbInfo.Text; //数据成图ToolStripMenuItem_Click(sender, e);//可触发一个菜单栏事件 MessageBox.Show(""); break; case "本布图油田": MessageBox.Show(""); break; case "宝浪油田": MessageBox.Show(""); break; case "双河油田": MessageBox.Show(""); break; case "下二门油田": MessageBox.Show(""); break; case "魏岗油田": MessageBox.Show(""); break; case "赵奥油田": MessageBox.Show(""); break; case "张店油田": MessageBox.Show(""); break; case "北马庄油田": MessageBox.Show(""); break; case "东庄油田": MessageBox.Show(""); break; case "井楼油田": MessageBox.Show(""); break; case "古城油田": MessageBox.Show(""); break; case "新庄油田": MessageBox.Show(""); break; case "王集油田": MessageBox.Show(""); break; case "杨楼油田": MessageBox.Show(""); break; case "付湾油田": MessageBox.Show(""); break; case "杜坡油田": MessageBox.Show(""); break; case "有待开发": //ConstInfo.Yt_Name = lbInfo.Text; //成果管理ToolStripMenuItem_Click(sender, e); break; } } #endregion } }
时间: 2024-10-27 07:24:58