【发布时间】:2015-09-14 08:02:07
【问题描述】:
我想在鼠标进入时为PictureBox 的BackColor 属性着色。
当MouseEnter 事件触发时,我将BackColor 变为黄色,并在MouseLeave 中重置为透明。
然后当我点击PictureBox 时,我改变了它的位置,所以我还有一个Move 事件,它可以透明地重置它。
问题是,一旦我移动它,我需要用鼠标输入两次PictureBox 来触发 MouseEnter 事件!
这是一个非常图形化的问题,所以我上传了一点video 来告诉你发生了什么,它肯定会比我更好地解释我的问题。
我尝试了另一种方法,不是在MouseEnter 中更改颜色,而是在MouseHover 中更改颜色。在这种情况下,它运行良好,只是我在触发 Move 事件之前有 500 毫秒的延迟,这显然不是我想要的。
我目前没有可行的解决方案。
代码很简单,目前我有:
private void pictureBoxMouseUp(object sender, MouseEventArgs e)
{
// I move the PictureBox here
}
private void pictureBoxMove(object sender, EventArgs e)
{
(sender as PictureBox).BackColor = Color.Transparent;
}
private void pictureBoxMouseEnter(object sender, MouseEventArgs e)
{
(sender as PictureBox).BackColor = Color.LightYellow;
}
private void pictureBoxMouseLeave(object sender, MouseEventArgs e)
{
(sender as PictureBox).BackColor = Color.Transparent;
}
在 Designer.cs 中,我对每个 PictureBox 的事件如下:
this.pictureBox2.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pictureBoxMouseDown);
this.pictureBox2.MouseEnter += new System.EventHandler(this.pictureBoxMouseEnter);
this.pictureBox2.MouseLeave += new System.EventHandler(this.pictureBoxMouseLeave);
this.pictureBox2.MouseUp += new System.Windows.Forms.MouseEventHandler(this.pictureBoxMouseUp);
this.pictureBox2.Move += new System.EventHandler(this.pictureBoxMove);
编辑:回答我的问题,这是我现在使用的代码:(cmets 是法语)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Diagnostics;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using EmoTEDTherapeute;
namespace ControlSceneImage {
public class SceneImage : PictureBox {
public static readonly int defaultWidth = 100;
public static readonly int defaultHeight = 100;
static readonly int activePbPosY; // Position en Y des scènes actives
static readonly Dictionary<string, Point> scenesPos = null; // Invariant, stocke la position initiale des PictureBox
static readonly List<Point>[] activeScenesPos = null; // Invariant, stocke les positions des PictureBox en fonction du nombre de scènes actives
static List<SceneImage> activeScenes = null;
const int maxActiveScenes = 5;
const int ecart = 80;
const int decalage = 15;
const int panelScenesWidth = 909;
const int panelScenesHeight = 154;
const int panelScenesLocationX = 35;
const int panelScenesLocationY = 36;
bool isActive;
static SceneImage() {
// Constructeur initialisant tous les membres statiques, n'est appelé qu'une seule fois, avant tout le reste
activePbPosY = (panelScenesLocationY + panelScenesHeight - (int)(0.6 * defaultHeight)) / 2;
scenesPos = new Dictionary<string, Point>();
activeScenesPos = new List<Point>[maxActiveScenes];
for (int i = 0; i < maxActiveScenes; i++) {
activeScenesPos[i] = CalcActiveScenesPos(i+1);
}
activeScenes = new List<SceneImage>();
}
public SceneImage() {
MouseEnter += new EventHandler(OnMouseEnter);
MouseDown += new MouseEventHandler(OnMouseDown);
MouseUp += new MouseEventHandler(OnMouseUp);
MouseLeave += new EventHandler(OnMouseLeave);
BorderStyle = BorderStyle.FixedSingle;
Size = new Size(defaultWidth, defaultHeight);
SizeMode = PictureBoxSizeMode.Zoom;
isActive = false;
}
private static List<Point> CalcActiveScenesPos(int nbActiveScenes) {
List<Point> ret = new List<Point>();
for (int i = 0; i < nbActiveScenes; i++) {
ret.Add(new Point((panelScenesLocationX + panelScenesWidth + ecart) / 2 + (int)((ecart + defaultWidth) * (i - nbActiveScenes / 2.0)) + decalage, activePbPosY));
}
return ret;
}
private void UpdateScenesPos() {
for(int i = 0; i < activeScenes.Count; i++) {
activeScenes[i].Location = activeScenesPos[activeScenes.Count - 1][i];
}
}
private void OnMouseEnter(object sender, EventArgs e) {
BackColor = Color.LightYellow;
Cursor = Cursors.Hand;
}
private void OnMouseDown(object sender, MouseEventArgs e) {
BorderStyle = BorderStyle.Fixed3D;
}
private void OnMouseUp(object sender, MouseEventArgs e) {
if (!scenesPos.ContainsKey(Name)) {
// Si ce n'est pas déjà fait, on stocke la position initiale de notre PictureBox
scenesPos.Add(Name, Location);
// Et on crée un Panel vide sous elle
Panel panel = new Panel();
panel.Location = Location;
panel.Size = Size;
panel.BorderStyle = BorderStyle.Fixed3D;
// On ajoute notre panel à notre form
Form1.AddInSeance(panel);
}
if (!isActive) {
activeScenes.Add(this);
} else {
Location = scenesPos[Name];
activeScenes.Remove(this);
}
isActive = !isActive;
UpdateScenesPos();
}
private void OnMouseLeave(object sender, EventArgs e) {
BorderStyle = BorderStyle.FixedSingle;
BackColor = Color.Transparent;
}
}
}
我使用与以前相同的方法,但由于未知原因,现在它可以工作了。 感谢大家帮助我:)
【问题讨论】:
-
我在我的信息顶部写了一个“大家好”,但由于某些奇怪的原因,它似乎被剪掉了,即使我编辑了它,所以不要粗鲁:)跨度>
-
您能否提供更多代码来显示您分配给处理程序的事件?
-
可能跟一些重点部分有关。
-
@Falco Alexander 我编辑了我的第一篇文章,但我不知道我是否理解正确!
-
@user3623965 “你好”等是frowned upon,因为它们把事情搞得一团糟;许多称呼是automatically removed。别担心,没有人认为你不礼貌:-)
标签: c# windows forms winforms mouseenter