6 Replies - 25221 Views - Last Post: 28 June 2011 - 12:16 PM Rate Topic: -----

#1 cipher_nemo   User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 35
  • Joined: 17-July 08

Resize, CreateParams, reducing flicker, etc.

Posted 24 March 2009 - 09:18 AM

My question is based on C#, .NET 3.x, Windows Forms Application

I always struggle with .NET controls to reduce their fickering when resizing a form (onpaint, redrawing, etc.). To overcome this, I've enjoyed the simplicity of overriding CreateParams to set the window style of my forms as transparent. However, this causes issues with some .NET controls such as the Numeric Up/Down control.

But the good news is, my Numeric Up/Down controls do repaint properly after the form is re-sized by the user.

My question:

How do I found out what a re-size is doing that redraws these numeric up/down controls correctly and use it programmatically whenever I want? I pretty much want to use this to draw the up/down controls when they're shown on a tab page (user clicks back and forth between tab pages on a tab control).

Here's the override I use to reduce flicker in each form (sets window style for transparency):

protected override CreateParams CreateParams
	{
		get
		{
			CreateParams cp = base.CreateParams;
			cp.ExStyle |= 0x02000000;
			return cp;
		}
	}


May be there's a better way to reduce flicker, but I've found this to be the most simple and effective one so far (aside from drawing numeric up/down controls poorly).

Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Resize, CreateParams, reducing flicker, etc.

#2 cipher_nemo   User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 35
  • Joined: 17-July 08

Re: Resize, CreateParams, reducing flicker, etc.

Posted 30 March 2009 - 06:13 AM

It's unfortunately that no one appears to know of any advice, alternatives, or solutions for this. I'd going to have to move back to text boxes instead of numeric up/down controls. :(
Was This Post Helpful? 0
  • +
  • -

#3 cipher_nemo   User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 35
  • Joined: 17-July 08

Re: Resize, CreateParams, reducing flicker, etc.

Posted 23 November 2009 - 11:41 AM

View Postcipher_nemo, on 30 Mar, 2009 - 07:13 AM, said:

It's unfortunately that no one appears to know of any advice, alternatives, or solutions for this. I'd going to have to move back to text boxes instead of numeric up/down controls. :(


7 months later and still no replies? I can't believe that no one has dealt with this before.

This post has been edited by cipher_nemo: 23 November 2009 - 11:41 AM

Was This Post Helpful? 0
  • +
  • -

#4 Momerath   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1021
  • View blog
  • Posts: 2,463
  • Joined: 04-October 09

Re: Resize, CreateParams, reducing flicker, etc.

Posted 23 November 2009 - 12:11 PM

I don't think anyone goes back 7 months to look at old messages.

Have you tried the DoubleBuffered = true option? Do you only redraw once the sizing it over?
Was This Post Helpful? 0
  • +
  • -

#5 lesPaul456   User is offline

  • D.I.C Addict
  • member icon

Reputation: 175
  • View blog
  • Posts: 729
  • Joined: 16-April 09

Re: Resize, CreateParams, reducing flicker, etc.

Posted 23 November 2009 - 12:42 PM

When the form's re-size event is called, the control redraws itself. If you want to force the control to redraw itself, just invoke the control's Invalidate() method.
Was This Post Helpful? 0
  • +
  • -

#6 cipher_nemo   User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 35
  • Joined: 17-July 08

Re: Resize, CreateParams, reducing flicker, etc.

Posted 17 June 2011 - 07:24 AM

Going back in time... I still have not found a complete answer to this, but since the use of WPF and other advances in .NET this is somewhat deprecated. Still, we sometimes have to work on a Windows Form app and this problem will keep popping up.

View PostMomerath, on 23 November 2009 - 03:11 PM, said:

Have you tried the DoubleBuffered = true option? Do you only redraw once the sizing it over?


DoubleBuffered doesn't help one bit.

View PostlesPaul456, on 23 November 2009 - 03:42 PM, said:

When the form's re-size event is called, the control redraws itself. If you want to force the control to redraw itself, just invoke the control's Invalidate() method.


Invalidate() is for a region, what you meant to say was Refresh() for the entire control. However, this doesn't solve anything either.

A good way to illustrate this problem is this:
  • Create a form.
  • Add a web browser and data grid control to it.
  • Have the web browser navigate to some page.
  • Set a data source for the data grid (another class is fine).
  • Run the app to show the form and resize it. Notice that the web browser flickers like mad.
  • Add the override from the OP.
  • Notice that the web browser no longer flickers, but the drawing of the data grid's lines becomes transparent.


Ultimately the best solution is to handle drawing of individual controls, but it would be nice to find a way to implement this trick globally and then cancel it for specific controls.

A good but hacky workaround is this: http://www.angryhack...plications.aspx. I can use this example just fine. However, instead of disabling it when the form is shown, as explained at the end of that article, I disable it within the HandleCreated event of the control which doesn't work well with it. That way, the flicker-free is active until the control that doesn't like it (ie: datagridview) is shown when switching through tabs on a tab control.

This post has been edited by cipher_nemo: 17 June 2011 - 07:51 AM

Was This Post Helpful? 1
  • +
  • -

#7 cipher_nemo   User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 35
  • Joined: 17-July 08

Re: Resize, CreateParams, reducing flicker, etc.

Posted 28 June 2011 - 12:16 PM

After all of these years I found the near-perfect solution to this. It took me that long because before today it was only a luxury to find a solution to this. Now it's a must-have for me.
:)

Provided your form name is "Form1"...

int intOriginalExStyle = -1;
bool bEnableAntiFlicker = true;

public Form1()
{
	ToggleAntiFlicker(false);
	InitializeComponent();
	this.ResizeBegin += new EventHandler(Form1_ResizeBegin);
	this.ResizeEnd += new EventHandler(Form1_ResizeEnd);
}

protected override CreateParams CreateParams
{
	get
	{
		if (intOriginalExStyle == -1)
		{
			intOriginalExStyle = base.CreateParams.ExStyle;
		}
		CreateParams cp = base.CreateParams;

		if (bEnableAntiFlicker)
		{
			cp.ExStyle |= 0x02000000; //WS_EX_COMPOSITED
		}
		else
		{
			cp.ExStyle = intOriginalExStyle;
		}

		return cp;
	}
} 

private void Form1_ResizeBegin(object sender, EventArgs e)
{
	ToggleAntiFlicker(true);
}

private void Form1_ResizeEnd(object sender, EventArgs e)
{
	ToggleAntiFlicker(false);
}

private void ToggleAntiFlicker(bool Enable)
{
	bEnableAntiFlicker = Enable;
	this.MaximizeBox = true;
}


This will solve any flickering issues when the form is being re-sized by the user, but it will more importantly let other controls such as NumericUpDown and DataGridView draw as intended.

SOLVED! :)
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1