locked
Panel OnPaint lag RRS feed

  • Question

  • I'm working on a custom menu system. The aim is no having something rather like the track selection system in WMP 12.

     

    Track Name ##:##

     

    I want to be able to add a background/highlight on mouse over and on click/select, I want both fields to selected as though they are a single entity. I also want the menu to be confined to a small area and scroll on overflow.

     

    I've experimented with a data grid, however I was having issues with transparency and I couldn't get rid of the cell lines. These were deal breakers to me so on to my current solution.

    I'm now working with a set of generated labels in a panel to provide the scrolling. These are doing a lovely job. I've got scrolling and no grid lines. I've caught mouse over and click etc to prove the highlighting I wanted. The drawing the highlighting has caused me a few issues though. I would like to just draw the highlighting via the paint event for the panel. Then I could have everything nice and central and fairly straight forward. However if I add the paint code there I get lag when moving the mouse around to highlight. Below is the rough test code.

    int top = 0;
    private void Chapter_MouseEnter(object _sender, EventArgs _eventArgs)
    {
    	if (_sender is Label)
    	{
    		Label lblSender = (Label)_sender;
    		top = lblSender.Top;
    		lblSender.Parent.Refresh(); //Invalidate() is worse
    	}
    }
    
    private void panContents_Paint(object _sender, PaintEventArgs _eventArgs)
    {
    	Panel panSender = (Panel)_sender;
    	_eventArgs.Graphics.DrawRectangle(m_selectedBorder, 0, top, lblSampleChapterDetails.Right -1, lblSampleChapterDetails.Height -1);
    }


    The highlight lags behind the mouse and it's pretty noticeable. I have got around this this but the solution is not very nice. On mouse enter I set the background colour on both labels (the track and the duration) and then invalidate them so their on draw can draw the border. It does work but I'm not very happy with the code and was hoping someone could help me out with why I'm getting lag when using the panel's paint but not the label's. Both the label and panel are double buffered.

     

    • Moved by CoolDadTx Thursday, September 30, 2010 1:52 PM Winforms related (From:Visual C# General)
    Thursday, September 30, 2010 11:14 AM

Answers

  • Hi,

     

    Do you mean you want to hightlight the control under the drag rectangle?

     

    Form only allows one control to be hightlight. But we can draw the hightlight rectangle.

     

    If you want to draw on the panel, you can try the following:

     

            void panel1_Paint(object sender, PaintEventArgs e)

            {

                foreach (Control c in panel1.Controls)

                {      

                    e.Graphics.DrawRectangle(new Pen(Brushes.Blue), new Rectangle(c.Location,new Size( c.Size.Width-1,c.Size.Height-1))); 

                }

            }

     

    You can also create custom control for this, or handle the control paint event:

     

        public class MyButton : Button

        {

            private bool _IshightLight = false;

     

            public bool IshightLight

            {

                get { return _IshightLight; }

                set

                {

                    if (_IshightLight != value)

                        _IshightLight = value;

                    this.Refresh();

                }

            }

     

            protected override void OnPaint(PaintEventArgs pevent)

            {

                base.OnPaint(pevent);

     

                if (IshightLight == true)

                {

                    pevent.Graphics.DrawRectangle(new Pen(Brushes.Brown), new Rectangle(0, 0, pevent.ClipRectangle.Width - 1, pevent.ClipRectangle.Height - 1));

                }

            }

     

        }

     

     

    handle control paint event:

            void myButton1_Paint(object sender, PaintEventArgs e)

            {

                if (myButton1.IshightLight == true)

                {

                    e.Graphics.DrawRectangle(new Pen(Brushes.Pink), new Rectangle(0,0, e.ClipRectangle.Width-1,e.ClipRectangle.Height-1));

     

                }

            }

     

    Best regards,

    Ling Wang


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    • Marked as answer by Jing0 Monday, October 11, 2010 3:30 AM
    Tuesday, October 5, 2010 8:40 AM

All replies

  •   protected override void OnPaintBackground(System.Windows.Forms.PaintEventArgs e)
      {
       base.OnPaintBackground(e);
      }
    

    Have you tried just overriding the labels background paint event? If you are only painting backcolor..

     

     

    Thursday, September 30, 2010 8:14 PM
  • Hi,

     

    Do you mean you want to hightlight the control under the drag rectangle?

     

    Form only allows one control to be hightlight. But we can draw the hightlight rectangle.

     

    If you want to draw on the panel, you can try the following:

     

            void panel1_Paint(object sender, PaintEventArgs e)

            {

                foreach (Control c in panel1.Controls)

                {      

                    e.Graphics.DrawRectangle(new Pen(Brushes.Blue), new Rectangle(c.Location,new Size( c.Size.Width-1,c.Size.Height-1))); 

                }

            }

     

    You can also create custom control for this, or handle the control paint event:

     

        public class MyButton : Button

        {

            private bool _IshightLight = false;

     

            public bool IshightLight

            {

                get { return _IshightLight; }

                set

                {

                    if (_IshightLight != value)

                        _IshightLight = value;

                    this.Refresh();

                }

            }

     

            protected override void OnPaint(PaintEventArgs pevent)

            {

                base.OnPaint(pevent);

     

                if (IshightLight == true)

                {

                    pevent.Graphics.DrawRectangle(new Pen(Brushes.Brown), new Rectangle(0, 0, pevent.ClipRectangle.Width - 1, pevent.ClipRectangle.Height - 1));

                }

            }

     

        }

     

     

    handle control paint event:

            void myButton1_Paint(object sender, PaintEventArgs e)

            {

                if (myButton1.IshightLight == true)

                {

                    e.Graphics.DrawRectangle(new Pen(Brushes.Pink), new Rectangle(0,0, e.ClipRectangle.Width-1,e.ClipRectangle.Height-1));

     

                }

            }

     

    Best regards,

    Ling Wang


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    • Marked as answer by Jing0 Monday, October 11, 2010 3:30 AM
    Tuesday, October 5, 2010 8:40 AM