Archived

This topic is now archived and is closed to further replies.

Custom C# Control Flicker...

This topic is 5001 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

I''m developing my own programming window (like that which you program in when using VS .Net); however, it flickers something awful when the window is redrawn (each time the user presses a key on the keyboard). Any ideas on how I can reduce flicker? I''ve set the style of the control to doublebuffered. But I don''t know what to do beyond that in the .Net envoronment. In C++ I would just use the Win32 API to create an offscreen buffer that Iwould draw to then copy and paste from there. This would prevent flicker, but I don''t know what to do in .Net to help prevent this flicker. Thanks for any help, Mike

Share on other sites
override OnPaintBackground.

http://staff.develop.com/candera/weblog2/articleview.aspx/WinForms/FlickerFree.xml

Share on other sites
thanks, that helped a ton.

Share on other sites
With Windows Forms you can enable double buffering with custom controls which I found removed all the flicker. I don''t have the code at hand right now due to being at work. But it shouldnt be that difficult to find now you know what to look for.

Share on other sites
quote:
Original post by Anonymous Poster
With Windows Forms you can enable double buffering with custom controls which I found removed all the flicker.

He already said he tried that.

However, he didn''t say that he set the AllPaintingInWmPaint flag...

Share on other sites
SetStyle( ControlStyles.AllPaintingInWmPaint, true );
SetStyle( ControlStyles.DoubleBuffer, true );

always worked for me.

Share on other sites
okay, this is odd, when i use:

SetStyle( ControlStyles.AllPaintingInWmPaint, true );

the result is an entirely black window. I have no idea why. the drawing works fine unless I set that flag. Any ideas? With AllPaintingInWmPaint sent, the window is entirely black until you press a key. when you press the key, it flickers for a moment (durring that flicker it is properly drawn), the goes back to being all black. All of the code for my window''s drawing resides in OnPaint event handler.

Any ideas?

Share on other sites
What happens if you use delegates?

Constructor:
Paint += new PaintEventHandler(PaintProc);

This is the kind of code generated by VS.NET when asking to handle the Paint event, it doesn''t override OnPaint.

Share on other sites
quote:
Original post by Anonymous Poster This is the kind of code generated by VS.NET when asking to handle the Paint event, it doesn''t override OnPaint.

Overriding OnPaint is the recommended way if you are deriving from Form. However, you are required to call base.OnPaint().

--
AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.
[Project site] [Blog] [RSS] [Browse the source] [IRC channel]

Share on other sites
Okay, I've developed my own OnPaint handler, now I'm having an interesting problem. Here is my OnPaint

protected override void OnPaint( PaintEventArgs e ){	base.OnPaint( e );	Console.Write( "OnPaint\n" );	FillEmptySpace();	DrawText( true );	DrawLineNumbers();	DrawBorders();}

and here is the constructor

public iCoderTextBox(){	this.SetStyle( ControlStyles.DoubleBuffer |                       ControlStyles.UserPaint |                       ControlStyles.Opaque |                        ControlStyles.AllPaintingInWmPaint , true ); 	// This call is required by the Windows.Forms Form Designer.	InitializeComponent();		// TODO: Add any initialization after the InitForm call	m_bBorderColor		= new SolidBrush( Color.FromArgb( 0, 89, 16 ) );	m_bNumColColor		= new SolidBrush( Color.FromArgb( 221, 255, 227 ) );	m_bCodeColColor		= new SolidBrush( Color.White );	m_bSelLineColor		= new SolidBrush( Color.FromArgb( 243, 243, 243 ) );	m_bCodeFontColor	= new SolidBrush( Color.FromArgb( 0, 0, 0 ) );	m_bNumFontColor		= new SolidBrush( Color.FromArgb( 0, 89, 16 ) );	m_iFontHeight = m_font.Height;	m_lnManager.LoadLanguageFile( "cpp.xml" );	m_hlManager.LoadLanguageFile( "cpp.xml" );	m_kbHandler = new KeyboardHandler( this );	m_lnManager.NewLine();}

Here is how it looks without ControlStyles.AllPaintingInWmPaint set to true:
good

Here is how it looks with that style set

[edited by - Mike on April 6, 2004 7:02:23 PM]

Share on other sites
Have you tried getting rid of .Opaque?

Share on other sites
Yes, I have. If my understanding is correct, the opaque setting is the same as overriding the OnPaintBackground and doing nothing (which i do). The presence of the opaque setting does not affect the black issue. The only setting that does is the AllPaintInWmPaint.

Share on other sites
I belive the problem is that when you set the flag "AllPaintingInWmPaint" .Net belive that you only use the graphics provided in the PaintEventArgs (e.Graphics)

Share on other sites
Considering that with the WM_PAINT message you are supose to use the HDC you get from BeginPaint, I''d imagine you''re quite right. I''ll give it a try shortly.

Share on other sites
yeah, that fixed it, thanks a lot.

Share on other sites

Hi guys,

I got a slightly differt question: How do you avoid your PictureBox from flickering ???

This control does not have a SetStyle method, and overriding some events does not seem to work for some strange reason.

I want to add some animation to my c# picturebox....

Thanks,

Edo

Share on other sites
Derive from it and do the .SetStyle thing in the constructor? .SetStyle is protected, IIRC.

• Forum Statistics

• Total Topics
628740
• Total Posts
2984467

• 12
• 25
• 11
• 10
• 16