The Artima Developer Community
Sponsored Link

.NET Buzz Forum
C#: Rahmenfarbe beim Panel ändern

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
-

Posts: 1524
Nickname: nitronic
Registered: Jul, 2006

Norbert Eder works as a software architect.
C#: Rahmenfarbe beim Panel ändern Posted: Jul 10, 2007 3:07 AM
Reply to this message Reply

This post originated from an RSS feed registered with .NET Buzz by -.
Original Post: C#: Rahmenfarbe beim Panel ändern
Feed Title: Norbert Eder - Living .NET
Feed URL: http://feeds.feedburner.com/NorbertEder-Livingnet
Feed Description: Copyright (c)2005, 2006 by Norbert Eder
Latest .NET Buzz Posts
Latest .NET Buzz Posts by -
Latest Posts From Norbert Eder - Living .NET

Advertisement
Durch das Setzen der Eigenschaft BorderStyle auf FixedSingle erhält das Panel einen 2D-Rahmen, der fix auf Schwarz eingestellt ist. In manchen Fällen soll nun eine andere Rahmenfarbe her. Dazu muss von Panel abgeleitet und der Handler OnPaint überschrieben werden. Hier ein kleines Beispiel, bei dem sowohl die Rahmenfarbe, als auch die Rahmenstärke bequem über Eigenschaften eingestellt werden kann.
public partial class PanelEx : Panel
{
    [DllImport("user32.dll")]
    private static extern IntPtr GetWindowDC(IntPtr hwnd);
    [DllImport("user32.dll")]
    private static extern int ReleaseDC(IntPtr hwnd, IntPtr hdc);

    private Color _borderColor = Color.Black;
    private int _borderWidth = 1;

    public int BorderWidth
    {
        get { return _borderWidth; }
        set { _borderWidth = value; }
    }

    public Color BorderColor
    {
        get { return _borderColor; }
        set { _borderColor = value; }
    }

    public PanelEx()
    {
        this.InitializeComponent();

        SetStyle
            (ControlStyles.DoubleBuffer,
            true);
        SetStyle
            (ControlStyles.AllPaintingInWmPaint,
            false);
        SetStyle
            (ControlStyles.ResizeRedraw,
            true);
        SetStyle
            (ControlStyles.UserPaint,
            true);
        SetStyle
            (ControlStyles.SupportsTransparentBackColor,
            true);

    }

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);

        if (this.BorderStyle == BorderStyle.FixedSingle)
        {
            IntPtr hDC = GetWindowDC(this.Handle);
            Graphics g = Graphics.FromHdc(hDC);

            ControlPaint.DrawBorder(
                g, 
                new Rectangle(0, 0, this.Width, this.Height), 
                _borderColor, 
                _borderWidth, 
                ButtonBorderStyle.Solid, 
                _borderColor, 
                _borderWidth, 
                ButtonBorderStyle.Solid, 
                _borderColor, 
                _borderWidth, 
                ButtonBorderStyle.Solid, 
                _borderColor, 
                _borderWidth, 
                ButtonBorderStyle.Solid);
            g.Dispose();
            ReleaseDC(Handle, hDC);
        }
    }
}

Zusätzliche Erweiterungen sind natürlich denkbar und können gerne von jedem vorgenommen werden.

Read: C#: Rahmenfarbe beim Panel ändern

Topic: ASP.NET: Session-Informationen zur richtigen Zeit auslesen Previous Topic   Next Topic Topic: ASP.NET: Die PostBack-Falle

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use