OVH Cloud OVH Cloud

OnMouseEnter sur tout un control ??

1 réponse
Avatar
amplitude
Bonjour,


Voil=E0, j'avance bien ds mon control, mais j'ai un prob :

J'ai overrid=E9 les control de la souris, dont le OnMouseEnter() qui=20
change la couleur au passage de la souris.
Le prob est que sur mon panel, j'ai 2 labels entre autre, et quand le=20
curseur passe sur un label, il croit que le panel n'a plus la souris et=20
il effectue le OnMouseLeave()

Comme mon code n'est pas trop trop long, je me permet de vous le passer..=
=2E

using System;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;

namespace AMPPanel
{
/// <summary>
/// Summary description for Class1.
/// </summary>
public class AMPPanel : Panel
{
AMPPanelLabelTitre ampPanelLabelTitre =3D new AMPPanelLabelTitre();
AMPPanelLabelNbObj ampPanelLabelNbObj =3D new AMPPanelLabelNbObj();
private System.Windows.Forms.PictureBox pictureBox =3D new=20
System.Windows.Forms.PictureBox();
private int NbObj =3D 0;
=09
[Category("Dossier"),
Description("(string) Affiche ou attribu le titre du dossier"),
DefaultValue("default")]
public string LeTitre
{
get
{
return ampPanelLabelTitre.Text;
}
set
{
SetTitre(value);
}
}

[Category("Dossier"),
Description("(int) Affiche ou attribu le nombre d'objet"),
DefaultValue(0)]
public int NombreObjet
{
get
{
return NbObj;
}
set
{
NbObj =3D value;
SetNbObj(NbObj);
}
}
=09
public AMPPanel() : base()
{
Name =3D "AMPPanel";
BackColor =3D System.Drawing.Color.WhiteSmoke;
BorderStyle =3D System.Windows.Forms.BorderStyle.Fixed3D;
Size =3D new System.Drawing.Size(224, 64);
Cursor =3D System.Windows.Forms.Cursors.Hand;
//
//// Picture
//
System.Resources.ResourceManager resources =3D new=20
System.Resources.ResourceManager(typeof(AMPPanel));
pictureBox.Location =3D new System.Drawing.Point(48, 48);
pictureBox.Name =3D "pictureBox1";
pictureBox.Location =3D new System.Drawing.Point(8, 8);
pictureBox.Image =3D=20
((System.Drawing.Image)(resources.GetObject("pictureBox.Image")));
//--------------------------------------------------------------------=
-
Controls.Add(ampPanelLabelTitre);
Controls.Add(ampPanelLabelNbObj);
Controls.Add(pictureBox);
}

protected class AMPPanelLabelTitre : Label
{
public AMPPanelLabelTitre()
{
Location =3D new System.Drawing.Point(72, 8);
Anchor =3D=20
((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.=
Top=20

| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
Cursor =3D System.Windows.Forms.Cursors.Hand;
Font =3D new System.Drawing.Font("Trebuchet MS", 9.75F,=20
System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point,=20
((System.Byte)(0)));
Size =3D new System.Drawing.Size(144, 16); =09
}
}

protected class AMPPanelLabelNbObj : Label
{
public AMPPanelLabelNbObj()
{
Location =3D new System.Drawing.Point(72, 24);
Anchor =3D=20
((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.=
Top=20

| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
Cursor =3D System.Windows.Forms.Cursors.Hand;
Font =3D new System.Drawing.Font("Trebuchet MS", 8.25F,=20
System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,=20
((System.Byte)(0)));
Size =3D new System.Drawing.Size(144, 16); =09
}
}

protected void SetTitre(string Titre)
{
ampPanelLabelTitre.Text =3D Titre;
}

protected void SetNbObj(int NbObjet)
{
string Tmp =3D NbObjet+" objet";
if(NbObjet>1)
{
Tmp +=3D "s";
}
ampPanelLabelNbObj.Text =3D Tmp;
}
=09
protected override void OnCreateControl()
{
base.OnCreateControl();
SetTitre("Label Titre avec image");
SetNbObj(NbObj);
}
=09
protected override void OnResize(EventArgs eventargs)
{
// TODO: Add AMPPanel.OnResize implementation
if(base.Height > 64)
{
base.Height =3D 64;
}
base.OnResize (eventargs);
}
=09
protected override void OnMouseEnter(EventArgs e)
{
// TODO: Add AMPPanel.OnMouseEnter implementation
base.BackColor =3D System.Drawing.Color.LightSteelBlue;
base.OnMouseEnter (e);
}
=09
protected override void OnMouseLeave(EventArgs e)
{
// TODO: Add AMPPanel.OnMouseLeave implementation
base.BackColor =3D System.Drawing.Color.WhiteSmoke;
base.OnMouseLeave (e);
}
=09
protected override void OnMouseDown(MouseEventArgs e)
{
// TODO: Add AMPPanel.OnMouseDown implementation
base.BorderStyle =3D System.Windows.Forms.BorderStyle.None;
base.BackColor =3D System.Drawing.Color.AliceBlue;
base.OnMouseDown (e);
}
=09
protected override void OnMouseUp(MouseEventArgs e)
{
// TODO: Add AMPPanel.OnMouseUp implementation
base.BorderStyle =3D System.Windows.Forms.BorderStyle.Fixed3D;
base.BackColor =3D System.Drawing.Color.LightSteelBlue;
base.OnMouseUp (e);
}
}
}

1 réponse

Avatar
Julien Bakmezdjian [MS]
Bonjour,

Je pense qu'il va vous falloir jouer sur les événement MouseEnter et
MouseLeave des contrôles enfants du panel. Avec un petit flag (et quelques
tests pour savoir dans quel ordre ont lieu les événements), vous devriez
pouvoir vous en sortir.

Autre option : lors d'un mouseLeave du panel, récupérer la position du
pointeur de la souris (avec System.Windows.Forms.Cursor.Position) et la
comparer au rectangle des contrôles enfants.

Cordialement,

Julien

"amplitude" a écrit dans le message de
news:cft6de$kng$
Bonjour,


Voilà, j'avance bien ds mon control, mais j'ai un prob :

J'ai overridé les control de la souris, dont le OnMouseEnter() qui
change la couleur au passage de la souris.
Le prob est que sur mon panel, j'ai 2 labels entre autre, et quand le
curseur passe sur un label, il croit que le panel n'a plus la souris et
il effectue le OnMouseLeave()

Comme mon code n'est pas trop trop long, je me permet de vous le passer...

using System;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;

namespace AMPPanel
{
/// <summary>
/// Summary description for Class1.
/// </summary>
public class AMPPanel : Panel
{
AMPPanelLabelTitre ampPanelLabelTitre = new AMPPanelLabelTitre();
AMPPanelLabelNbObj ampPanelLabelNbObj = new AMPPanelLabelNbObj();
private System.Windows.Forms.PictureBox pictureBox = new
System.Windows.Forms.PictureBox();
private int NbObj = 0;

[Category("Dossier"),
Description("(string) Affiche ou attribu le titre du dossier"),
DefaultValue("default")]
public string LeTitre
{
get
{
return ampPanelLabelTitre.Text;
}
set
{
SetTitre(value);
}
}

[Category("Dossier"),
Description("(int) Affiche ou attribu le nombre d'objet"),
DefaultValue(0)]
public int NombreObjet
{
get
{
return NbObj;
}
set
{
NbObj = value;
SetNbObj(NbObj);
}
}

public AMPPanel() : base()
{
Name = "AMPPanel";
BackColor = System.Drawing.Color.WhiteSmoke;
BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
Size = new System.Drawing.Size(224, 64);
Cursor = System.Windows.Forms.Cursors.Hand;
//
//// Picture
//
System.Resources.ResourceManager resources = new
System.Resources.ResourceManager(typeof(AMPPanel));
pictureBox.Location = new System.Drawing.Point(48, 48);
pictureBox.Name = "pictureBox1";
pictureBox.Location = new System.Drawing.Point(8, 8);
pictureBox.Image ((System.Drawing.Image)(resources.GetObject("pictureBox.Image")));
//---------------------------------------------------------------------
Controls.Add(ampPanelLabelTitre);
Controls.Add(ampPanelLabelNbObj);
Controls.Add(pictureBox);
}

protected class AMPPanelLabelTitre : Label
{
public AMPPanelLabelTitre()
{
Location = new System.Drawing.Point(72, 8);
Anchor ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top

| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
Cursor = System.Windows.Forms.Cursors.Hand;
Font = new System.Drawing.Font("Trebuchet MS", 9.75F,
System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point,
((System.Byte)(0)));
Size = new System.Drawing.Size(144, 16);
}
}

protected class AMPPanelLabelNbObj : Label
{
public AMPPanelLabelNbObj()
{
Location = new System.Drawing.Point(72, 24);
Anchor ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top

| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
Cursor = System.Windows.Forms.Cursors.Hand;
Font = new System.Drawing.Font("Trebuchet MS", 8.25F,
System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,
((System.Byte)(0)));
Size = new System.Drawing.Size(144, 16);
}
}

protected void SetTitre(string Titre)
{
ampPanelLabelTitre.Text = Titre;
}

protected void SetNbObj(int NbObjet)
{
string Tmp = NbObjet+" objet";
if(NbObjet>1)
{
Tmp += "s";
}
ampPanelLabelNbObj.Text = Tmp;
}

protected override void OnCreateControl()
{
base.OnCreateControl();
SetTitre("Label Titre avec image");
SetNbObj(NbObj);
}

protected override void OnResize(EventArgs eventargs)
{
// TODO: Add AMPPanel.OnResize implementation
if(base.Height > 64)
{
base.Height = 64;
}
base.OnResize (eventargs);
}

protected override void OnMouseEnter(EventArgs e)
{
// TODO: Add AMPPanel.OnMouseEnter implementation
base.BackColor = System.Drawing.Color.LightSteelBlue;
base.OnMouseEnter (e);
}

protected override void OnMouseLeave(EventArgs e)
{
// TODO: Add AMPPanel.OnMouseLeave implementation
base.BackColor = System.Drawing.Color.WhiteSmoke;
base.OnMouseLeave (e);
}

protected override void OnMouseDown(MouseEventArgs e)
{
// TODO: Add AMPPanel.OnMouseDown implementation
base.BorderStyle = System.Windows.Forms.BorderStyle.None;
base.BackColor = System.Drawing.Color.AliceBlue;
base.OnMouseDown (e);
}

protected override void OnMouseUp(MouseEventArgs e)
{
// TODO: Add AMPPanel.OnMouseUp implementation
base.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
base.BackColor = System.Drawing.Color.LightSteelBlue;
base.OnMouseUp (e);
}
}
}