I want to draw the Labels on PictureBoxes but should be transparent background .
This is my code : `
Labels[i].Location = new Point(0, 0);
Labels[i].Size = new Size(13, 13);
Labels[i].BackColor = Color.Transparent;
Labels[i].ForeColor = Color.Blue;
Invoke(new MethodInvoker(delegate { Panels.Controls.Add(Pictures[i]); }));
Invoke(new MethodInvoker(delegate { Panels.Controls.Add(Labels[i]); }));
I'am drawing my Pictures fine, Labels too ... But I get the Labels under the Pictures (I can see each label when I drag the Picture out of his place)
so I want to know , how I can put them in Front with Transparent BackGround . thank you
`
i think you should use something like z-index
Try calling Panels.Controls[Labels[i].Name].BringToFront(); in your delegate. This should bring each label in front of the pictures since they've already been added to the panel at that point.
I suppose you've already googled for this but here's a good link about winforms transparency.
I used to write many winforms applications that used very ugly skins with transparent labels. As I can remember there was some glitches when used jpg as a background picture and if picture loaded after the label has been drawn.
Anyway, I suggest overriding onPaint and onDraw methods as you can see in the link.
Transparency is only with respect to the container. For this to work you have to add the Label to the PictureBox. Change Panels to the name of the PictureBox you are working with in the Controls.Add() line.
Related
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!
I hope you can help me with this problem, attached videos to explain in a simpler way.
First example
Panel (has a textured background) with labels (the labels have a png image without background)
Events: MouseDown, MouseUp and MouseMove.
As you will notice in the video to drag the label the background turns white panel and regains its background image when I stop dragging the label
Panel controls have a transparent background as property, but changing the background with any color, let the problem occurred related to the substance, I do not understand why this happens and how to fix less.
Second Example
Contains the above, with the only difference that the panel controls instead of having transparent background, I chose black color for that property
You have to use double buffer and you don't have to stop using an image on the background, you can have everything running smoothly.
You have a couple of ways to do this, the fast way (not enough most of the time) is to enable doublebuffer of the panel.
The "slow" but better way is to do your own Double Buffer using a Bitmap object as a buffer.
This example creates a "side buffer" and accepts an image as parameter and draws it using created buffer.
public void DrawSomething(Graphics graphics, Bitmap yourimage)
{
Graphics g;
Bitmap buffer = new Bitmap(yourimage.Width, yourimage.Height, graphics);
g = Graphics.FromImage(buffer);
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.DrawImage(yourimage, 0, 0);
graphics.DrawImage(buffer, 0, 0);
g.Dispose();
}
Call this on your OnPaint event.
BTW... this is just a double buffer example.
Cheers
Change DoubleBuffered to true for both form and panel. I think that should solve your problem.
this is totally normal, because System.Windows.Forms.Control based items were not designed to do this kind of advanced Graphics operations.
in fact the reason that this effect happens here, is that when you assign any value other than 255 to the alpha component of a control BackColor, the form does the following when you change the control size or position:
it sets the new control position
it redraws the parent control
it gets the background of the control's parent as an image
it draws acquired image into the control body to seem as if the control is transparent
the control body gets drawn on top of the previously drawn background
the control children are drawn
* this is is a simplified explanation for the sake of illustration to deliver the idea
steps 1, 2 are responsible for the flickering effect that you see.
but you have two ways to solve this,
-the first is some kinda advanced solution but it's very powerful, which is you would have to create a double buffered custom control that would be your viewport.
the second is to use WPF instead of windows forms, as WPF was designed exactly to do this kind of things.
if you can kindly provide some code, i can show you how to do both.
UPDATE: I took a break from messing with the transparency stuff for a few days. I started messing with it again tonight. I got a new result using Hans Passant's solution:
http://img3.imageshack.us/img3/4265/icontransp.jpg
Passant's solution does solve the issue of the transparent background gradient. However, I'm still running into the problem with the transparent colors in my icon blending with the form's BackColor. You can see the fuchsia around various parts of the icon in the above image.
ORIGINAL CONTENT:
I've been going at this for several hours now, and I haven't had much luck. I've messed with Control.Region, Form.TransparencyKey, Form.Opacity, and a couple other random things with some funky effects.
Lately I've been trying to customize my desktop and decided to mess with Application Docks. After seeing what the Mac dock and a few third-party Windows implementations had to offer, I decided I wanted to build my own.
Eventually I want to move on to using the Win32 API. For now I just want to get something working using as much C# and .Net framework capabilities as possible.
There are a few things I want to be able to do in this application:
Display a form/menu with a gradient background.
Allow the form/menu to have transparency while keeping icons opaque.
Display icons that contain transparent backgrounds.
The Menu and Icons should be able to receive mouse-related events (hover, leave, click, dragover, dragdrop, and a few others).
This is the effect I'm shooting for:
http://img207.imageshack.us/img207/5716/desired.jpg
This image shows the visual effects I'm trying to achieve. This was a skin I made for a program called Rainmeter. The image shows Notepad++ behind the skin with a few of the skin's files open in the editor. The menu is transparent, but the icons remain opaque.
My Approach:
Using a Form to act as the menu seemed like a logical first choice to me. I have a basic understanding of events. I'm not quite sure how to create my own click events, so a form would make working with events a tad easier. I considered a few options for the icons. I decided I'd use PictureBoxes for the icons, since they can hold images and receive events.
Once I finished the code for all the structural logic of my menu, I started playing around with it to try to get the visual effect I wanted. Form.Opacity affected the transparency of everything on the form. Since I want the icons to be fully opaque, I left this property alone. I tried setting the BackColor to Color.Transparent, but that gives an error. I played around with a few combinations...
http://img204.imageshack.us/img204/757/effectsi.jpg
I drew the gradient with a Drawing2D.LinearGradientBrush into a Bitmap. This Bitmap was then placed as the Form.BackgroundImage or as a PictureBox.Image. If used, the PictureBox was sized to cover the entire Form and sent to the back.
I noticed that some of the Form.BackgroundColor would be mixed in with the outlines of my icons. The icons have transparency along the edges for a smoother appearance. Since the icons are picking up the Form's BackgroundColor, this makes me think that the PictureBoxes are creating new images when the icons are loaded into the form. The semi-transparent portions of the image are then merged with the Form's BackgroundColor when they should merge with whatever colors are behind the form.
http://img838.imageshack.us/img838/8299/whitedesktop.jpg
In this image you can see the Fuchsia existing in the icons even though the Form's Fuchsia color is now completely transparent. I forgot to point out that the same green to yellow gradient with an Alpha value of 150 was used in every case. In the images where the gradient doesn't look green, it's because the transparent colors are blending with the Fuchsia background.
I'm not really sure what to do from here. I feel like I could get what I want if I could somehow make the Form alone completely transparent. I was also thinking I may have better luck just drawing the icons instead of using PictureBoxes. The problem then would be setting up the icons to receive mouse events. (I've never made my own events, and I think it would involved some Win32 API calls.)
Is there something else I can do with the PictureBoxes to get the effect I want? Whichever the case, I'm open to any ideas or suggestions for the overall effect I'm trying to achieve.
This is pretty easy to do in Winforms. What you need is a sandwich of two forms. The bottom one should provide the transparent gradient background, the top one should draw the icons and handle mouse clicks. Some sample code:
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
this.TopMost = true;
this.FormBorderStyle = FormBorderStyle.None;
this.TransparencyKey = this.BackColor = Color.Fuchsia;
this.Opacity = 0.3;
var overlay = new Form();
overlay.FormBorderStyle = FormBorderStyle.None;
overlay.TransparencyKey = overlay.BackColor = Color.Fuchsia;
overlay.StartPosition = FormStartPosition.Manual;
overlay.Location = this.Location;
overlay.MouseDown += HandleIconClick;
this.Resize += delegate { overlay.Size = this.Size; };
this.LocationChanged += delegate { overlay.Location = this.Location; };
overlay.Paint += PaintIcons;
this.Paint += PaintBackground;
this.Load += delegate { overlay.Show(this); };
}
private void PaintBackground(object sender, PaintEventArgs e) {
var rc = new Rectangle(0, 0, this.ClientSize.Width, this.ClientSize.Height);
using (var br = new LinearGradientBrush(rc, Color.Gainsboro, Color.Yellow, 0f)) {
e.Graphics.FillRectangle(br, rc);
}
}
private void PaintIcons(object sender, PaintEventArgs e) {
e.Graphics.DrawIcon(Properties.Resources.ExampleIcon1, 50, 30);
// etc...
}
void HandleIconClick(object sender, MouseEventArgs e) {
// TODO
}
}
Which looks like this with the somewhat random colors and icon I selected:
OK, I got a bit lost in all that, but from the description in the original paragraph, I would make sure the background rectangle is NOT the visual parent of the pictureboxes. Make them overlapping siblings, with the pictureboxes in front using Panel.Zindex.
Then you can just change the opacity of the rectangle, without affecting the icons. Also make sure the icon source image files have a transparent background.
Should work I think.
I would like to have a form in which the controls on the form are fully visible but the form itself is invisible. If I change the form's Opacity, this makes both the form and the controls on it semi-transparent, so this doesn't work.
I can't do this by setting the form's TransparencyKey, since I have a PictureBox on the form. If the image in the PictureBox happens to contain pixels that match the TransparencyKey, they appear as openings in the form, which I don't want.
TransparencyKey is the only way to get this. Pick the right color. Color.Fuchsia has a long tradition of being the color of choice, going back to the early days of Win32 development. Assault your eye with it to see its merits.
With the caveat that I've never used it, just ran across it once, thought "neat!" and moved on...
Look into System.Drawing.Drawing2D.GraphicsPath and setting the form's Region property. I added two buttons to the basic Windows forms application:
public Form1()
{
InitializeComponent();
Rectangle r1 = new Rectangle(button1.Location, button1.Size);
Rectangle r2 = new Rectangle(button2.Location, button2.Size);
GraphicsPath gp = new GraphicsPath();
gp.AddRectangle(r1);
gp.AddRectangle(r2);
this.Region = new Region(gp);
}
I've approximated the shape of the button with a rectangle; with this code, you can see the form background color at the buttons' corners. You'll need to work out the enclosing path for each of your controls and add them to the path individually. You'll need to take into account any offset introduced by the form title bar or border style.
Update: I did some investigation and have a couple of possible approaches for the problem:
Using the GraphicsPath method, set pictureBox.Visible to False if there is no image loaded.
When you load an image into the picture box, analyze the image to get a list of all the colors in it, then randomly generate one that isn't. Set the form's BackColor and TransparencyKey properties to match this new color, Hans Passant's answer.
What's the quickest way to show a red/green light indicator on a C# form?
I originally thought about using radio buttons, but not sure how to set the color of the dot, only the foreground/background text.
Then I thought about drawing a circle. Couldn't find a toolbox shape for that, and didn't want to write code just to draw the circle.
Basically, I'm writing a little application specific monitor, that shows a red light if certain services are down, or certain web services are not responding.
This is what I have so far using a square button instead of a circle. The code is just what I want, I just want a round shape.
if (allGood)
{
btnIISIndicator.BackColor = Color.Green;
}
else
{
btnIISIndicator.BackColor = Color.Red;
}
This is simple, just use System.Windows.Shapes for the object and System.Windows.Media.Brushes for the colors.
For a circle you can do the following:
System.Windows.Shapes.Ellipse circle = new System.Windows.Shapes.Ellipse();
circle.Height = 20; //or some size
circle.Width = 20; //height and width is the same for a circle
circle.Fill = System.Windows.Media.Brushes.Red;
Then you can make a function to do your check for red and green.
Also, you can use hex values for the colors as well:
circle.Fill = new System.Windows.Media.SolidColorBrush((Color)ColorConverter.ConvertFromString("#RRGGBB"));
Not exactly related to the question at hand, but your code could be shortened somewhat using the ternary operator as such:
btnIISIndicator.BackColor = allGood ? Color.Green : Color.Red;
But that all depends on your (or your organization's) definition of readability and maintainability.
I would just make a panel or PictureBox and set the Background image to that of a red/green light. Either make the images in PhotoShop/PaintShop/MS Paint or download some stock images off the web.
Whenever the status changes, just swap the image out.
just try this it works for me.
SolidColorBrush solidColor=new SolidColorBrush();
solidColor.Color=Colors.Red;
ellips_circle.Fill=solidColor;
Use an image, but theres some great icons available here so you dont have to actually make some.
Create red and green bitmaps and use the PictureBox control to show the bitmaps.
I just use some standard images and put them in a picturebox. works great on our apps.
I simply used a non enabled button as indicator since I did not manage to install the WinUI for shapes. Same suggestion as question but simplified.
indicatorButton.Enabled = false;
...
if (allGood)
{
indicatorButton.BackColor = Color.Green;
indicatorButton.Text = "On";
}
else
{
indicatorButton.BackColor = Color.Red;
indicatorButton.Text = "Off";
}