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?
Related
I am working on a GUI , and it is showing Transparency for no reason in place of white color of the image itself , what might be the cause of this ?
Original GUI before executing application:
Upon Executing the Application:
To solve this issue, reset the TransparencyKey back to its default value, or remove the assignment:
this.TransparencyKey = Color.Empty;
instead of
this.TransparencyKey = Color.Transparent;
References: https://stackoverflow.com/a/13426429/1132334, and documentation:
When the TransparencyKey property is assigned a Color, the areas of the form that have the same BackColor will be displayed transparently
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);
}
}
I have an Indian flag cursor like sym66.cur and I used it in C# code. It is running
but it is not getting in proper image. Instead, it is getting a black image. How can make the cursor display in the proper format?
This is my code:
private void New_Registration_Load(object sender, EventArgs e)
{
this.Cursor = new Cursor(Application.StartupPath + "\\sym66.cur");
}
Q: Do you see a black square instead of a cursor ... or is your color cursor displaying in black and white.
The WinForms "Cursor" only supports black and white, not color:
http://msdn.microsoft.com/en-us/library/system.windows.forms.cursor%28v=vs.110%29.aspx
The Cursor class does not support animated cursors (.ani files) or cursors with colors other than black and white.
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.