equivalent of Control.CreateGraphics() for Compact framework 2.0
Hello i need to convert a control.CreateGraphics() for my app. VS2005 gave me a runtime error saying that it's not supported for Compact framework. Is there anyway to bypass that
Hello how can i override the OnPaint method Here is my problem: First I setup some circles on a panel with the paint event
Rectangle[] Circles; private void panel1_Paint(object sender, PaintEventArgs e) { Graphics Draw = e.Graphics; Circles = new Rectangle[ 8 ]; for (int i = 0; i < 8; i++) { Circles[ i ] = new Rectangle(10, 200 - (i * 24), 20, 20); Brush brWhite = new SolidBrush(Color.White); Pen penBlack = new Pen(Color.Black);
Draw.FillEllipse(brWhite, Circles[ i ]); Draw.DrawEllipse(penBlack, Circles[ i ]); } } /////////////////////////////////////////////////////////////////////////////////////////////// Then I create a method to update the color of those circle depending if it's a one or a zero
private void UpdateCircle (Graphics Update, int OneIndex, int ZeroIndex) { Brush brWhite = new SolidBrush(Color.White); Pen penBlack = new Pen(Color.Black); Brush brRed = new SolidBrush(Color.Red);
if (OneIndex > -1) // Draw red circle if one { Update.FillEllipse(brRed, Circles[OneIndex]); Update.DrawEllipse(penBlack, Circles[OneIndex]); }
else if (ZeroIndex > -1) // Draw white circle if zero { Update.FillEllipse(brWhite, Circles[ZeroIndex]); Update.DrawEllipse(penBlack, Circles[ZeroIndex]); } } //////////////////////////////////////////////////////////////////////////////////// On the method where i read in the 1 and 0 i just need to call the UpdateCircle method and pass in the the graphics object to precise where i want it to draw and the the variables to read the index of 1 and 0.
Graphics Update = this.panel1.CreateGraphics(); int OneIndex =........ int ZeroIndex =....... UpdateCircle(Update, OneIndex, ZeroIndex);
It works fine on the desktop but in the CF world i only can draw on a form. Anyway to go around this
In my example code I just simply called panel1.Invalidate() for simplicity. If you only want to invalidate a portion of a control you can use the Invalidate(Rectangle) override, passing it the rectangle (of the ellipse in your case) that needs to be updated.
You can do a series of calles to Invalidate(Rectangle) if you want more than one rectangle to be updated on the next paint event.
You need to store the colors that you want to change in UpdateCircle() in member variables that will be used by the Paint event handler. For example:
Brush circleBrushes[] = new Brush[ 8 ]; Pen circlePen = new Pen(Color.Blank); //... presumably in constructor for(int i = 0; i < circleBrushes.Length; ++i) { circleBrushes[ i ] = new SolidBrush(Color.Wite); }
private void panel1_Paint(object sender, PaintEventArgs e) { // ... Debug.Assert(Circles.Length == circleBrushes.Length); for(int i = 0; i < Circles.Length; ++i) { e.Graphics.FillEllipse(circleBrushes[ i ], Circles[ i ]); e.Graphics.DrawEllipse(circlePen, Circles[ i ]); } // ... }
private void UpdateCircle(int OneIndex, int ZeroIndex) { if (OneIndex > -1) // Draw red circle if one { circleBrushes[OneIndex] = new SolidBrush(Color.Red); panel1.Invalidate(); } else if (ZeroIndex > -1) // Draw white circle if zero { circleBrushes[ZeroIndex] = new SolidBrush(Color.White); panel1.Invalidate(); } }
Thank you for your help. It was a great fix and it works. Oh one more thing since i constantly updating the indexes, I notice that the panel and circles got flickered. Is it due to the panel1.Invalidate(). Is there a way to prevent this flickering event And again I appreciate you help.
equivalent of Control.CreateGraphics() for Compact framework 2.0
robydx
Here is my problem:
First I setup some circles on a panel with the paint event
Rectangle[] Circles;
private void panel1_Paint(object sender, PaintEventArgs e)
{
Graphics Draw = e.Graphics;
Circles = new Rectangle[ 8 ];
for (int i = 0; i < 8; i++)
{
Circles[ i ] = new Rectangle(10, 200 - (i * 24), 20, 20);
Brush brWhite = new SolidBrush(Color.White);
Pen penBlack = new Pen(Color.Black);
Draw.FillEllipse(brWhite, Circles[ i ]);
Draw.DrawEllipse(penBlack, Circles[ i ]);
}
}
///////////////////////////////////////////////////////////////////////////////////////////////
Then I create a method to update the color of those circle depending if it's a one or a zero
private void UpdateCircle (Graphics Update, int OneIndex, int ZeroIndex)
{
Brush brWhite = new SolidBrush(Color.White);
Pen penBlack = new Pen(Color.Black);
Brush brRed = new SolidBrush(Color.Red);
if (OneIndex > -1) // Draw red circle if one
{
Update.FillEllipse(brRed, Circles[OneIndex]);
Update.DrawEllipse(penBlack, Circles[OneIndex]);
}
else if (ZeroIndex > -1) // Draw white circle if zero
{
Update.FillEllipse(brWhite, Circles[ZeroIndex]);
Update.DrawEllipse(penBlack, Circles[ZeroIndex]);
}
}
////////////////////////////////////////////////////////////////////////////////////
On the method where i read in the 1 and 0 i just need to call the UpdateCircle method and pass in the the graphics object to precise where i want it to draw and the the variables to read the index of 1 and 0.
Graphics Update = this.panel1.CreateGraphics();
int OneIndex =........
int ZeroIndex =.......
UpdateCircle(Update, OneIndex, ZeroIndex);
It works fine on the desktop but in the CF world i only can draw on a form.
Anyway to go around this
LamptonWorm
In my example code I just simply called panel1.Invalidate() for simplicity. If you only want to invalidate a portion of a control you can use the Invalidate(Rectangle) override, passing it the rectangle (of the ellipse in your case) that needs to be updated.
You can do a series of calles to Invalidate(Rectangle) if you want more than one rectangle to be updated on the next paint event.
donWong
You need to store the colors that you want to change in UpdateCircle() in member variables that will be used by the Paint event handler. For example:
Brush circleBrushes[] = new Brush[ 8 ];
Pen circlePen = new Pen(Color.Blank);
//... presumably in constructor
for(int i = 0; i < circleBrushes.Length; ++i)
{
circleBrushes[ i ] = new SolidBrush(Color.Wite);
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
// ...
Debug.Assert(Circles.Length == circleBrushes.Length);
for(int i = 0; i < Circles.Length; ++i)
{
e.Graphics.FillEllipse(circleBrushes[ i ], Circles[ i ]);
e.Graphics.DrawEllipse(circlePen, Circles[ i ]);
}
// ...
}
private void UpdateCircle(int OneIndex, int ZeroIndex)
{
if (OneIndex > -1) // Draw red circle if one
{
circleBrushes[OneIndex] = new SolidBrush(Color.Red);
panel1.Invalidate();
}
else if (ZeroIndex > -1) // Draw white circle if zero
{
circleBrushes[ZeroIndex] = new SolidBrush(Color.White);
panel1.Invalidate();
}
}
FRED COLES
Talyrond
Nat999
And again I appreciate you help.
khs202968