Panel of controls over content in the form - c#

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?

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.

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

Related

C# Winforms Get a Form's Stretched Background Image

How can I retrieve a form's stretched image? When I use MyForm.BackgroundImage it gives me the original image, but not the stretched image that is being displayed on the form.
If I cannot get the image, can I recreate the resulting image from BackgroundImageLayout = Stretch?
Why I want to do this:
I have a control that does not allow transparency. In order to fake transparency I take the background image and create an image for the section that the control covers. This works well until I set the BackGroundImageLayout to anything other than none.
Any help is greatly appreciated, thanks.
You can retrieve/re-create the stretched form background image like so:
private Bitmap getFormBackgroundImage()
{
Bitmap bmp = new Bitmap(this.ClientSize.Width, this.ClientSize.Height);
using (Graphics g = Graphics.FromImage(bmp))
{
g.DrawImage(this.BackgroundImage,
new Rectangle(0, 0, bmp.Width, bmp.Height));
}
return bmp;
}
Then you can crop a portion of it to use as the child control's background.
Try this :
a) Set the background image of the form (Base Control).
this.BackgroundImage = <image>;
b) Create a child control and drop it over over your base control.
c) Set the dock of your child control to Fill.
this.childControl.Dock = DockStyle.Fill;
d) In the base control constructor , set the background image for the child control as the image of the base control. Like this:
childControl.BackgroundImage = this.BackgroundImage;
e) Set the background image layout of the child control to strech.
childControl.BackgroundImageLayout = ImageLayout.Stretch;
This would make your child control appear as transparent. Hope it solves your problem.

visual c# draw graphics in front of buttons

I'm working on a winform project (visual c#). One of the forms has many buttons on it and they are located next to each other. I want to add graphics in front of those buttons, in a way the user could keep pressing the buttons. I can't add the graphics behind because the buttons hide the graphics and I don't want to move the buttons. I tried to bring the controls to the back but it didn't work. Any ideas? Thanks!
toPolygon.Insert(toPolygon.Count, button); //insert button to vector
System.Drawing.Pen myPen;
myPen = new System.Drawing.Pen(System.Drawing.Color.Black);
myPen.Width = 10;
System.Drawing.Graphics formGraphics = this.CreateGraphics();
formGraphics.DrawLine(myPen, toPolygon[toPolygon.Count - 2].Location.X, toPolygon[toPolygon.Count - 2].Location.Y,
button.Location.X , button.Location.Y); //draw a line from last button in the vector to the new one
myPen.Dispose();
if (toPolygon.First() == toPolygon.Last())
{
//draw polygon and clean the vector
}
You can do 2 things here, you can do what GuidoG said and make the button image the image you are trying to put over the button or in your form Designer if you click on your button and change it's BackColor to transparent by going to the "Web" tab and then change the "FlatSytle" to be "Flat" and then change the "ForeColor" to be transparent too. That way you will not see the button at all and be able to see everything behind it and still use it as a control.
And if you are trying to draw like lines over the control ect. you will have to overload the "OnPaint" method for that control.
Hope it helps!

Resize PictureBox as resizing the form

I'm loading multiple images into a Panel (multiple PictureBoxes inside a Panel) and would like to resize the images as the windows form is resized.
Here is my code:
foreach (string filename in ofdmulti.FileNames){
picbox[i] = new PictureBox();
picbox[i].Size = new System.Drawing.Size(256, 256);
picbox[i].SizeMode = PictureBoxSizeMode.Zoom;
picbox[i].Dock = DockStyle.Fill;
i++;
}
But then I don't see the multiple images, just one and stretched fully, what may be wrong?
You have multiple issues with your code. First off, this line of code will ensure that you only see one PictureBox...likely the last one you added:
picbox[i].Dock = DockStyle.Fill;
Second, I don't see that you are setting the PictureBox Location, so they are all going to Point(0, 0), meaning they would overlap to some extent even regardless of the Dock setting.
If you are trying to get a nice arrangement, such as Tiled, then you could use a TableLayoutPanel. That would allow you to describe a grid pattern with the Rows and Columns and then add your PictureBox controls to the grid.
There are other options, of course, depending upon your goal.
Replace picturebox.Image with Your resized Image, and use sizemode.AutoSize!

c# - clear surface when resizing

I'm trying to build my own custom control for a windows forms application in C#.Net. Currently I paint some rectangles and other graphic elements using the paint event.
When I now resize the app form to fit the desktop size, all elements are repainted (which is exactly the behaviour I need) but the old one's are shown in the background.
Here's what I'm doing by now:
Pen penDefaultBorder = new Pen(Color.Wheat, 1);
int margin = 5;
private void CustomControl_Paint(object sender, PaintEventArgs e) {
CustomControl calendar = (CustomControl)sender;
Graphics graphics = e.Graphics;
graphics.Clear(Color.WhiteSmoke);
graphics.DrawRectangle(penDefaultBorder, margin, margin, calendar.Width - margin * 2, calendar.Height - margin * 2);
//...
}
Neither the graphics.Clear, nor adding a graphics.FillRectangle(...) will hide the old rectangle from the surface.
Ideas? Thank you all.
Paint events usually don't request an update for the entire canvas, just the area specified in the PaintEventArgs. I'm guessing what's happening is that only the newly-exposed regions of the canvas are being passed in the PaintEventArgs.
This one of the reasons that you shouldn't do any rendering in the Paint event. You should render to an offscreen bitmap - a buffer - and copy from that buffer to the control's canvas in the Paint event.
Searching for "double buffering" here or on Google will give you many examples of the technique.
Have you tried .Invalidate() to cause the form to redraw?

Painted Border Runs In Certain Situations

In my tool I use a a panel to change pages. Each page has it's own panel and when I change a page I send the panel with the controls. On the panel I use as the canvas I have the following paint event:
private void panelContent_Paint(object sender, PaintEventArgs e)
{
e.Graphics.CompositingQuality = CompositingQuality.HighQuality;
e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
// Paints a border around the panel to match the treeview control
e.Graphics.DrawRectangle(Pens.CornflowerBlue,
e.ClipRectangle.Left,
e.ClipRectangle.Top,
e.ClipRectangle.Width - 1,
e.ClipRectangle.Height - 1);
e.Graphics.Flush();
base.OnPaint(e);
}
This method basically draws a nice border around the panel so it look better. For some reason when I move a another form above this panel the lines that make up the border start to run a little. Occasionally small lines will be drawn from the border too. The problem only happens for a few seconds before the entire panel redraws again. Is there anything I can do to prevent this from happening?
ClipRectangle tells you what part of the control needs to be repainted. If you're moving something over it, this is probably going to be the intersection of your object and the one being moved. You can use this information to more efficiently repaint your control.
You probably want to draw the rectangle from (0, 0) to (panelContent.Width-1, panelContent.Height-1).

Categories