Accueil
Rechercher:
sur developpez.com sur les forums
Forums | Tutoriels | F.A.Q's | Participez | Hébergement | Contacts
Club Emploi Blogs   TV   Dév. Web PHP XML Python Autres 2D-3D-Jeux Sécurité Windows Linux PC Mac
Accueil Conception Java DotNET Visual Basic  C  C++ Delphi MS-Office SQL & SGBD Oracle  4D  Business Intelligence
FORUMS DELPHI F.A.Q DELPHI TUTORIELS DELPHI LIVRES COMPOSANTS SOURCES DEFI TELECHARGEZ DELPHI TV
Nono40.developpez.com
Le petit coin du web de Nono40
SOURCES ARTICLES NONOVISU ACCUEIL NOUVELLES
Retour à l'accueil
78 - CLASSE NOTIFYICON AVEC WINFORM

PRÉSENTATION : Utilisation de la classe NotifyIcon dans une application WinForm.
ZIP : Téléchargez le zip : miroir 1 , miroir 2 APERÇUS :

NOTES : Voir ici pour une description complète de cette méthode : Utilisation de la classe NotifyIcon

CODE :
Unit WinForm;

Interface

Uses
  System.Drawing, System.Collections, System.ComponentModel,
  System.Windows.Forms, System.Data, System.Resources;

Type
  TWinForm = Class(System.Windows.Forms.Form)
  {$REGION 'Designer Managed Code'}
  strict Private
    /// <summary>
    /// Required designer variable.
    /// </summary>
    components: System.ComponentModel.IContainer;
    NotifyIcon1: System.Windows.Forms.NotifyIcon;
    ContextMenu1: System.Windows.Forms.ContextMenu;
    MenuRestaurer: System.Windows.Forms.MenuItem;
    MenuFermer: System.Windows.Forms.MenuItem;
    Timer1: System.Windows.Forms.Timer;
    MenuReduire: System.Windows.Forms.MenuItem;
    Label1: System.Windows.Forms.Label;
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    Procedure InitializeComponent;
    Procedure TWinForm_Closing(sender: System.Object; e: System.ComponentModel.CancelEventArgs);
    Procedure MenuFermer_Click(sender: System.Object; e: System.EventArgs);
    Procedure MenuRestaurer_Click(sender: System.Object; e: System.EventArgs);
    Procedure Timer1_Tick(sender: System.Object; e: System.EventArgs);
    Procedure NotifyIcon1_DoubleClick(sender: System.Object; e: System.EventArgs);
    Procedure NotifyIcon1_MouseDown(sender: System.Object; e: System.Windows.Forms.MouseEventArgs);
    Procedure MenuReduire_Click(sender: System.Object; e: System.EventArgs);
  {$ENDREGION}
  strict Protected
    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    Procedure Dispose(Disposing: Boolean); override;
  Private
    { Private Declarations }
    DoitFermer:Boolean;
  Public
    Constructor Create;
  End;

  [assembly: RuntimeRequiredAttribute(TypeOf(TWinForm))]

Implementation

{$REGION 'Windows Form Designer generated code'}
/// <summary>
/// Required method for Designer support -- do not modify
/// the contents of this method with the code editor.
/// </summary>
Procedure TWinForm.InitializeComponent;
Type
  TSystem_Windows_Forms_MenuItemArray = Array Of System.Windows.Forms.MenuItem;
Var
  resources: System.Resources.ResourceManager;
Begin
  Self.components := System.ComponentModel.Container.Create;
  resources := System.Resources.ResourceManager.Create(TypeOf(TWinForm));
  Self.NotifyIcon1 := System.Windows.Forms.NotifyIcon.Create(Self.components);
  Self.ContextMenu1 := System.Windows.Forms.ContextMenu.Create;
  Self.MenuRestaurer := System.Windows.Forms.MenuItem.Create;
  Self.MenuReduire := System.Windows.Forms.MenuItem.Create;
  Self.MenuFermer := System.Windows.Forms.MenuItem.Create;
  Self.Timer1 := System.Windows.Forms.Timer.Create(Self.components);
  Self.Label1 := System.Windows.Forms.Label.Create;
  Self.SuspendLayout;
  // 
  // NotifyIcon1
  // 
  Self.NotifyIcon1.ContextMenu := Self.ContextMenu1;
  Self.NotifyIcon1.Icon := (System.Drawing.Icon(resources.GetObject('NotifyIcon1.Icon')));
  Self.NotifyIcon1.Text := 'Nono40 - source 78';
  Self.NotifyIcon1.Visible := True;
  Include(Self.NotifyIcon1.MouseDown, Self.NotifyIcon1_MouseDown);
  Include(Self.NotifyIcon1.DoubleClick, Self.NotifyIcon1_DoubleClick);
  // 
  // ContextMenu1
  // 
  Self.ContextMenu1.MenuItems.AddRange(TSystem_Windows_Forms_MenuItemArray.Create(Self.MenuRestaurer, 
          Self.MenuReduire, Self.MenuFermer));
  // 
  // MenuRestaurer
  // 
  Self.MenuRestaurer.DefaultItem := True;
  Self.MenuRestaurer.Index := 0;
  Self.MenuRestaurer.Text := 'Restaurer';
  Include(Self.MenuRestaurer.Click, Self.MenuRestaurer_Click);
  // 
  // MenuReduire
  // 
  Self.MenuReduire.Index := 1;
  Self.MenuReduire.Text := 'Réduire';
  Include(Self.MenuReduire.Click, Self.MenuReduire_Click);
  // 
  // MenuFermer
  // 
  Self.MenuFermer.Index := 2;
  Self.MenuFermer.Text := 'Fermer';
  Include(Self.MenuFermer.Click, Self.MenuFermer_Click);
  // 
  // Timer1
  // 
  Self.Timer1.Interval := 400;
  Include(Self.Timer1.Tick, Self.Timer1_Tick);
  // 
  // Label1
  // 
  Self.Label1.Location := System.Drawing.Point.Create(88);
  Self.Label1.Name := 'Label1';
  Self.Label1.Size := System.Drawing.Size.Create(24032);
  Self.Label1.TabIndex := 0;
  Self.Label1.Text := 'Pour fermer cette fenêtre, utiliser l''icône dans la barre des tâches.';
  // 
  // TWinForm
  // 
  Self.AutoScaleBaseSize := System.Drawing.Size.Create(513);
  Self.ClientSize := System.Drawing.Size.Create(33856);
  Self.Controls.Add(Self.Label1);
  Self.FormBorderStyle := System.Windows.Forms.FormBorderStyle.FixedSingle;
  Self.Icon := (System.Drawing.Icon(resources.GetObject('$this.Icon')));
  Self.MaximizeBox := False;
  Self.MinimizeBox := False;
  Self.Name := 'TWinForm';
  Self.ShowInTaskbar := False;
  Self.Text := 'Classe NotifyIcon - WinForm - par Nono40';
  Include(Self.Closing, Self.TWinForm_Closing);
  Self.ResumeLayout(False);
End;
{$ENDREGION}

Procedure TWinForm.Dispose(Disposing: Boolean);
Begin
  If Disposing Then
  Begin
    If Components <> Nil Then
      Components.Dispose();
  End;
  Inherited Dispose(Disposing);
End;

Constructor TWinForm.Create;
Begin
  Inherited Create;
  //
  // Required for Windows Form Designer support
  //
  InitializeComponent;
  //
  // TODO: Add any constructor code after InitializeComponent call
  //
  DoitFermer:=False;
End;

Procedure TWinForm.MenuReduire_Click(sender: System.Object; e: System.EventArgs);
Begin
  // L'option réduire permet de ... réduire la fenêtre !
  WindowState:=FormWindowState.Minimized;
End;

Procedure TWinForm.MenuRestaurer_Click(sender: System.Object; e: System.EventArgs);
Begin
  // L'option restaurer permet de ... restaurer la fenêtre !
  If WindowState=FormWindowState.Minimized
    Then WindowState:=FormWindowState.Normal;
  Show;
End;

Procedure TWinForm.MenuFermer_Click(sender: System.Object; e: System.EventArgs);
Begin
  // Il faut autoriser la feremeture cette fois-ci
  DoitFermer:=True;
  // et fermer la fenêtre
  Close;
End;

Procedure TWinForm.TWinForm_Closing(sender: System.Object; e: System.ComponentModel.CancelEventArgs);
Begin
  // On teste si la fermeture de la fenêtre doit être effective
  If Not DoitFermer Then
  Begin
    // Dans le cas contraire, on se contente de réduire l'application
    e.Cancel := True;
    WindowState := FormWindowState.Minimized;
  End;
End;

Procedure TWinForm.NotifyIcon1_MouseDown(sender: System.Object; e: System.Windows.Forms.MouseEventArgs);
Begin
  // Dans le cas d'un click gauche sur l'icone, on test si c'est le premier
  // click puis on lance le timer de retard d'affichage du menu.
  // Cette méthode permet de laisser le temps de double cliquer sur l'icone
  // avant que le menu n'apparaisse.
  If (e.Button=System.Windows.Forms.MouseButtons.Left) And
     (e.Clicks=1)
  Then
    Timer1.Enabled:=True;
End;

Procedure TWinForm.NotifyIcon1_DoubleClick(sender: System.Object; e: System.EventArgs);
Begin
  // Dans le cas d'un double-click, on arrête le timer lancé par le premier
  // click et on effectue l'action par défaut.
  Timer1.Enabled:=False;
  MenuRestaurer_Click(Sender,e);
End;

Procedure TWinForm.Timer1_Tick(sender: System.Object; e: System.EventArgs);
Begin
  // Si le timer s'est écoulé, c'est qu'il n'y a pas eu de double-click.
  // Dans ce cas on affiche le menu là ou est la souris.
  Timer1.Enabled:=False;
  ContextMenu1.Show(Self,Control.MousePosition);
End;

end.

Les sources présentées sur cette page sont libres de droits, et vous pouvez les utiliser à votre convenance. Par contre, la page de présentation constitue une oeuvre intellectuelle protégée par les droits d'auteurs. Copyright © 2003 Bruno Guérangé. Aucune reproduction, même partielle, ne peut être faite de ce site et de l'ensemble de son contenu : textes, documents, images, etc sans l'autorisation expresse de l'auteur. Sinon vous encourez selon la loi jusqu'à 3 ans de prison et jusqu'à 300 000 E de dommages et intérêts. Cette page est déposée à la SACD.

Responsables bénévoles de la rubrique Delphi : Bruno Guérangé et Pierre Rodriguez - Contacter par EMail :
Vos questions techniques : forum d'entraide Delphi - Publiez vos articles, tutoriels et cours
et rejoignez-nous dans l'équipe de rédaction du club d'entraide des développeurs francophones
Nous contacter - Copyright © 2000-2008 www.developpez.com - Legal informations.