I know how to create a fadeIn/Out winform.
The problem is, I don't know how to create a Custom Control which will lose its full transparency when mouse leaves its area and gets transparent when mouse gets back on it.
A Custom Control doesn't have Opacity and TransparentKey properties.
This tutorial explains how to add Opacity property to a User Control.
public UserControl1()
{
InitializeComponent();
// Enables support for transparent back color for inner controls.
SetStyle(ControlStyles.SupportsTransparentBackColor, true);
}
// This enables transparent backgrounds for the control.
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x20;
return cp;
}
}
// A property to adjust the opacity level.
private int _opacity = 255;
[Description("Set opacity of control. 0 to 255")]
public int Opacity
{
get
{
return this._opacity;
}
set
{
this._opacity = value;
this.Refresh();
}
}
// Override default OnPaintBackground event.
protected override void OnPaintBackground(PaintEventArgs e)
{
e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(this.Opacity, this.BackColor)), this.ClientRectangle);
}
But it doesn't work on my Custom Control or I just don't know how to use it.
By FadeIn/Out, I mean a control which gets transparent when mouse leave it until user points back to it
Related
In the code below, when the Form is resized using the left bar, the Button appears to be pulled away from the right edge of the window, even though the RTL flag is set. Is there a way to keep the button from visually bouncing when the window is resized from the left?
For example, if the RTL flag line is commented out, and the Form is resized from the right bar, then the button is visually stable, always appearing exactly against the leftmost border.
Tested using:
Windows 8.1 Pro with Media Center (Version 6.3 build 9600) for .NET 4.5.2 and .NET 4.7.2
Windows Server 2016 (versions to be determined).
Animated gif:
public class MyForm : Form {
private const int WS_EX_LAYOUTRTL = 0x00400000;
LE le = new LE();
public Button btn = new Button { Text = "Button" };
public MyForm() : base() {
Controls.Add(btn);
}
protected override CreateParams CreateParams {
get {
CreateParams cp = base.CreateParams;
cp.ExStyle |= WS_EX_LAYOUTRTL; // comment out this line and resize from the right, the button stays still
return cp;
}
}
public override LayoutEngine LayoutEngine {
get {
return le;
}
}
// empty layout engine
private class LE : LayoutEngine {
public override void InitLayout(object child, BoundsSpecified specified) {}
public override bool Layout(object container, LayoutEventArgs layoutEventArgs) {
return false;
}
}
}
So I'm coding a project that is Plants vs. Zombies made of pure c# using no game engines and here I have a graphic problem.
I need to render a transparent picturebox over another transparent picturebox and I had to define a new control that is really transparent and everything goes fine with the transparency aspect but here is a problem:
Flicker :|
I have so much of them because of the RecreateHandle(); method I use when I change the image of my control to make animations and when it moves to have the real transparency.
Here is my code I wonder if any one could help !
public class TransparentControl : Control
{
private Image _image;
public TransparentControl()
{
SetStyle(ControlStyles.AllPaintingInWmPaint |
ControlStyles.SupportsTransparentBackColor, true);
base.BackColor = Color.FromArgb(0, 0, 0, 0);
}
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x20;
return cp;
}
}
protected override void OnMove(EventArgs e)
{
//RecreateHandle();
}
protected override void OnPaint(PaintEventArgs e)
{
if (_image != null)
{
e.Graphics.DrawImage(_image, (Width / 2) - (_image.Width / 2), (Height / 2) - (_image.Height / 2));
}
}
protected override void OnPaintBackground(PaintEventArgs e)
{
//Do not paint background
}
//Hack
public void Redraw()
{
//RecreateHandle();
}
public Image Image
{
get
{
return _image;
}
set
{
_image = value;
//RecreateHandle();
}
}
}
One way to reduce flickering is to create two different bitmaps. One to draw to, and one to display.
Image BackBuffer;
Image BrontBuffer;
private void RotateImages()
{
lock (this.BackBuffer)
{
var temp = this.BackBuffer;
this.BackBuffer = this.FrontBuffer;
this.FrontBuffer = temp;
}
}
Draw everything into your BackBuffer, then show the FrontBuffer.
Note that you should declare the FrontBuffer with the same width/height values as the BackBuffer, in pretty much exactly the same spot that you declare the BackBuffer.
Use the method RotateImages() immediately after (before should work, too) you display the front buffer.
In Win32 API the BS_GROUPBOX just creates a 'button' that is basically painted as a group, it doesn't serve as a container for any other control.
In WinForms, this concept was changed and GroupBox is actually a container.
I'm porting an old Win32 app to WinForms where I generate at run-time dialog forms by parsing a Win32 API dialog definition string. Everything works fine, except these groupBoxes, for which I cannot find an alternative in .NET world.
How would I get a control ala Win32 GroupBox that doesn't take any ownership as the .NET GroupBox?
Thx
After all I solved the problem by creating a Transparent GroupBox in .NET. Here is the code.
public sealed class IGTransparentGroupBox : GroupBox
{
public IGTransparentGroupBox()
{
SetStyle(ControlStyles.SupportsTransparentBackColor, true);
SetStyle(ControlStyles.Opaque, true);
BackColor = Color.Transparent;
}
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ExStyle = cp.ExStyle | 0x20;
return cp;
}
}
protected override void OnBackColorChanged(EventArgs e)
{
Parent?.Invalidate(Bounds, true);
base.OnBackColorChanged(e);
}
protected override void OnParentBackColorChanged(EventArgs e)
{
Invalidate();
base.OnParentBackColorChanged(e);
}
}
C# .Net 4.5
I have a ListView that is constantly being updated by different threads through this method:
public void UpdateUI_List_SetRow(int rowNum, ListViewItem item)
{
lock (lock_List)
{
try
{
if (this.lstStatus.InvokeRequired)
lstStatus.Invoke(new MethodInvoker(() => lstStatus.Items[rowNum] = item));
else
lstStatus.Items[rowNum] = item;
}
catch (Exception ex)
{
ErrorLogging.Log(ex);
}
}
}
It constantly flickers while being updated. I have tried the DoubleBuffered fix (How to prevent flickering in ListView when updating a single ListViewItem's text?) but it didn't work.
Sorry if this is a duplicate post but I couldn't find any solution other than enabling double buffering.
Subclass ListView to enable double buffering:
namespace System.Windows.Forms
{
public class ListViewEx : ListView
{
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x02000000; // Turn on WS_EX_COMPOSITED
return cp;
}
}
}
}
Code provided in accepted answer works, but ruins selection rectangle. The solution below works like a charm. It enables double buffering and retains correct rectangle.
public partial class myListView : ListView
{
public myListView()
{
SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint, true);
}
}
How to use it - add this code to your namespace and use myListView instead of conventional ListView.
In my WinForm Application, I am needing to layer some images. However, I'm having trouble getting a transparent control to place the image in. I have done some research and came up with the following class:
public class TransparentPicture : PictureBox
{
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x20;
return cp;
}
}
protected override void OnPaintBackground(PaintEventArgs e)
{
// do nothing
}
protected override void OnMove(EventArgs e)
{
RecreateHandle();
}
}
This seems to work fine until I close Visual Studios and reopen the Solution. Then my controls all disappear in the designer. They show when I run the program, but I need them to show in designer too where I can continue to design my application.
I know this isn't everything I need to do, because these controls are always causing my program to freeze up for a few seconds and stuff.
So my question is..does anybody know where I can find code for a transparent control, or how to fix the one I've thrown together?
Make the TransparentPicture be a regular PictureBox, until an IsTransparent property is set to true.
Set the property to false on design time, and to true in FormLoad event (which will only happen when you actually run the application).
That way, on design time, they will behave as regular picture boxes, but when you run the application, they will become transparent.
public class TransparentPicture : PictureBox
{
public bool IsTransparent { get; set; }
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
if (this.IsTransparent)
{
cp.ExStyle |= 0x20;
}
return cp;
}
}
protected override void OnPaintBackground(PaintEventArgs e)
{
if (!this.IsTransparent)
{
base.OnPaintBackground(e);
}
}
protected override void OnMove(EventArgs e)
{
if (this.IsTransparent)
{
RecreateHandle();
}
else
{
base.OnMove(e);
}
}
}
Then, on your FormLoad event, you should do:
for (var i = 0; i < this.Controls.Count; i++)
{
var tp = this.Controls[i] as TransparentPicture;
if (tp != null)
{
tp.IsTransparent = true;
}
}
Or if you only have a few:
tp1.IsTransparent = tp2.IsTransparent = tp3.IsTransparent = true;