Is it possible to change the background color of a VscrollBar? The best approach I have consulted is http://www.codeproject.com/Articles/624997/Enhanced-Scrollbar or http://www.codeproject.com/Articles/41869/Custom-Drawn-Scrollbar, but no exists BackColor property, and BackgroundImage not works.
I tried the simplest thing like:
public class ScrollBarEx : VScrollBar
{
protected override void OnPaint(PaintEventArgs e)
{
base.BackColor = Color.Red;
base.Invalidate();
}
}
It does not work. Any suggestions?
I built my own custom ScrollBar control based on http://www.codeproject.com/Articles/41869/Custom-Drawn-Scrollbar.
My custom ScrollBar comes with ready-to-use themes (VS 2019 Dark and VS 2019 Light). You can change the control background color by modifying the field _backColor or you can expose this private field with a property to allow you to change the background color from the code.
I have modified the original control to get rid of the 3D colors and replaced them with flat colors
Complete Flat ScrollBar control code in VB.NET
https://gist.github.com/ahmedosama007/c1b0cd327d395a5698c1e17e96d0f8f9
Not much new info, but: you have to play around with Paint event and do custom drawing of your scrollbar, here is another link with this solution (custom drawing through Paint event) http://www.codeproject.com/Articles/14801/How-to-skin-scrollbars-for-Panels-in-C
Related
I have a transparent form that overlays the desktop in a c# .NET winforms application. The transparency is done by setting the BackColor to bright orange and then setting the TransparencyKey to the same bright orange.
So far this works great and it creates a transparent form. I then want to create a control on the transparent form that draws a rectangle around items on the desktop. So essentially you can think of a transparent button with a rectangular border. To make this happen I currently extend Control and setup the control like this inside of the parent form which is also transparent:
public class CustomControl : Control
{
public CustomControl(Size size, Point point)
{
SetStyle(ControlStyles.SupportsTransparentBackColor, true);
this.BackColor = Color.Transparent;
this.Size = size;
this.Location = point;
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Pen pen = new Pen(Color.Red, 2f);
e.Graphics.DrawRectangle(pen, this.ClientRectangle);
}
}
This creates the exact effect I'm looking for in that there is a control with a solid opaque rectangle border inside a transparent form and you can see the desktop through the control and form (see image below on left side).
The problem is that the transparent area of the control does not receive any mouse events. In fact, it basically doesn't exist. For example, when your mouse goes over the red rectangular border of the control that triggers the Mouse Hover, Mouse Enter, and Mouse Leave events and changes the cursor to whatever you set as this.Cursor for the control. Once, the mouse is in the transparent portion of the control none of these things trigger.
How can I keep things the way they are now in regards to the look of the control but still receive mouse events in the transparent area. In particular I want to receive Mouse Hover and have the controls Cursor value be honored. Note that if I make the BackColor of the control anything other than Color.Transparent then mouse events work fine.
Thank you!
Update
Based on Hans comment I would like to add that we have implemented the above and that it has usually worked (i.e., the transparent area does respond to mouse events). The reason this issue came up is because I recently rebuilt my machine with all the Windows 8.1 updates and latest ATI graphic drivers and after that the above setup no longer works ever (transparent areas on the control no longer receive any mouse events and for all intents and purposes are not part of the control) On my colleague's machine it almost always works although we've occasionally noticed that it won't work but we could never consistently reproduce the issue.
My assumption was that we did something wrong to cause the transparent areas to not respond to mouse events. However, based on Hans' comment, it seems as though the above code should never work and that the only reason it worked was because of a bug in Aero.
The exact color of our transparency key is rgb(255, 128, 0). In addition we did notice that any label controls placed on the transparent form look terrible (per Han's comment).
Update 2
Based on Han's additional comment regarding the Aero transparency bug I have the following updated questions.
Is there any information on this bug somewhere that explains what the bug is?
What is the intended behavior (assume there are no bugs) for transparent areas of a control? Should mouse events (e.g., Mouse Hover) work on the transparent area?
The Final Answer (that usually works)
The answered provided by Reza below did work for me on some of my computers. However, my primary desktop continued to stubbornly refuse to cooperate. Even when copying the exact project between computers using the same version of windows and .NET the problem existed. The issue when it arose was that transparent areas would not trigger mouse events and were not treated as part of the control.
In addition, I noticed the same thing Reza noticed which was that certain TransparencyKey colors flat out did not work. Although I don't know any details of the bug I would have to agree with Hans that there are bugs involved with transparency on WinForms and if anyone is starting from scratch I would go the WPF route and save yourself any possible future headaches.
In the end we ended up implementing a work around based on some of Hans' answers that required using a timer to check the mouse location and nesting two forms (one with opacity set) to be able to manage the mouse cursor. This solution has worked on all our systems and will hopefully hold up until we move over to WPF. I accepted Reza's answer because it seems to work in most places but be aware that it might not work you and there is no rhyme or reason as to why.
For detailed information on the two workaround we implemented please see the following questions and answers by Hans.
MouseHover and MouseLeave Events controlling
How can I add transparency to a c# form while keeping controls visible?
Important
Consider set BackgroundColor of form to Red and TransparencyKey to Red and BackGroundColor of transparent control to Transparent and it will work!
the strange thing that I see is, the approach doesn't work with for example Magenta but works with Red and Blue.
I think you should create your transparent control this way:
Transparent Control Code
public class TransparentControl : Control
{
public TransparentControl()
{
this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
}
private const int WS_EX_TRANSPARENT = 0x20;
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ExStyle = cp.ExStyle | WS_EX_TRANSPARENT;
return cp;
}
}
}
And for the mouse events and rendering border when the mouse enters, here is the sample I did Using .Net 4.5 On Windows 8.1:
Create a Form and put a TransparentControl that we created using above code, on it, then handle MouseEnter and MouseLeave and Paint events and paint the border when mouse is in control bouds and handle Click event and show a message.
Form Code
private bool drawBorder;
private void transparentControl1_MouseLeave(object sender, EventArgs e)
{
drawBorder = false;
transparentControl1.Invalidate();
}
private void transparentControl1_MouseEnter(object sender, EventArgs e)
{
drawBorder = true;
transparentControl1.Invalidate();
}
private void transparentControl1_Paint(object sender, PaintEventArgs e)
{
if(drawBorder)
{
using (var pen = new Pen(this.ForeColor, 5))
{
e.Graphics.DrawRectangle(pen, 0, 0, this.transparentControl1.Width - 1, this.transparentControl1.Height - 1);
}
}
}
private void transparentControl1_Click(object sender, EventArgs e)
{
MessageBox.Show("Clicked");
}
Screenshot
The mouse cursor is in the control's area so the black border has been painted.
Important Note
If you draw border the same color as form's transparency key, the border will not be shown.
I think it's okay for the mouse cursor to go in the middle of that overlay box and move with the mouse cursor and the overlay box to make it look like it's a mouse cursor.
Like this picture
I have had similar problem when I use a picturebox control on a transparent form and use the mouseclick event over the control to trigger code. Sometimes the mouseclick is captured and sometimes it isn't. No pattern to it other than the color used as the form background. Red works fine but many other colors do not. Even black does not work consistently. I have found an almost black color that works fine when form.backcolor and form.transparencykey are set to this color. Based on this and others experiences it appears a bug in VB Studio and the way transparency is handled.
Form1.BackColor = Color.FromArgb(64, 0, 0) ' a color that works with transparency and allows picturebox to be clicked
I want to create a colored border around a TextBox on a Windows Form, something like what can be done in Android (with the "ID" and "PackSize" EditText widgets):
Is this possible (without writing a gazillion LOC and sacrificing a chicken)?
There is a BorderStyle property, but that does not change the color of the border. The ForeColor and BackColor properties also do nothing helpful.
You could either:
Put the textbox in a slightly larger Panel with a dark BackColor or
Handle the Paint event and draw the border yourself.
I am trying to dynamically change the BackColor of a RichTextBox to control however I cannot find this colour in code view however I can locate it in designer mode using the properties panel.
I have used the following code to change BackColour to red, but cant use the same method for accessing the control color;
tbx_description.BackColor = Color.Red;
Does anyone know how I can use the control color as a BackColor?
You can use the SystemColors class to access special colours.
tbx_description.BackColor = SystemColors.Control;
How I make a background transparent on my form? Is it possible in C#?
Thanks in advance!
You can set the BackColor of your form to an uncommon color (say Color.Magenta) then set the form's TransparencyKey property to the same color. Then, set the FormBorderStyle to None.
Of course, that's just the quick and easy solution. The edges of controls are ugly, you have to keep changing the background color of new controls you add (if they're Buttons or something like that) and a whole host of other problems.
It really depends what you want to achieve. What is it? If you want to make a widget sort of thing, there are much better ways. If you need rounded corners or a custom background, there are much better ways. So please provide some more information if TransparencyKey isn't quite what you had in mind.
Put the following in the constructor of the form:
public Form1()
{
this.TransparencyKey = Color.Turquoise;
this.BackColor = Color.Turquoise;
}
Note: This method prevents you from clicking through the form.
Update:
How to: Give Your Control a Transparent Background
Deprecated:
How to: Create Transparent Windows Forms:
Note: As transparent forms are only supported in Windows 2000 or
later, Windows Forms will be
completely opaque when run on older
operating systems, such as Windows 98,
regardless of the value set for the
Opacity property.
A simple solution to get a transparent background in a winform is to overwrite the OnPaintBackground method like this:
protected override void OnPaintBackground(PaintEventArgs e)
{
//empty implementation
}
(Notice that the base.OnpaintBackground(e) is removed from the function)
Is there any way to make a panel transparent or semi transparent? I haven't found any appropriate property to set transparency for a panel.
I was also trying to make a WPF control with grid (grid background and control background was set to transparent) and place it on normal windows form, but when I put this control on normal Windows Form(not WPF) I don't get the proper transparency.
If your display is pretty much static, you can do this to achieve semi-transparency (Source):
class SeeThroughPanel : Panel
{
public SeeThroughPanel()
{
}
protected override CreateParams CreateParams {
get {
var cp = base.CreateParams;
cp.ExStyle |= 0x00000020;
return cp;
}
}
protected override void OnPaint(PaintEventArgs e)
{
//base.OnPaint(e);
e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(50, 0, 0, 0)), this.ClientRectangle);
}
}
But, this approach is not without problems if you need dynamic rendering on your semi-transparent control. See this question which I have posted. Hope it gets answered someday.
Unfortunately, transparency is not natively or well supported in WinForms and is difficult to implement yourself.
If you want a transparent panel that allows you to "see through" the form, take a look at this article, which tells you to set both the Panel's TransparencyKey and BackColor to something like Fuscia. With text/other stuff in the panel this effect may not look so good.
If you want a transparent or translucent panel that shows the form below take a look at this SO question.
I would use WPF all the way, if you are able to use it in a form now.
I'm not sure about making the panel semi-transparent, but I know you can use the TransparencyKey property of the form to create completely transparent sections.
For example, set the form's TransparencyKey property to Fuchsia, then set the panel's BackColor to Fuchsia, and it will create a transparent area on the form.
Edit:
Agree with #Callum Rogers about going with WPF. Text directly on the panel shows fringing (see the label in the screenshot). However, if you need to add this quickly to an existing Win Forms app and don't need to show text directly on the panel, it could work for you.