What I want to achieve is a semi-transparent background that covers the screen.
And then be able to draw non-transparent brushes on top of it.
What I have tried is to have a form with the size of the screen, then setting it's color and setting this.opacity = 0.5. However this affects all brushes in the form.
Ive also tried setting the background color to Color.Transparent, and then drawing an additional brush that covers the screen with opacity 0.5 before I then draw the opaque brushes... However the background becomes opaque as well.
Even though the style flag is set (ControlStyles.SupportsTransparentBackColor, true)
I know I can achieve this by having an additional form.
One form for the transparent background and one for the opaque foreground, but isn't that overkill?
Is there a better way?
Update 1:
Trying what is suggested in the comments.
Current state:
Form1: the main program, calls for the 'overlay' to show, which is:
Form2: Overlay background (semi-transparent black) and,
Form3: Overlay foreground, this is where the user draws.
Form 1 and 2 works as indended, however Form3 refuses to work with transparency.
If I set
this.BackColor = Color.Lime;
this.TransparencyKey = Color.Lime;
then the performance drops and the program lags heavily (although it does become transparent).
Ideally I would want to use this.BackColor = Color.Transparent; however that doesn't have any effect (solid background, no alpha).
Note that the form covers the screen and the background is usually the desktop. Maybe that's why it doesn't work?
You can override the OnPaintBackground of the form, without calling it's base.
This will enable you to specify whatever color you want for the background.
Try this:
protected override void OnPaintBackground(PaintEventArgs e)
{
using (var brush = new SolidBrush(Color.FromAgrb(50, 0, 0, 0))
{
e.Graphics.FillRectangle(brush, e.ClipRectangle);
}
}
Related
I am trying to set the form's background as invisible as I want the background image to be the thing that has the transparency.
I have used multiple background colours; - lime, magenta, red, white with the same transparency key but they all give me a horrible result. You can see that I have set the backcolour to be yellow, and the transparency key to yellow but at the top there is still yellow
I have also used
SetStyle(ControlStyles.SupportsTransparentBackColor, true);
this.BackColor = Color.Transparent;
That didn't work either and neither did
protected override void OnPaintBackground(PaintEventArgs e) { /* Ignore */ }
Does anyone have a way that I can do this?
I created a simple stick man in a Windows Form User-Control (consisting of a radio button and three labels and one progress bar).
I set the back-color of the new user-control to transparent so that when I drag it onto my form, it blends with other colors and drawings on the form.
I am not getting what I'm trying to achieve.
Here is the picture:
UserControl already supports this, its ControlStyles.SupportsTransparentBackColor style flag is already turned on. All you have to do is set the BackColor property to Color.Transparent.
Next thing you have to keep in mind in that this transparency is simulated, it is done by asking the Parent of the control to draw itself to produce the background. So what is important is that you get the Parent set correctly. That's a bit tricky to do if the parent is not a container control. Like a PictureBox. The designer will make the Form the parent so you will see the form's background, not the picture box. You'll need to fix that in code, edit the form constructor and make it look similar to this:
var pos = this.PointToScreen(userControl11.Location);
userControl11.Parent = pictureBox1;
userControl11.Location = pictureBox1.PointToClient(pos);
In constructor set style of control to support a transparent backcolor
SetStyle(ControlStyles.SupportsTransparentBackColor, true);
and then set Background to transperent color
this.BackColor = Color.Transparent;
From MSDN
A more complex approach (and possibly working one) is described here - with overrding of CreateParams and OnPaint.
Why all those things?
UserControl class has property Region.
Set this to what ever shape you like and no other adjustments are needed.
public partial class TranspBackground : UserControl
{
public TranspBackground()
{
InitializeComponent();
}
GraphicsPath GrPath
{
get
{
GraphicsPath grPath = new GraphicsPath();
grPath.AddEllipse(this.ClientRectangle);
return grPath;
}
}
protected override void OnPaint(PaintEventArgs e)
{
// set the region property to the desired path like this
this.Region = new System.Drawing.Region(GrPath);
// other drawing goes here
e.Graphics.FillEllipse(new SolidBrush(ForeColor), ClientRectangle);
}
}
The result is as in the image below:
No low level code, no tweaking, simple and clean.
There is however one issue but in most cases it can go undetected, the edges are not smooth and anti-aliasing will not help either.
But the workaround is fairly easy. In fact much easier than all those complex background handling..
This might sound like a weird question but I have C# Winform that I set the FormBorderStyle to None. So far everything is good but I was wondering if there was a way to add like a 1px border on around my form ? I know I could do it by creating my own image but I was wondering if there was a more natural way of doing it.
Thanks
I consider using an image, or creating unnecessary controls for something that is easily paintable using GDI+ a waste of resources.
I think the simplest solution is overriding the OnPaint method of your form and drawing the border yourself:
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.DrawRectangle(Pens.Black, this.Bounds);
}
Of course, you may also use your own Pen with your own color and width.
Use padding 1;1;1;1 to your form and set a background color to your form, and put a panel to your form. Set white or other normal background color to the panel. And set dock in parent controller. The background color of the form will act as a border.
How about just adding a Panel (and setting it's border) to the Form?
Thanks for the suggestions, I've decided to create 4 1px label and just toss on the edge on each side. That way:
1. They are minding their own business on the side rather than taking up the whole middle if you use use a groupbox or panel.
2. You are able to choose change your border color.
There is no more natural or non natural ways to do it. It depends on what you want.
If you put a background image on the form, you have to consider a fact that in order to be able to support resizable for you have to have resizable background images.
If you simply draw on the background with a Pen or Brush, you can support also resizable form, but you have to work more if you want to do something cool, instead with image it's easier.
You can embed some control inside the form and with color's of them make a feeling of the border. Like control, you can use Panel, as suggested in comment, can use GroupBox that creates thin broder arround, or something else.
I created this method, so you could easily set the borderposition, color and thickness.
private void customBackgroundPainter(PaintEventArgs e, int linethickness = 2, Color linecolor = new Color(), int offsetborder = 6)
{
Rectangle rect = new Rectangle(offsetborder, offsetborder, this.ClientSize.Width - (offsetborder * 2), this.ClientSize.Height - (offsetborder * 2));
Pen pen = new Pen(new Color());
pen.Width = linethickness;
if (linecolor != new Color())
{
pen.Color = linecolor;
}
else
{
pen.Color = Color.Black;
}
e.Graphics.DrawRectangle(pen, rect);
}
You could use it in the OnPaintBackground likes this:
protected override void OnPaintBackground(PaintEventArgs e)
{
base.OnPaintBackground(e);
customBackgroundPainter(
e,
linethickness: 3,
linecolor: Color.DarkOrange,
offsetborder: 5
);
}
The idea of my project is to show solid text on a transparent form control.
I have used this technique to make the form transparent:
BackColor = Color.Lime;
TransparencyKey = Color.Lime;
The problem I'm having is coloured edges around the text. I've tried drawing anti-aliased text using graphics and displaying the text using labels but neither worked. I still have disgusting-looking, pixelated, lime edges around my text.
I looked around a little - posts are usually concerned with making the form transparent not dealing with this issue.
You can get reasonable output by using TextRenderingHint.AntiAliasGridFit.
private void TestForm_Paint(object sender, PaintEventArgs e) {
e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;
e.Graphics.DrawString("Header", this.Font, SystemBrushes.WindowText, new Point(1, 1));
}
But if you plan on using large fonts, it won't render too well since it can't really antialias properly.
The nature of fonts, in general, is to have a background to draw on. If you have black text on a transparent form, and the end user has a black background-- the end user isn't going to see anything.
I am writing a program in Visual C# 2010 that has several icons on the form. When the mouse is placed over the icon (which is just an image) I want the icon to b highlighted via a border around the icon. In visual basic i can make a transparent rectangle with a colored border and position it over the icon. In C#, i can do the same however until i call invalidate multiple borders appear. The problem with calling invalidate is that my program is doing something in the background every second so the border keeps flashing (re-drawing).
Anyone got any ideas how i can implement this?
You didn’t say how you draw the border but from your description you are creating a graphics context for this. Don’t do this, it’s wrong. Instead, draw inside the Paint element of either the control or the its parent container.
The Paint event handler could look as follows:
private void yourControl_Paint(object sender, PaintEventArgs e)
{
if (! HasFocus(yourControl))
return;
Graphics g = e.Graphics;
using (Pen p = new Pen(Color.FromArgb(128, 0, 0, 128)))
g.DrawRectangle(p, 0, 0, yourControl.Width -1, yourControl.Height - 1);
}
This uses a hypothetical HasFocus method to determine whether this control should have a focus rectangle.
By the way, this is identical in VB and C#.