Solution : Nasty Flickering on OnPaint Graphics.Invalidate()

M

Mark Johnson

Solution : Nasty Flickering on OnPaint Graphics.Invalidate()

First I would like to thank Stoitcho Goutsev for solving this problem.
I would also like to thank Chris Tacke, eMVP for the help offered,
as well as Jigar Desai who supplied the masterMindDragDropCode.zip Sample
at http://www.c-sharpcorner.com/Code/2002/May/MasterMindWithDragDrop.asp
which showed me how to support Drag and Drop under .NET Framework.Compact.

Using : VS NET 2003 - C# for .NET Framework and .NET Framework.Compact
Problem : OnPaint Graphics.Invalidate() caused flicking when screen was
repainted.
Goal : Smooth moving of Card while draging from one Position to another
Sample : as done in Solitair on PC and WinCe

The solution offered by Stoitcho Goutsev works on both the PC and WinCe.

1) I had created a class (Cards) which recieve a reference to the
System.Windows.Forms.Panel that it was to Paint on.
2) Inside this class I added OnMouseDown/Up/Move OnPaint and
OnPaintBackground Events
- the (empty) OnPaintBackground was supposed to prevent a Clear() before
OnPaint was called
- this did not work and caused the flickering
- of couse these Events are removed on Dispose !

The following was done to rectify this problem :

1) Create a class called FlickerFreePanel with a overided OnPaintBackground
Method that does nothing.
- I addded this to my Cards namespace
- Stoitcho Goutsev added it inside the Main Form (private class
FlickerFreePanel:panel)

#region class FlickerFreePanel
/// <summary>
/// Create a Panel that does not use OnPaintBackground
/// <remark>
/// <para>this solves the Flicker Problem on OnPaint()</para>
/// <para>Warning for Compact : make sure the Size fills the parent
compleatly!</para>
/// </remark>
/// </summary>
class FlickerFreePanel:System.Windows.Forms.Panel
{
protected override void
OnPaintBackground(System.Windows.Forms.PaintEventArgs pevent)
{ // Thank you : Stoitcho Goutsev ([email protected])
//Stops painting the background
//base.OnPaintBackground (pevent);
} // protected override void OnPaintBackground(PaintEventArgs pevent)
} // private class FlickerFreePanel:panel
#endregion

2) Declare a FlickerFreePanel and class you want to draw with in the Main
Form

private mj10777_Cards.FlickerFreePanel panelCardGame;
private mj10777_Cards.TryGame_Cards mj_Cards;

3) Create a Method that does all the work needed, since there is no
Designer support for this Class
- note that this code sample supports both Framework and
Framework.Compact when a
pre-processor var (called "COMPACT") has been set in the Compact
Project.
- in the Compact Project you must make sure that Panel fills the parent
Control compleatly
otherwise you will see the Background color of the parent Control
where it does not fit!
- in the PC Project use .Fill to avoid this (I hope this will one day be
suppported under Compact)

#region OnCreateCardGame
/// <summary>
/// Does all the woek neede to start the CardGame
/// - define a FlickerFreePanel and CardGame Member in Form
/// - call from Form Constuctor
/// </summary>
public void OnCreateCardGame()
{// create Flicker free Panel for the Card game
#if !COMPACT
this.SuspendLayout();
#endif
this.panelCardGame = new mj10777_Cards.FlickerFreePanel();
#if !COMPACT
this.panelCardGame.SuspendLayout();
#endif
this.panelCardGame.BackColor = System.Drawing.Color.Green;
#if COMPACT
this.panelCardGame.Location = new System.Drawing.Point(0,0);
//this.panelCardGame.Size = tabControlPage030.Size;
this.panelCardGame.Size = new
System.Drawing.Size(tabControlPage030.Size.With+5,tabControlPage030.Size.Hei
ght+5);
this.tabControlPage030.Controls.Add(this.panelCardGame);
#else
this.panelCardGame.BackColor = System.Drawing.SystemColors.Desktop;
this.panelCardGame.Dock = System.Windows.Forms.DockStyle.Fill;
this.panelCardGame.ForeColor = System.Drawing.Color.White;
this.panelCardGame.Location = new System.Drawing.Point(0, 0);
this.panelCardGame.Name = "panelCardGame";
this.panelCardGame.Size = new System.Drawing.Size(792, 570);
this.panelCardGame.TabIndex = 0;
this.Controls.Add(this.panelCardGame);
this.panelCardGame.ResumeLayout();
this.ResumeLayout();
#endif

//--------------------------------------------------------------------------
-----
// Cards :
// - Panel : where to Paint to
// - My Documents Dir : where to find the Images
// - Language : 1=English ; 49=German
// - CardsType : 0=CardsDll ; 1=PC-Cards ; 2=WinCe-Cards
// - Function ; 0=none ; 1=call OnSaveCardsDllBitmap() ;
// - ConformToBase ; 0=no ; 1=yes
// Try_Games_Cards :
// - none yet ;

//--------------------------------------------------------------------------
-----
mj_Cards = new
mj10777_Cards.TryGame_Cards(this.panelCardGame,"",49,1,0,1);
mj_Cards.sbStatusBar = statusBarCardGame;
//-------------------------------
} // public void OnCreateCardGame()
#endregion

4) Call OnCreateCardGame() in Constructor of the Main Form.

This solution created a flicker free User-Interface and is easy to use for
the programmer
to support on both the PC and Compact Platforms.

I hope that this (long) explanation will be of help to somebody,

Mark Johnson, Berlin Germany
(e-mail address removed)
 

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