[c#] Help - GUI

Newbie Spellweaver
Joined
Sep 21, 2014
Messages
32
Reaction score
1
C# Need help

Any Idea, how to achieve smooth transition in c#? Because its so flickery when you put an image as background on a form.

heres my code:

View attachment SlideFormAnimation.rar
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace SlideFormAnimation
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();


this.tm2.Enabled = true;
gbox1.Visible = false;





}
int tx1 = 49;




private void timer1_Tick(object sender, EventArgs e)
{



if (tx1 > 300)
{
timer1.Stop();
}
else
{



this.gbox1.Size = new Size(this.gbox1.Width,tx1);
tx1 += 2;
gbox1.Show();
}
}


private void timer2_Tick(object sender, EventArgs e)
{
if (this.Height >= 400) this.tm2.Enabled = false;
else this.Height += 66;
timer1.Start();

}




}
}
 
I didn't test your project but:

Most of the time when you encounter flickering it is because the controls are drawn directly onto the device without any synchronization with the Monitor Refresh Rate. So while the dialog (rather the UI thread) is busy drawing the components onto the form / dialog / whatever you have there, the monitor already refreshes and like half of the dialog is drawn. As monitors refresh very frequently this looks like the controls flicker around (when they re-draw themselves).

There are several approaches to prevent this and it's also dependent on your situation. I can highly suggest double buffering where the controls are drawn virtually (in the memory) before an updated result is being drawn to the screen.

Here is a related MSDN article that describes how to enable double buffering for your form application: How to: Reduce Graphics Flicker with Double Buffering for Forms and Controls

Basically add this:
Code:
SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
to the constructor of your Form and try again.
 
I didn't test your project but:

Most of the time when you encounter flickering it is because the controls are drawn directly onto the device without any synchronization with the Monitor Refresh Rate. So while the dialog (rather the UI thread) is busy drawing the components onto the form / dialog / whatever you have there, the monitor already refreshes and like half of the dialog is drawn. As monitors refresh very frequently this looks like the controls flicker around (when they re-draw themselves).

There are several approaches to prevent this and it's also dependent on your situation. I can highly suggest double buffering where the controls are drawn virtually (in the memory) before an updated result is being drawn to the screen.

Here is a related MSDN article that describes how to enable double buffering for your form application: How to: Reduce Graphics Flicker with Double Buffering for Forms and Controls

Basically add this:
Code:
SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
to the constructor of your Form and try again.


{
InitializeComponent();

DoubleBuffered = true;

this.tm2.Enabled = true;
gbox1.Visible = false;





}

i have already tried putting it in initialization.
but still its so laggy when you have an image in background.
 
Then it might be the case that the control you use for the background draws, just like any other control, in a random order. With that, it could overdraw some other controls. Or you need to set the double buffering property for the control that draws the background explicitly.

Would you mind sharing the whole basic drawing source code you use? This will make it much easier to debug the problem.
Also, I don't like .exe files I have no clue about for obvious security reasons.
 
Back