Transparent UserControl Flickers when form get resized.

G

Guest

Hi,

1) I have created one windows application, In the main form ( form1) i have added one usercontrol (usercontrol1), In that user control i am drawing one image.
2) In the UserControl1 i am showing one transparent form (form3) when ever user preseed left mouse button.
3) The form3 has one transparent user control (usercontrol2) that paints circles. That measn the circles will show on top the usercontrol1 image.
4) The form3 border style is none. So to resize the form, programaticlly i am resizing the form.
5) I have created one PictureBox (picBox) right bottom of the form3. Using this picture box user can resize the form3.
6) When resizing the form3, the controls on the form3 flikkers.

Can you please tell me what could be the problem. I have listed the full code, can you please see my code and give me solution.

This iam struggling for three foure days.. please reply the solution.

Form1.cs
-----------
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace transparant
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
UserControl1 uc;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//

uc = new UserControl1();
uc.Location = this.Location;
uc.Size = this.Size;
this.Controls.Add(uc);

this.SizeChanged += new EventHandler(OnSizeChanged);
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Name = "Form1";
this.Text = "Form1";
this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void OnSizeChanged(object sender, EventArgs args)
{
uc.Size = this.Size;
}
}
}

UserControl1.cs
------------------
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace transparant
{
public class UserControl1 : System.Windows.Forms.UserControl
{
private System.ComponentModel.Container components = null;
private Form3 frm;
public UserControl1()
{
// This call is required by the Windows.Forms Form Designer.
InitializeComponent();

// TODO: Add any initialization after the InitForm call

frm = new Form3();
frm.Location = new Point(200,200);
frm.Size = new Size(400,400);
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
//
// UserControl1
//
this.Name = "UserControl1";
this.Size = new System.Drawing.Size(568, 360);
this.Click += new System.EventHandler(this.UserControl1_Click);

}
#endregion

protected override void OnPaint(PaintEventArgs e)
{
//Image you can draw any bmp image.
Bitmap bmp = new Bitmap("C:\\medical.bmp");
e.Graphics.DrawImage(bmp, 0, 0, this.Width, this.Height);
}
private void UserControl1_Click(object sender, System.EventArgs e)
{
frm.Show();
}

}

Form3.cs
----------
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace transparant
{
/// <summary>
/// Summary description for Form3.
/// </summary>
public class Form3 : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

private PictureBox picBox;
private bool bDrag = false;
private Point prevPoint;
private Chart chart;
public Form3()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

this.FormBorderStyle = FormBorderStyle.None;
this.SizeChanged += new EventHandler(OnSizeChanged);
CreatePicBox();

CreateChart();

this.BackColor = Color.Black;
this.TransparencyKey = Color.Black;
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.Size = new System.Drawing.Size(300,300);
this.Text = "Form3";
}
#endregion

private void CreatePicBox()
{
picBox = new PictureBox();
picBox.Size = new Size(10,10);
picBox.BackColor = Color.Red;
picBox.MouseDown += new MouseEventHandler(OnMouseDown);
picBox.MouseMove += new MouseEventHandler(OnMouseMove);
picBox.MouseUp += new MouseEventHandler(OnMouseUp);
picBox.Cursor = Cursors.SizeNWSE;
this.Controls.Add(picBox);
}

private void OnMouseDown(object sender, MouseEventArgs args)
{
picBox.Capture = true;
bDrag = true;
prevPoint = new Point(args.X, args.Y);

}

private void OnMouseMove(object sender, MouseEventArgs args)
{
Point pt = new Point(args.X, args.Y);

if (bDrag)
{
this.Cursor = Cursors.SizeNWSE;
int width = this.Size.Width;
int height = this.Size.Height;
width += (args.X - prevPoint.X);
height += (args.Y - prevPoint.Y);
this.Size = new Size(width, height);

this.Refresh();
}

}

private void OnMouseUp(object sender, MouseEventArgs args)
{
bDrag = false;
picBox.Capture = false;

}

private void OnSizeChanged(object sender, EventArgs args)
{
picBox.Location = new Point(Size.Width - 10, Size.Height - 10);
uc2.Size = new Size(Size.Width, Size.Height - 50);
}

UserControl uc2;
private void CreateChart()
{
uc2 = new UserControl2();
uc2.Location = this.Location;
Controls.Add(uc2);
}

}
}

USerControl2.cs
-------------------
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

namespace transparant
{

/// <summary>
/// Summary description for UserControl2.
/// </summary>
public class UserControl2 : System.Windows.Forms.UserControl
{


/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Image img;

public UserControl2()
{
// This call is required by the Windows.Forms Form Designer.
InitializeComponent();


// TODO: Add any initialization after the InitForm call

}


/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
//
// UserControl2
//
this.Name = "UserControl2";
this.Size = new System.Drawing.Size(440, 320);

}
#endregion


protected override void OnPaint(PaintEventArgs e)
{
int x = 210;
int y = 160;
int width = 30;
int height = 30;

Pen pen = new Pen(Color.Yellow, 2);

//Brush brush = new SolidBrush(Color.FromArgb(60, Color.Red));
//e.Graphics.FillRectangle(brush, 0, 0, this.Width, this.Height);

for(int i = 0; i < 5; i++)
{
e.Graphics.DrawEllipse(pen, x, y, width, height);
x -= 30;
y -= 30;
width += 60;
height += 60;
}
}

}
}
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Hi Jaikumar,

The problem is that the control erases itself upon receiving WM_ERASEBKGND.
To reduce flickering add the following line ot usercontrol2 constructor

SetStyle(ControlStyles.AllPaintingInWmPaint, true);

However you can notice some flicks.

For best result turn the doublebuffering on

SetStyle(ControlStyles.DoubleBuffer|ControlStyles.UserPaint|ControlStyles.Al
lPaintingInWmPaint, true);


--
HTH
Stoitcho Goutsev (100) [C# MVP]


Jaikumar said:
Hi,

1) I have created one windows application, In the main form ( form1) i
have added one usercontrol (usercontrol1), In that user control i am drawing
one image.
2) In the UserControl1 i am showing one transparent form (form3) when ever
user preseed left mouse button.
3) The form3 has one transparent user control (usercontrol2) that paints
circles. That measn the circles will show on top the usercontrol1 image.
4) The form3 border style is none. So to resize the form, programaticlly i am resizing the form.
5) I have created one PictureBox (picBox) right bottom of the form3. Using
this picture box user can resize the form3.
6) When resizing the form3, the controls on the form3 flikkers.

Can you please tell me what could be the problem. I have listed the full
code, can you please see my code and give me solution.
This iam struggling for three foure days.. please reply the solution.

Form1.cs
-----------
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace transparant
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
UserControl1 uc;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//

uc = new UserControl1();
uc.Location = this.Location;
uc.Size = this.Size;
this.Controls.Add(uc);

this.SizeChanged += new EventHandler(OnSizeChanged);
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Name = "Form1";
this.Text = "Form1";
this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void OnSizeChanged(object sender, EventArgs args)
{
uc.Size = this.Size;
}
}
}

UserControl1.cs
------------------
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace transparant
{
public class UserControl1 : System.Windows.Forms.UserControl
{
private System.ComponentModel.Container components = null;
private Form3 frm;
public UserControl1()
{
// This call is required by the Windows.Forms Form Designer.
InitializeComponent();

// TODO: Add any initialization after the InitForm call

frm = new Form3();
frm.Location = new Point(200,200);
frm.Size = new Size(400,400);
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
//
// UserControl1
//
this.Name = "UserControl1";
this.Size = new System.Drawing.Size(568, 360);
this.Click += new System.EventHandler(this.UserControl1_Click);

}
#endregion

protected override void OnPaint(PaintEventArgs e)
{
//Image you can draw any bmp image.
Bitmap bmp = new Bitmap("C:\\medical.bmp");
e.Graphics.DrawImage(bmp, 0, 0, this.Width, this.Height);
}
private void UserControl1_Click(object sender, System.EventArgs e)
{
frm.Show();
}

}

Form3.cs
----------
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace transparant
{
/// <summary>
/// Summary description for Form3.
/// </summary>
public class Form3 : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

private PictureBox picBox;
private bool bDrag = false;
private Point prevPoint;
private Chart chart;
public Form3()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

this.FormBorderStyle = FormBorderStyle.None;
this.SizeChanged += new EventHandler(OnSizeChanged);
CreatePicBox();

CreateChart();

this.BackColor = Color.Black;
this.TransparencyKey = Color.Black;
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.Size = new System.Drawing.Size(300,300);
this.Text = "Form3";
}
#endregion

private void CreatePicBox()
{
picBox = new PictureBox();
picBox.Size = new Size(10,10);
picBox.BackColor = Color.Red;
picBox.MouseDown += new MouseEventHandler(OnMouseDown);
picBox.MouseMove += new MouseEventHandler(OnMouseMove);
picBox.MouseUp += new MouseEventHandler(OnMouseUp);
picBox.Cursor = Cursors.SizeNWSE;
this.Controls.Add(picBox);
}

private void OnMouseDown(object sender, MouseEventArgs args)
{
picBox.Capture = true;
bDrag = true;
prevPoint = new Point(args.X, args.Y);

}

private void OnMouseMove(object sender, MouseEventArgs args)
{
Point pt = new Point(args.X, args.Y);

if (bDrag)
{
this.Cursor = Cursors.SizeNWSE;
int width = this.Size.Width;
int height = this.Size.Height;
width += (args.X - prevPoint.X);
height += (args.Y - prevPoint.Y);
this.Size = new Size(width, height);

this.Refresh();
}

}

private void OnMouseUp(object sender, MouseEventArgs args)
{
bDrag = false;
picBox.Capture = false;

}

private void OnSizeChanged(object sender, EventArgs args)
{
picBox.Location = new Point(Size.Width - 10, Size.Height - 10);
uc2.Size = new Size(Size.Width, Size.Height - 50);
}

UserControl uc2;
private void CreateChart()
{
uc2 = new UserControl2();
uc2.Location = this.Location;
Controls.Add(uc2);
}

}
}

USerControl2.cs
-------------------
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

namespace transparant
{

/// <summary>
/// Summary description for UserControl2.
/// </summary>
public class UserControl2 : System.Windows.Forms.UserControl
{


/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Image img;

public UserControl2()
{
// This call is required by the Windows.Forms Form Designer.
InitializeComponent();


// TODO: Add any initialization after the InitForm call

}


/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
//
// UserControl2
//
this.Name = "UserControl2";
this.Size = new System.Drawing.Size(440, 320);

}
#endregion


protected override void OnPaint(PaintEventArgs e)
{
int x = 210;
int y = 160;
int width = 30;
int height = 30;

Pen pen = new Pen(Color.Yellow, 2);

//Brush brush = new SolidBrush(Color.FromArgb(60, Color.Red));
//e.Graphics.FillRectangle(brush, 0, 0, this.Width, this.Height);

for(int i = 0; i < 5; i++)
{
e.Graphics.DrawEllipse(pen, x, y, width, height);
x -= 30;
y -= 30;
width += 60;
height += 60;
}
}

}
}
 
G

Guest

Hi,

Like you said, i did SetStyle on userControl2. Now what happens is, the transparent effect gone, it is showing black background instead of transparent image which is on user control1.

Then while i reduce the form size, some gray background is coming.

Stoitcho Goutsev (100) said:
Hi Jaikumar,

The problem is that the control erases itself upon receiving WM_ERASEBKGND.
To reduce flickering add the following line ot usercontrol2 constructor

SetStyle(ControlStyles.AllPaintingInWmPaint, true);

However you can notice some flicks.

For best result turn the doublebuffering on

SetStyle(ControlStyles.DoubleBuffer|ControlStyles.UserPaint|ControlStyles.Al
lPaintingInWmPaint, true);


--
HTH
Stoitcho Goutsev (100) [C# MVP]


Jaikumar said:
Hi,

1) I have created one windows application, In the main form ( form1) i
have added one usercontrol (usercontrol1), In that user control i am drawing
one image.
2) In the UserControl1 i am showing one transparent form (form3) when ever
user preseed left mouse button.
3) The form3 has one transparent user control (usercontrol2) that paints
circles. That measn the circles will show on top the usercontrol1 image.
4) The form3 border style is none. So to resize the form, programaticlly i am resizing the form.
5) I have created one PictureBox (picBox) right bottom of the form3. Using
this picture box user can resize the form3.
6) When resizing the form3, the controls on the form3 flikkers.

Can you please tell me what could be the problem. I have listed the full
code, can you please see my code and give me solution.
This iam struggling for three foure days.. please reply the solution.

Form1.cs
-----------
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace transparant
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
UserControl1 uc;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//

uc = new UserControl1();
uc.Location = this.Location;
uc.Size = this.Size;
this.Controls.Add(uc);

this.SizeChanged += new EventHandler(OnSizeChanged);
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Name = "Form1";
this.Text = "Form1";
this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void OnSizeChanged(object sender, EventArgs args)
{
uc.Size = this.Size;
}
}
}

UserControl1.cs
------------------
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace transparant
{
public class UserControl1 : System.Windows.Forms.UserControl
{
private System.ComponentModel.Container components = null;
private Form3 frm;
public UserControl1()
{
// This call is required by the Windows.Forms Form Designer.
InitializeComponent();

// TODO: Add any initialization after the InitForm call

frm = new Form3();
frm.Location = new Point(200,200);
frm.Size = new Size(400,400);
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
//
// UserControl1
//
this.Name = "UserControl1";
this.Size = new System.Drawing.Size(568, 360);
this.Click += new System.EventHandler(this.UserControl1_Click);

}
#endregion

protected override void OnPaint(PaintEventArgs e)
{
//Image you can draw any bmp image.
Bitmap bmp = new Bitmap("C:\\medical.bmp");
e.Graphics.DrawImage(bmp, 0, 0, this.Width, this.Height);
}
private void UserControl1_Click(object sender, System.EventArgs e)
{
frm.Show();
}

}

Form3.cs
----------
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace transparant
{
/// <summary>
/// Summary description for Form3.
/// </summary>
public class Form3 : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

private PictureBox picBox;
private bool bDrag = false;
private Point prevPoint;
private Chart chart;
public Form3()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

this.FormBorderStyle = FormBorderStyle.None;
this.SizeChanged += new EventHandler(OnSizeChanged);
CreatePicBox();

CreateChart();

this.BackColor = Color.Black;
this.TransparencyKey = Color.Black;
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.Size = new System.Drawing.Size(300,300);
this.Text = "Form3";
}
#endregion

private void CreatePicBox()
{
picBox = new PictureBox();
picBox.Size = new Size(10,10);
picBox.BackColor = Color.Red;
picBox.MouseDown += new MouseEventHandler(OnMouseDown);
picBox.MouseMove += new MouseEventHandler(OnMouseMove);
picBox.MouseUp += new MouseEventHandler(OnMouseUp);
picBox.Cursor = Cursors.SizeNWSE;
this.Controls.Add(picBox);
}

private void OnMouseDown(object sender, MouseEventArgs args)
{
picBox.Capture = true;
bDrag = true;
prevPoint = new Point(args.X, args.Y);

}

private void OnMouseMove(object sender, MouseEventArgs args)
{
Point pt = new Point(args.X, args.Y);

if (bDrag)
{
this.Cursor = Cursors.SizeNWSE;
int width = this.Size.Width;
int height = this.Size.Height;
width += (args.X - prevPoint.X);
height += (args.Y - prevPoint.Y);
this.Size = new Size(width, height);

this.Refresh();
}

}

private void OnMouseUp(object sender, MouseEventArgs args)
{
bDrag = false;
picBox.Capture = false;

}

private void OnSizeChanged(object sender, EventArgs args)
{
picBox.Location = new Point(Size.Width - 10, Size.Height - 10);
uc2.Size = new Size(Size.Width, Size.Height - 50);
}

UserControl uc2;
private void CreateChart()
{
uc2 = new UserControl2();
uc2.Location = this.Location;
Controls.Add(uc2);
}

}
}

USerControl2.cs
-------------------
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

namespace transparant
{

/// <summary>
/// Summary description for UserControl2.
/// </summary>
public class UserControl2 : System.Windows.Forms.UserControl
{


/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Image img;

public UserControl2()
{
// This call is required by the Windows.Forms Form Designer.
InitializeComponent();


// TODO: Add any initialization after the InitForm call

}


/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
//
// UserControl2
//
this.Name = "UserControl2";
this.Size = new System.Drawing.Size(440, 320);

}
#endregion


protected override void OnPaint(PaintEventArgs e)
{
int x = 210;
int y = 160;
int width = 30;
int height = 30;

Pen pen = new Pen(Color.Yellow, 2);

//Brush brush = new SolidBrush(Color.FromArgb(60, Color.Red));
//e.Graphics.FillRectangle(brush, 0, 0, this.Width, this.Height);

for(int i = 0; i < 5; i++)
{
e.Graphics.DrawEllipse(pen, x, y, width, height);
x -= 30;
y -= 30;
width += 60;
height += 60;
}
}

}
}
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Jaikumar,

I didn't observe such an black-background effect. However there is some
delay of repainting the usercontrol1's background, but eventually it always
update it correctly. That delay disappeared completely when I turned on
usercontrol1 double buffering.

Add in the usercontrol1's constructor following line
SetStyle(ControlStyles.DoubleBuffer | ControlStyles.AllPaintingInWmPaint |
ControlStyles.UserPaint, true);


--
HTH
Stoitcho Goutsev (100) [C# MVP]


Jaikumar said:
Hi,

Like you said, i did SetStyle on userControl2. Now what happens is, the
transparent effect gone, it is showing black background instead of
transparent image which is on user control1.
Then while i reduce the form size, some gray background is coming.

Stoitcho Goutsev (100) said:
Hi Jaikumar,

The problem is that the control erases itself upon receiving WM_ERASEBKGND.
To reduce flickering add the following line ot usercontrol2 constructor

SetStyle(ControlStyles.AllPaintingInWmPaint, true);

However you can notice some flicks.

For best result turn the doublebuffering on

SetStyle(ControlStyles.DoubleBuffer|ControlStyles.UserPaint|ControlStyles.Al
lPaintingInWmPaint, true);


--
HTH
Stoitcho Goutsev (100) [C# MVP]


Jaikumar said:
Hi,

1) I have created one windows application, In the main form ( form1) i
have added one usercontrol (usercontrol1), In that user control i am drawing
one image.
2) In the UserControl1 i am showing one transparent form (form3) when
ever
user preseed left mouse button.
3) The form3 has one transparent user control (usercontrol2) that
paints
circles. That measn the circles will show on top the usercontrol1 image.
4) The form3 border style is none. So to resize the form,
programaticlly i
am resizing the form.
5) I have created one PictureBox (picBox) right bottom of the form3.
Using
this picture box user can resize the form3.
6) When resizing the form3, the controls on the form3 flikkers.

Can you please tell me what could be the problem. I have listed the
full
code, can you please see my code and give me solution.
This iam struggling for three foure days.. please reply the solution.

Form1.cs
-----------
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace transparant
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
UserControl1 uc;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//

uc = new UserControl1();
uc.Location = this.Location;
uc.Size = this.Size;
this.Controls.Add(uc);

this.SizeChanged += new EventHandler(OnSizeChanged);
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Name = "Form1";
this.Text = "Form1";
this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void OnSizeChanged(object sender, EventArgs args)
{
uc.Size = this.Size;
}
}
}

UserControl1.cs
------------------
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace transparant
{
public class UserControl1 : System.Windows.Forms.UserControl
{
private System.ComponentModel.Container components = null;
private Form3 frm;
public UserControl1()
{
// This call is required by the Windows.Forms Form Designer.
InitializeComponent();

// TODO: Add any initialization after the InitForm call

frm = new Form3();
frm.Location = new Point(200,200);
frm.Size = new Size(400,400);
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
//
// UserControl1
//
this.Name = "UserControl1";
this.Size = new System.Drawing.Size(568, 360);
this.Click += new System.EventHandler(this.UserControl1_Click);

}
#endregion

protected override void OnPaint(PaintEventArgs e)
{
//Image you can draw any bmp image.
Bitmap bmp = new Bitmap("C:\\medical.bmp");
e.Graphics.DrawImage(bmp, 0, 0, this.Width, this.Height);
}
private void UserControl1_Click(object sender, System.EventArgs e)
{
frm.Show();
}

}

Form3.cs
----------
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace transparant
{
/// <summary>
/// Summary description for Form3.
/// </summary>
public class Form3 : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

private PictureBox picBox;
private bool bDrag = false;
private Point prevPoint;
private Chart chart;
public Form3()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

this.FormBorderStyle = FormBorderStyle.None;
this.SizeChanged += new EventHandler(OnSizeChanged);
CreatePicBox();

CreateChart();

this.BackColor = Color.Black;
this.TransparencyKey = Color.Black;
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.Size = new System.Drawing.Size(300,300);
this.Text = "Form3";
}
#endregion

private void CreatePicBox()
{
picBox = new PictureBox();
picBox.Size = new Size(10,10);
picBox.BackColor = Color.Red;
picBox.MouseDown += new MouseEventHandler(OnMouseDown);
picBox.MouseMove += new MouseEventHandler(OnMouseMove);
picBox.MouseUp += new MouseEventHandler(OnMouseUp);
picBox.Cursor = Cursors.SizeNWSE;
this.Controls.Add(picBox);
}

private void OnMouseDown(object sender, MouseEventArgs args)
{
picBox.Capture = true;
bDrag = true;
prevPoint = new Point(args.X, args.Y);

}

private void OnMouseMove(object sender, MouseEventArgs args)
{
Point pt = new Point(args.X, args.Y);

if (bDrag)
{
this.Cursor = Cursors.SizeNWSE;
int width = this.Size.Width;
int height = this.Size.Height;
width += (args.X - prevPoint.X);
height += (args.Y - prevPoint.Y);
this.Size = new Size(width, height);

this.Refresh();
}

}

private void OnMouseUp(object sender, MouseEventArgs args)
{
bDrag = false;
picBox.Capture = false;

}

private void OnSizeChanged(object sender, EventArgs args)
{
picBox.Location = new Point(Size.Width - 10, Size.Height - 10);
uc2.Size = new Size(Size.Width, Size.Height - 50);
}

UserControl uc2;
private void CreateChart()
{
uc2 = new UserControl2();
uc2.Location = this.Location;
Controls.Add(uc2);
}

}
}

USerControl2.cs
-------------------
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

namespace transparant
{

/// <summary>
/// Summary description for UserControl2.
/// </summary>
public class UserControl2 : System.Windows.Forms.UserControl
{


/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Image img;

public UserControl2()
{
// This call is required by the Windows.Forms Form Designer.
InitializeComponent();


// TODO: Add any initialization after the InitForm call

}


/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
//
// UserControl2
//
this.Name = "UserControl2";
this.Size = new System.Drawing.Size(440, 320);

}
#endregion


protected override void OnPaint(PaintEventArgs e)
{
int x = 210;
int y = 160;
int width = 30;
int height = 30;

Pen pen = new Pen(Color.Yellow, 2);

//Brush brush = new SolidBrush(Color.FromArgb(60, Color.Red));
//e.Graphics.FillRectangle(brush, 0, 0, this.Width, this.Height);

for(int i = 0; i < 5; i++)
{
e.Graphics.DrawEllipse(pen, x, y, width, height);
x -= 30;
y -= 30;
width += 60;
height += 60;
}
}

}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top