J'ai le contrôle personnalisé suivant et peut placer sur un formulaire (avec AutoScroll mis à vrai et le contrôle ancré à gauche, haut et à droite).Barre de défilement est toujours peint après qu'il devrait être retiré
Si le formulaire est trop court pour le contrôle, le formulaire redimensionne correctement le contrôle (pour faire de la place pour le défilement) et affiche la barre de défilement. Lorsque le contrôle est fermé à l'aide du glyphe de fermeture, le contrôle est redimensionné et la barre de défilement est supprimée, mais il arrive que la barre de défilement reste peinte. Si le formulaire est réduit ou déplacé hors écran, la peinture restante est supprimée.
J'ai essayé Parent.Invalidate et ai joué avec de nombreuses façons mais en vain. Aucune suggestion?
(VS 2008 L'utilisation standard)
alt text http://waltware.homedns.org/screenshot.jpg
using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
namespace GroupPanelTest
{
public class GroupPanel : GroupBox
{
#region Members
private const Int32 iHeaderHeight = 20;
private Int32 iFullHeight = 200;
private Boolean bClosed = false;
private Rectangle rectCloseGlyphBounds = Rectangle.Empty;
private Boolean bIsMoveOverCloseGlyph = false;
#endregion
#region Properties
[DefaultValue(false)]
public Boolean Closed
{
get
{
return (this.bClosed);
}
set
{
if (this.bClosed != value)
{
this.bClosed = value;
if (this.bClosed)
{
this.iFullHeight = base.Height;
base.Height = GroupPanel.iHeaderHeight;
}
else
{
base.Height = this.iFullHeight;
}
foreach (Control con in base.Controls)
con.Visible = !this.bClosed;
this.Invalidate();
}
}
}
public new Int32 Height
{
get
{
return (base.Height);
}
set
{
if (value != base.Height)
{
if (this.Closed)
{
this.iFullHeight = value;
}
else
{
Int32 iOldHeight = base.Height;
base.Height = value;
}
}
}
}
[DefaultValue(typeof(Size), "350,200")]
public new Size Size
{
get
{
return (base.Size);
}
set
{
if (base.Size != value)
{
base.Size = value;
if (!this.Closed)
this.iFullHeight = value.Height;
}
}
}
[DefaultValue(typeof(Padding), "0,7,0,0")]
public new Padding Padding
{
get
{
return (base.Padding);
}
set
{
base.Padding = value;
}
}
#endregion
#region Construction
public GroupPanel()
{
SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.ResizeRedraw, true);
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
SetStyle(ControlStyles.Selectable, true);
this.Size = new Size(350, 200);
this.Padding = new Padding(0, 7, 0, 0); // the groupbox will add to that
this.rectCloseGlyphBounds = new Rectangle(base.ClientSize.Width - 24, 2, 16, 16);
}
#endregion
#region Overrides
protected override void OnSizeChanged (EventArgs e)
{
this.rectCloseGlyphBounds = new Rectangle(base.ClientSize.Width - 24, 2, 16, 16);
base.OnSizeChanged(e);
}
protected override void OnPaint (PaintEventArgs e)
{
base.OnPaint(e); // we want all the delegates to receive the events, but we do this first so we can paint over it
Graphics g = e.Graphics;
g.FillRectangle(SystemBrushes.Window, this.ClientRectangle);
Rectangle rectTitle = new Rectangle(0, 0, this.ClientRectangle.Width, GroupPanel.iHeaderHeight);
g.FillRectangle(SystemBrushes.Control, rectTitle);
g.DrawString(this.Text, this.Font, SystemBrushes.ControlText, new PointF(5.0f, 3.0f));
if (this.bIsMoveOverCloseGlyph)
{
g.FillRectangle(SystemBrushes.ButtonHighlight, this.rectCloseGlyphBounds);
Rectangle rectBorder = this.rectCloseGlyphBounds;
rectBorder.Inflate(-1, -1);
g.DrawRectangle(SystemPens.Highlight, rectBorder);
}
using (Pen pen = new Pen(SystemColors.ControlText, 1.6f))
{
if (this.Closed)
{
g.DrawLine(pen, this.rectCloseGlyphBounds.Left + 3, this.rectCloseGlyphBounds.Top + 3, this.rectCloseGlyphBounds.Left + 8, this.rectCloseGlyphBounds.Top + 8);
g.DrawLine(pen, this.rectCloseGlyphBounds.Left + 13, this.rectCloseGlyphBounds.Top + 3, this.rectCloseGlyphBounds.Left + 8, this.rectCloseGlyphBounds.Top + 8);
g.DrawLine(pen, this.rectCloseGlyphBounds.Left + 3, this.rectCloseGlyphBounds.Top + 7, this.rectCloseGlyphBounds.Left + 8, this.rectCloseGlyphBounds.Top + 12);
g.DrawLine(pen, this.rectCloseGlyphBounds.Left + 13, this.rectCloseGlyphBounds.Top + 7, this.rectCloseGlyphBounds.Left + 8, this.rectCloseGlyphBounds.Top + 12);
}
else
{
g.DrawLine(pen, this.rectCloseGlyphBounds.Left + 3, this.rectCloseGlyphBounds.Top + 8, this.rectCloseGlyphBounds.Left + 8, this.rectCloseGlyphBounds.Top + 3);
g.DrawLine(pen, this.rectCloseGlyphBounds.Left + 13, this.rectCloseGlyphBounds.Top + 8, this.rectCloseGlyphBounds.Left + 8, this.rectCloseGlyphBounds.Top + 3);
g.DrawLine(pen, this.rectCloseGlyphBounds.Left + 3, this.rectCloseGlyphBounds.Top + 12, this.rectCloseGlyphBounds.Left + 8, this.rectCloseGlyphBounds.Top + 7);
g.DrawLine(pen, this.rectCloseGlyphBounds.Left + 13, this.rectCloseGlyphBounds.Top + 12, this.rectCloseGlyphBounds.Left + 8, this.rectCloseGlyphBounds.Top + 7);
}
}
}
protected override void OnMouseDown (MouseEventArgs e)
{
if (e.Button == MouseButtons.Left && this.rectCloseGlyphBounds.Contains(e.Location))
this.Closed = !this.Closed; // close will call invalidate
base.OnMouseDown(e);
}
protected override void OnMouseMove (MouseEventArgs e)
{
this.bIsMoveOverCloseGlyph = this.rectCloseGlyphBounds.Contains(e.Location);
this.Invalidate(this.rectCloseGlyphBounds);
base.OnMouseMove(e);
}
#endregion
}
}
Quelque chose de très étrange dans votre capture d'écran. Comment le glyphe a-t-il réussi à être peint au sommet de la barre de défilement? Chasse ça. –