I recently had a need to
overlay a warning over all the tools I had displayed on a Panel. Basically I wanted a transparent image over
the entire panel that said something like "Warning" over and over
again.
I found some clues on
how to do this at http://stackoverflow.com/questions/4463363/how-can-i-set-the-opacity-or-transparency-of-a-panel-in-winforms
[1]
This guide will be a
complete step by step on how to accomplish this.
From Visual Studio 2013
click on File -> New -> Project
To create a new project.
Choose a Windows Forms
Application and click OK
Drag and drop a few
buttons on the Form.
Drag and Drop a Data
Grid View as well.
Click Edit Columns
Click Add
Set the Header Text and
Click Add
Add a few more Column
Names like this
Click Cancel when you
are done
Click OK to close it out
Add a few labels on
their
Click Start to run it
and see what we got.
Success!
Create the Custom Panel Class
Right click on your
Windows application and select Add -> Class
Select Class
Name it
TransparentMessagePanel.cs
And click Add
Update the code to the
following
using System;
using
System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using
System.Windows.Forms;
namespace
WindowsFormsApplication1 {
class TransparentMessagePanel : Panel {
protected override CreateParams CreateParams {
get {
CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x00000020; // WS_EX_TRANSPARENT
return cp;
}
}
protected override void OnPaintBackground(PaintEventArgs e) {
//base.OnPaintBackground(e);
}
protected override void OnPaint(PaintEventArgs e) {
base.OnPaint(e);
Graphics g = e.Graphics;
Font font = new Font("Microsoft
Sans Serif", 36F,
System.Drawing.FontStyle.Bold,
System.Drawing.GraphicsUnit.Point,
((byte)(0)));
SizeF stringSize = new SizeF();
String str = "
www.whiteboardcoder.com
www.whiteboardcoder.com";
int rowHeight;
int textWidth;
int xAdjust = 0;
stringSize =
e.Graphics.MeasureString(str, font, 800);
rowHeight = (int)stringSize.Height +
(int)stringSize.Height;
textWidth = ((int)stringSize.Width);
g.RotateTransform(-20);
for (int x = 0; x <
(this.Width / textWidth)
+ 2; x++) {
for (int y = 0; y < 2 *
(this.Height / rowHeight)
+ 2; y++) {
xAdjust = textWidth / 2;
g.DrawString(str, font,
System.Drawing.Brushes.Red,
new Point(x * textWidth -
xAdjust,
y * rowHeight));
}
}
}
}
}
|
Save the file and recompile
Click on Start
Now the TransparentMessagePanel has been compiled it will
show up in the toolbox.
Search for Transpar to shorten the list.
Add A TransparentMessagePanel to the Form and resize it to
overlay all the other components.
Click on Start to run
That worked, but I can't click on any of the buttons as they
are behind this TransparentMessagePanel
To fix this do the following….
Select the TransparenteMessagePanel and in its properties
select Enabled to False.
Click on Start to run
Now you can click through on any of the other components.
There you go a transparent panel to overlay other components
that you can click through.
References
[1] How can I set the opacity or
transparency of a Panel in WinForms?
Visited 2/2014
I got this to work in Visual Studio 2012, the TransparentMessagePanel pops up immediately after build in the toolbox. This is not the case for Visual Studio 2013. In your instructions you say, 'From Visual Studio 2013 click on File -> New -> Project' ... So is this template for VS12 or VS13? Thank you.
ReplyDeleteI found a fix. In Visual Studio 2013. Go to Tools > Options > Windows Forms Designer > General
ReplyDeleteAt the bottom of the list you'll find Toolbox > AutoToolboxPopulate which on a fresh install defaults to False. Set it true and then rebuild your solution. Good luck to the next person!
Thanks for leaving that, I am sure it will help the next guy :)
DeleteThe panel is very slow and it does not look like it should be,
ReplyDelete