Soyez le premier à donner votre avis sur cette source.
Snippet vu 9 488 fois - Téléchargée 26 fois
using System; using System.ComponentModel; using System.Collections.Generic; using System.Diagnostics; using System.Text; using System.Windows.Forms; using System.ComponentModel.Design; using System.Drawing; namespace EdgeGlue { /// <summary> /// Composant permettant le collage automatique d'une fenêtre sur les bords de l'écran. /// </summary> public partial class Patafix : Component { private int _minLeft = 10; private int _minRight = 10; private int _minTop = 10; private int _minBottom = 10; private Form _ParentForm; // Page sur laquelle a été déposée le contrôle /// <summary> /// Permet de spécifier la marge minimum pour la gauche /// </summary> public int MinLeft { set { _minLeft = value; } get { return _minLeft; } } /// <summary> /// Permet de spécifier la marge minimum pour la droite /// </summary> public int MinRight { set { _minRight = value; } get { return _minRight; } } /// <summary> /// Permet de spécifier la marge minimum pour le haut /// </summary> public int MinTop { set { _minTop = value; } get { return _minTop; } } /// <summary> /// Permet de spécifier la marge minimum pour le bas /// </summary> public int MinBottom { set { _minBottom = value; } get { return _minBottom; } } /// <summary> /// Permet de définir/récupérer le contrôle parent /// </summary> [Browsable(false)] public Form ParentForm { // Accesseur en lecture get { // Si l'on est en mode design, alors on demande au designer de définir la propriété ParentForm avec // le this de la form contenant l'instance du composant. if(this.Site.DesignMode) { IDesignerHost dh = (IDesignerHost)(this.GetService(typeof(IDesignerHost))); if(dh != null) { object obj = dh.RootComponent; if(obj != null) { _ParentForm = (Form)obj; } } } return _ParentForm; } // Accesseur en écriture set { if(value != null) { _ParentForm = value; this._ParentForm.Move += new EventHandler(_ParentForm_Move); } } } /// <summary> /// Constructeur /// </summary> public Patafix() {} /// <summary> /// Survient lorsque l'utilisateur déplace la fenêtre /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void _ParentForm_Move(object sender, EventArgs e) { if(_ParentForm.Location.X < _minLeft) _ParentForm.Location = new Point(0, _ParentForm.Location.Y); if(_ParentForm.Location.Y < _minTop) _ParentForm.Location = new Point(_ParentForm.Location.X, 0); if(Screen.GetWorkingArea(_ParentForm).Width - _ParentForm.Location.X - _ParentForm.Width < _minRight) _ParentForm.Location = new Point(Screen.GetWorkingArea(_ParentForm).Width - _ParentForm.Width, _ParentForm.Location.Y); if(Screen.GetWorkingArea(_ParentForm).Height - _ParentForm.Location.Y - _ParentForm.Height < _minBottom) _ParentForm.Location = new Point(_ParentForm.Location.X, Screen.GetWorkingArea(_ParentForm).Height - _ParentForm.Height); } } }
Heuuu, je me sens tout petit là mais j'ai une question toute bête...
Comment on s'en sert de ce koli code ???
Désolé pour cette horrible question, merci !
C'est vrai que ce problème de clignotement était assez génant, j'essaierai bientot ta nouvelle méthode !
private bool mouseIsDown = false;
private Point mouseLastPoint;
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
if (e.X >= 0 && e.X <= this.Width && e.Y >= 0 && e.Y <= 24 && e.Button == MouseButtons.Left)
{
mouseIsDown = true;
mouseLastPoint = new Point(e.X, e.Y);
}
}
protected override void OnMouseMove(MouseEventArgs e)
{
Point a = new Point();
if (mouseIsDown)
{
a = new Point(this.Left - (mouseLastPoint.X - e.X), this.Top - (mouseLastPoint.Y - e.Y));
if (a.X <= 10)
{
a = new Point(0, a.Y);
}
if (a.Y <= 10)
{
a = new Point(a.X, 0);
}
if (a.X >= (Screen.GetWorkingArea(this).Width - this.Width - 10))
{
a = new Point(Screen.GetWorkingArea(this).Width - this.Width, a.Y);
}
if (a.Y >= (Screen.GetWorkingArea(this).Height - this.Height - 10))
{
a = new Point(a.X, Screen.GetWorkingArea(this).Height - this.Height);
}
this.Location = a;
}
base.OnMouseMove(e);
}
protected override void OnMouseUp(MouseEventArgs e)
{
base.OnMouseUp(e);
mouseIsDown = false;
}
Et la pas de pb ca clignote plus! il faut modifier la position de la form d'abord en memoire et une fois le tout calculé tu dois le definir dans la form.
Tu fais toutes tes conditions et apres seulement du donnes la position a la fenetre!
Vous n'êtes pas encore membre ?
inscrivez-vous, c'est gratuit et ça prend moins d'une minute !
Les membres obtiennent plus de réponses que les utilisateurs anonymes.
Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.
Le fait d'être membre vous permet d'avoir des options supplémentaires.