OptimizedDoubleBuffer AllPaintingInWmPaint HOWTO in CF2.0?

  • Thread starter Michael Böckinghoff
  • Start date
M

Michael Böckinghoff

Hello NG,

i have a solution with a control dll for the compact Framework.
For exampel with a Button wich behaves on a special way i want to.

Im painting the button in the overriden OnPaint Method of the control.

Now i see sometimes a littel flickering wich on the full framwork i could
avoid by this 2 codelines:

this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);

How can i do this in the compactframework?


TIA!

Michael

Codesnippet of the OnPaint methode:
#######################

protected override void OnPaintBackground(PaintEventArgs pevent)
{
//Nichts machen damit nicht doppelt gezeichnet wird
//base.OnPaintBackground (pevent);
}


/// <summary>
/// Override the OnPaint method so we can draw the background image
and the text.
/// </summary>
/// <param name="e"></param>
protected override void OnPaint(PaintEventArgs e)
{
SolidBrush brush = new SolidBrush(this.ForeColor);
Graphics g = e.Graphics;



if (this._bPressed)
{
if (this._imgBackgroundImage2 != null)
g.DrawImage(this._imgBackgroundImage2, 0, 0);
else
{
if (this._imgBackgroundImage != null)
{
g.DrawImage(this._imgBackgroundImage, 0, 0);
}
else
{
// Ansonsten ein Graues Kästchen so groß wie der
Button zeichnen!
SolidBrush brushBack = new
SolidBrush(this.BackColor);
g.FillRectangle(brushBack, 0, 0, ClientSize.Width,
ClientSize.Height);
}
}

if (!_bDrawWithoutBorder)
DrawButtonDown(g);
}
else
{
if (this._imgBackgroundImage != null)
g.DrawImage(this._imgBackgroundImage, 0, 0);
else
{
// Ansonsten ein Graues Kästchen so groß wie der Button
zeichnen!
SolidBrush brushBack = new SolidBrush(this.BackColor);
g.FillRectangle(brushBack, 0, 0, ClientSize.Width,
ClientSize.Height);
}

if (!_bDrawWithoutBorder)
DrawButtonUp(g);
}

// LED Links oben einzeichen wenn LED angezeigt werden soll
if (this._bShowLED)
{
if (this._bLEDStatus)
g.DrawImage(this._imgLEDON, 2, 2);
else
g.DrawImage(this._imgLEDOFF, 2, 2);
}

// Wenn Enable false ist dann zeichne ein Rotes X durch das
Control
if (!this.Enabled)
{
DrawRedXross(g);
}

RectangleF recText;

if (this._bShowLED)
{
recText = new RectangleF(15, 2, this.ClientSize.Width - 17,
this.ClientSize.Height - 4);
}
else
{
recText = new RectangleF(2, 2, this.ClientSize.Width - 4,
this.ClientSize.Height - 4);
}

if (this.Text.Length > 0)
{
SizeF sizeText = e.Graphics.MeasureString(this.Text,
this.Font);
StringFormat stringFormat = new
StringFormat(StringFormatFlags.NoClip);

if (sizeText.Width > recText.Width)
{
stringFormat.Alignment = StringAlignment.Center;
stringFormat.LineAlignment = StringAlignment.Near;
}
else
{
stringFormat.Alignment = StringAlignment.Center;
stringFormat.LineAlignment = StringAlignment.Center;
}



e.Graphics.DrawString(this.Text,
this.Font,
new SolidBrush(this.ForeColor),
recText,
stringFormat);
}


brush.Dispose();
}
 
M

Michael Böckinghoff

I did it now this way, (using the styles it seems to be still ab bit more
faster and
less CPU Time expensive) :
http://www.codeproject.com/KB/GDI-plus/flicker_free.aspx
Double-buffering technique the .NET way
Luckily we can achieve the same goal without any direct help from Win32 API.
Image rendering in .NET is very simple and efficient compared to MFC. There
are two functions in Graphics class to render your Image object on screen,
these are DrawImage and DrawImageUnscaled. What makes these functions
important is the fact that .NET always uses BitBlt in background to render
the image on DC. So if we are able to do our off-screen drawing in an Image
object, we can use these functions to render this object directly to DC and
have the smooth animated effects.

The technique is same, but the way to implement it differs a little bit. In
the code fragment below, we are using a Bitmap object to do our off-screen
drawing. In order to draw on some Image object, it must be attached to a
Graphics object. We can create a new Graphics object from an Image object
using the static member function, of Graphics, named FromImage. Once we get
a Graphics object from some Image object, any drawing done on this Graphics
object will actually be changing the Image.

Bitmap offScreenBmp;
Graphics offScreenDC;
offScreenBmp = new Bitmap(this.Width, this.Height);
offScreenDC = Graphics.FromImage(offScreenBmp);Provided that we have created
offScreenDC as per shown in example above, we can implement the double
buffering technique as per the code fragment below.

Graphics clientDC = this.CreateGraphics();
// do drawing in offScreenDC
// do drawing in offScreenDC
// do drawing in offScreenDC
clientDC.DrawImage(offScreenBmp, 0, 0);
 

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