By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,665 Members | 1,912 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,665 IT Pros & Developers. It's quick & easy.

Transparent UserControl Flickers when Form get resized

P: n/a
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;
}
}

}
}

Nov 16 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Actually i have shown you the overall scenario of my project.
Like you said i can create a memory bitmap and draw it in UserControl1. But the problem is in my project the
userControl1 will be Third party control, whcih is developed in unmanaged code, Thay are drawing images in their
usercontrol, On top of that i have to show my transparent form.

So creating memory bitmap and drawing in userControl1 is now ruled out.

Can you please suggest me, is there any possiblity to do in Form3 and userControl2.

"pindlebot" wrote:
You probably don't want to be reopening and reading in the bmp in OnPaint in UserControl1, that will slow it down. When you resize a window it will need to paint the window again (infact even as you drag it bigger it will repaint may times as you go). It might be a performance issue.

You could also try painting the circles onto an offscreen bitmap and then drawing it to the screen in one go, that might be faster. Just remember not to create the bitmaps in OnPaint

//in constructor

//allocate memory for bitmaps

//draw vectors to bitmap
//in OnPaint

//display bitmap

Nov 16 '05 #2

P: n/a
Hi Jaikumar.

You're handling way too many repaints here.

1) In UserControl1.cs, remove everything in the OnPaint handler, and
add this line in the constructor, after InitializeComponent() :

this.BackgroundImage = new Bitmap("C:\\image.bmp");

2) Change the logic of UserControl2 so that when created, it creates
an image in memory with the chart; put a picturebox on UserControl2,
and specify its background image as the image you've just created.
Don't handle any OnPaint events there, let the framework do it for
you.

That should do the trick.

Michel

"Jaikumar" <Ja******@discussions.microsoft.com> wrote in message news:<66**********************************@microso ft.com>...
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.

Nov 16 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.