当鼠标进入PictureBox时,我想给它的BackColor属性加上颜色。
当then MouseEnter事件触发时,我将BackColor设置为黄色,并在MouseLeave中将其重置为透明。
然后当我单击一个PictureBox时,我改变了它的位置,所以我还有一个Move事件,它将它重置为透明的。
问题是,一旦我移动了它,我需要用鼠标输入PictureBox两次才能触发MouseEnter事件!
这是一个非常图形化的问题,所以我上传了一个小video来告诉你发生了什么,它肯定会比我更好地解释我的问题。
我尝试了另一种方法,不是在MouseEnter中,而是在MouseHover中更改颜色。在这种情况下,它工作得很好,除了我在触发移动事件之前有500ms的延迟,这显然不是我想要的。
我现在没有可行的解决方案。
对于代码,它非常简单,目前我有:
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);编辑:为了回答我的问题,这是我现在使用的代码:(评论是法语的)
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;
}
}
}我使用和以前一样的方法,由于未知的原因,现在它可以工作了。感谢所有帮助我的人:)
发布于 2015-09-14 16:24:16
问题是,当您重新定位PictureBox时,它将不会收到鼠标离开事件。也许您已经注意到了,这就是为什么还要在Move事件中设置BackColor的原因。
尝试手动发送鼠标离开事件(我还没有测试过):
private const int WM_MOUSELEAVE = 0x02A3;
[DllImport("user32.dll")]
private static extern IntPtr SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);
private void pictureBoxMouseUp(object sender, MouseEventArgs e)
{
// move the PictureBox...
SendMessage(((PictureBox)sender).Handle, WM_MOUSELEAVE, IntPtr.Zero, IntPtr.Zero);
}发布于 2015-09-14 16:19:50
我将避免移动PictureBox。很明显,你的bug就来自于此。
当您移动零部件时,鼠标不再位于其中,但其状态不会更新。
你可以在the windows form reference code中游得很深,或者你可以只说你有N小预览(底线上的图片)和一个大预览(上面的)。
创建N+1图片框并且不更改它。只要更改他们的image property即可。
单击小预览时,将其图像属性与大预览切换。
此外,还推荐一个好的MVC pattern。
https://stackoverflow.com/questions/32559891
复制相似问题