Content control panel in a form

Does WinForms support this? Let's say I want to show a pane containing a ListView from some results. It is displayed by clicking a button in the corner of the form, and the panel is displayed inside the form, primarily controls, preferably with a shadow in the back to make it more attractive.

I want this panel to always be inside the form, as part of it. One solution that comes to my mind is to create a limitless shape with this panel and make it synchronized with the original shape.

Is there some other way that I am missing?

+2


source to share


2 answers


Here's a rough method to get you started:

public Panel CreateFloatingPanel(Panel originalPanel)
{
    Bitmap bmp = new Bitmap(originalPanel.Width, 
        originalPanel.Height);
    Rectangle rect = new Rectangle(0, 0, 
        bmp.Width, bmp.Height);
    originalPanel.DrawToBitmap(bmp, rect);
    foreach (Control ctrl in originalPanel.Controls)
    {
        ctrl.Visible = false;
    }
    using (Graphics g = Graphics.FromHwnd(originalPanel.Handle))
    {
        g.DrawImage(bmp, 0, 0);
        bmp.Dispose();
        SolidBrush brush = 
            new SolidBrush(Color.FromArgb(128, Color.Gray));
        g.FillRectangle(brush, rect);
        brush.Dispose();
    }
    Panel floater = new Panel();
    floater.Size = originalPanel.Size;
    floater.Left = originalPanel.Left - 50;
    floater.Top = originalPanel.Top - 50;
    this.Controls.Add(floater);
    floater.BringToFront();
    return floater;
}

      

This method takes a panel with some controls, draws the panel with all its controls onto a temporary raster map, makes all controls invisible, draws a temporary raster map on the panel, then adds a semi-transparent gray layer to the panel. The method then creates a new panel and floats over the original panel, top and left, and then returns it. This basically makes the new panel a kind of modal popup, much like the way web pages sometimes do.



To use this in your form, place all the controls you want to be grayed out in the panel, then click the "Click" button to do something like this:

Panel floater = CreateFloatingPanel(panel1);
floater.BackColor = Color.White;
ListView lv = new ListView();
floater.Controls.Add(lv);

      

To undo the effect, you simply remove the floating panel from the Form's control collection, then make all the controls in the original panel visible again, and then update or invalidate the panel to get rid of the painted things. A simple Invalidate will work because the gray effect is not saved - to get that you have to make it harder. But that should get you started, at least.

+4


source


Could you create a hidden panel at the end of the form, then make it visible and then bring it to the front?



+2


source







All Articles