1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
| using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace Lanceur_GPS
{
public partial class Form1 : Form
{
//ma fenetre prend la taille du bord de l'écran ou elle est dockée
//par contre, selon qu'elle soit verticale ou horizontale,
//à cause de la hauteur de la barre de déplacement il y a une variation
int _hauteurAppli = 74;
int _largeurAppli = 51;
private enum Bord
{
Gauche,
Droit,
Haut,
Bas
}
public Form1()
{
InitializeComponent();
//au lancement du programme je la dock en haut !
DockHaut();
}
//cette fonction sert simplement à déterminer si la souris est plus proche d'un des quatre bords, et renvoie ce bord
private Bord Detecte_Bord(Screen screen)
{
Point souris = GetCursorPosition();
float pourcentGauche = 1f - (float)souris.X / (float)screen.WorkingArea.Width;
float pourcentDroit = (float)souris.X / (float)screen.WorkingArea.Width;
float pourcentHaut = 1f - (float)souris.Y / (float)screen.WorkingArea.Height;
float pourcentBas = (float)souris.Y / (float)screen.WorkingArea.Height;
if (pourcentGauche > pourcentDroit)
{
if (pourcentHaut > pourcentBas)
{
if (pourcentHaut > pourcentGauche)
return Bord.Haut;
else
return Bord.Gauche;
}
else
{
if (pourcentBas > pourcentGauche)
return Bord.Bas;
else
return Bord.Gauche;
}
}
else
{
if (pourcentHaut > pourcentBas)
{
if (pourcentHaut > pourcentDroit)
return Bord.Haut;
else
return Bord.Droit;
}
else
{
if (pourcentBas > pourcentDroit)
return Bord.Bas;
else
return Bord.Droit;
}
}
}
private void DockForm()
{
//selon le bord renvoyé, je traite...
switch (Detecte_Bord(Screen.PrimaryScreen))
{
case Bord.Bas:
DockBas();
break;
case Bord.Droit:
DockDroite();
break;
case Bord.Haut:
DockHaut();
break;
case Bord.Gauche:
DockGauche();
break;
}
}
private void DockHaut()
{
this.Size = new Size(Screen.PrimaryScreen.WorkingArea.Width, _hauteurAppli);
this.Location = new Point(0, 0);
}
private void DockBas()
{
this.Size = new Size(Screen.PrimaryScreen.WorkingArea.Width, _hauteurAppli);
this.Location = new Point(0, Screen.PrimaryScreen.WorkingArea.Height - this.Height);
}
private void DockGauche()
{
this.Size = new Size(_largeurAppli, Screen.PrimaryScreen.WorkingArea.Height);
this.Location = new Point(0, 0);
}
private void DockDroite()
{
this.Size = new Size(_largeurAppli, Screen.PrimaryScreen.WorkingArea.Height);
this.Location = new Point(Screen.PrimaryScreen.WorkingArea.Width - this.Width, 0);
}
//traitement des messages Windows
protected override void WndProc(ref Message m)
{
//quel message recoit la fenetre ?
switch (m.Msg)
{
//c'est un "WM_SIZING" (je suis en train de resizer...)
//Doc : http://msdn.microsoft.com/en-us/library/windows/desktop/ms632646%28v=vs.85%29.aspx
case 5:
//c'est un "SIZE_MAXIMIZED" (le resize est un Maximize)
if (m.WParam.ToInt32() == 2)
{
//je le remet en normal pour empecher le maximize
this.WindowState = FormWindowState.Normal;
}
break;
//c'est un "WM_EXITSIZEMOVE" (je déclique pendant un move)
//Doc : http://msdn.microsoft.com/en-us/library/windows/desktop/ms632623%28v=vs.85%29.aspx
case 562:
DockForm();
break;
}
base.WndProc(ref m);
}
[StructLayout(LayoutKind.Sequential)]
public struct POINT
{
public int X;
public int Y;
public static implicit operator Point(POINT point)
{
return new Point(point.X, point.Y);
}
}
/// <summary>
/// retrouve la position du curseur en coordonnées ecran.
/// </summary>
[DllImport("user32.dll")]
public static extern bool GetCursorPos(out POINT lpPoint);
public static Point GetCursorPosition()
{
POINT lpPoint;
GetCursorPos(out lpPoint);
return lpPoint;
}
}
} |
Partager