views:

289

answers:

3

Does WinForms have support for this? Let's say I want to show a panel containing ListView made of some results. It is shown by clicking on a button in a form corner, and the panel is shown inside the form, above all controls, preferably with shadow in the back to make it more attractive.

I want this panel to be always inside the form, as a part of it. One solution that comes to my mind is to create a border-less form with that panel, and to make it in sync with original form.

Is there maybe some other way that I'm missing?

+1  A: 

Could you just create a hidden Panel at the back of the Form, and then make it visible, and then bring it to front?

Joe
No need to have it in back and then bring to front ... just toggle its visibility.
JustLoren
Not really what I want ... :(
Sapphire
+1  A: 

Here's a crude method that will 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 on it, draws the panel with all its controls onto a temporary bitmap, makes all the controls invisible, draws the temporary bitmap onto the panel, then adds a semi-transparent Gray layer onto the panel. The method then creates a new panel and floats it over the original panel, above and to the left, and then returns it. This basically makes the new panel a sort of modal popup window, kind of like how web pages do it sometimes.

To use this on your form, put all the controls you want to be grayed-out underneath onto a panel, then have your button's Click event 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 would just remove the floating panel from the form's Controls collection, then make visible again all the controls on the original panel, and then Refresh or Invalidate the panel to get rid of the grayed-out stuff. The simple Invalidate will work because the grayed-out effect does not persist - to get that you'd have to make this all a bit more complicated. But this should get you started, at least.

MusiGenesis
A: 

HI iam working in the same situation and the DrawToBitmap is not working in asp.net and c#.

Vani