Réponse acceptée !
Salut emmanuel,
Je pense avoir la solution :
// Timer permettant de faire clignoter le TabPage
private Timer _tabPageClippingTimer;
// Variable indiquant si le TabPage en clignotement est allumé
private bool _isClipping;
// Indice du TabPage en train de clignoté
private int _tabPageIndex;
// Au chargement du formulaire
private void FormLoad( object sender, System.EventArgs e )
{
// Initialise le timer
_tabPageClippingTimer = new Timer();
_tabPageClippingTimer.Interval = 250;
_tabPageClippingTimer.Tick += new EventHandler( TabPageClipping );
// Intercepte l'événement généré lorsqu'un TabPage doit être dessiné
tabControl.DrawMode = TabDrawMode.OwnerDrawFixed;
tabControl.DrawItem += new DrawItemEventHandler( DrawTabControlItem );
}
// Débutte un clignottement
// tabPageIndex -> indice du TabPage à faire clignoter
private void StartTabPageClipping( int tabPageIndex )
{
_tabPageIndex = tabPageIndex;
_tabPageClippingTimer.Start();
}
// Met fin au clignottement
private void StopTabPageClipping()
{
_isClipping = false;
_tabPageClippingTimer.Stop();
tabControl.Invalidate( true );
}
// Le tabPage doit clignoter
private void TabPageClipping( object sender, EventArgs e )
{
// Le TabPage est allumé
if( _isClipping )
{
// Eteind le TabPage
_isClipping = false;
tabControl.Invalidate( true );
}
else
{
// Allume le TabPage
_isClipping = true;
tabControl.Invalidate( true );
}
}
// Un TabPage doit être redessiné
private void DrawTabControlItem( object sender, System.Windows.Forms.DrawItemEventArgs e )
{
// Pinceau du fond du texte
Brush backBrush;
// Pinceau du texte
Brush foreBrush;
// Il s'agit du TabPage à faire clignoté et le TabPage est allumé
if( _isClipping && _tabPageIndex == e.Index )
{
backBrush = new Pen( Color.Red ).Brush;
foreBrush = new Pen( Color.Yellow ).Brush;
}
else
{
backBrush = new Pen( SystemColors.Control ).Brush;
foreBrush = new Pen( Color.Black ).Brush;
}
// Dessine le fond du TabPage
e.Graphics.FillRectangle( backBrush, e.Bounds );
// Récupère le texte du TabPage à dessiner
string tabPageText = tabControl.TabPages[ e.Index ].Text;
// Récupère les dimensions du texte
SizeF stringSize = e.Graphics.MeasureString( tabPageText, tabControl.Font );
// Récupère l'image
Image tabPageImage = null;
if( tabControl.TabPages[ e.Index ].ImageIndex != -1 && tabControl.ImageList.Images.Count > tabControl.TabPages[ e.Index ].ImageIndex )
tabPageImage = tabControl.ImageList.Images[ tabControl.TabPages[ e.Index ].ImageIndex ];
// Calcul la position du texte
int stringLeft = (int)( e.Bounds.Left + ( e.Bounds.Width - ( tabPageImage != null ? tabPageImage.Width : 0 ) - stringSize.Width ) / 2 );
int stringTop = (int)( e.Bounds.Top + ( e.Bounds.Height - ( tabPageImage != null ? tabPageImage.Height : 0 ) - stringSize.Height ) / 2 );
// dessine le texte deux pixel plus bas s'il n'est pas sélectionné
if( tabControl.SelectedIndex != e.Index )
stringTop += 2;
// Dessine l'image
if( tabControl.TabPages[ e.Index ].ImageIndex != -1 && tabControl.ImageList.Images.Count > tabControl.TabPages[ e.Index ].ImageIndex )
e.Graphics.DrawImage( tabPageImage, e.Bounds.Left, e.Bounds.Top );
// Dessine le texte
e.Graphics.DrawString( tabPageText, tabControl.Font, foreBrush, stringLeft, stringTop );
}
Sauf que chez moi, l'image list est vide, alors que j'y ait mis 3 images ?!?
Dis-moi si c'est OK !!!