Build a custom drawn control for .net compact framework - c#

I just started on the .net compact framework. I want to draw a Sudoku field on the screen. So I put down a PictureBox and defined a method for the Paint event:
private void pictureBoxPlayfield_Paint(object sender, PaintEventArgs e)
{
// use e.Graphics to draw the grid, numbers and cursor
}
This works, but you can see as the grid is drawn. So my question is, what is the right/better way to create such a custom control? Is there maybe a way to enable double buffering?

There is no built-in support for double buffering in the Compact Framework. You can add it yourself, PictureBox already supports the Image property. Create a Bitmap in the constructor and assign it to Image. You don't need the Paint event anymore, the one provided by PictureBox already draws it to the screen.
Whenever the image needs to change, create a Graphics object with Graphics.FromImage(), passing the PB's Image and draw your stuff. Call the PB's Invalidate() method to tell it that it needs to redraw the image. If you still see flicker, override the PB's OnPaintBackground() method and do nothing.
The only other consideration is handling resizing, you'd need a larger or smaller Bitmap. Not so sure that would be necessary for a game.

Related

Graphics FillEllipse in Picturebox save state

I create graphics thru Graphics.FillEllipse(...) which is drawn on Picturebox. After all drawing is done I would like to save current state of Picturebox with points drawn. Can someone point me to right direction, or what is the best approach to achieve desired functionality?
Thanks in advance ;)
Windows Forms painting on controls (including of course PictureBox) is immediate mode drawing. Whatever you are drawing must be repainted in some event handler, or it is lost on the next repaint. Typically the Control.Paint event. Perhaps you should move the drawing logic into some method that takes a Graphics instance as a parameter. When you want to draw to a bitmap, for example, you can create a graphics object from the bitmap using Graphics.FromImage(...) and pass it to the painting method. Whatever you "paint" in the bitmap stays in the bitmap: no need to repaint. Then you can save or do whatever you want with the bitmap.
See immediate versus retained mode:
http://msdn.microsoft.com/en-us/library/windows/desktop/ff684178%28v=vs.85%29.aspx

Issue on PowerPack Controls and Graphics upon clearing and redrawing

To briefly explain:
I have a win form written in C# that contains a panel.
The panel contains a shape container, through that I have programmatically added some oval and line shape controls from VisualBasic.PowerPacks to the panel.
The reason of using such shapes was I needed to perform some operations on their MouseHover and MouseClick events.
I have also drawn some graphics like strings and ellipse on the panel using Graphics in Paint(object sender, PaintEventArgs e) method of the panel.
The application has zoom in and zoom out buttons and whenever user clicks on them the size of shapes and graphics are supposed to be changed based on the scale.
To redraw graphics after scaling, I needed to clear the old ones before drawing new graphics in the new scale, otherwise they remained on the panel. So, I used Clear(Color color) method of the graphic to do so.
Now, the problem I have is upon using Clear(Color color), everything including shape controls gets disappeared. By my perception, I do not expect shapes to be disappeared because they should be treated as controls, unless I am missing something here. How can I avoid this issue? Any advice would be appreciated.
Solved the problem by invalidating "shape.Invalidate()" shape controls.

Making scribbles on a monochrome image

Imagine there's a picturebox which loads a monochrome image. And there is a need to make few color scribbles on it. I have no background with graphics. Would it be just a pen drawing pixels or something more complex I don't know.
Target language is C#. Technology: WinForms.
I think the easiest way to achieve what you want would be to create a very lightweight retained mode drawing system. Keep track of all the positions where the user has scribbeled and draw dots/circles/lines/rubberducks/whatever at these positions in the PictureBox's Paint event. On mousedown+move events, call the PictureBox' Invalidate() function. The original picture must either be painted underneath or in the class' OnPaintBackground (which IMO is more elegant).
This tutorial should get you started:
https://web.archive.org/web/20121006140255/http://www.bobpowell.net/backtrack.htm

Border for Dynamically created buttons in Windows Application C#.net

Hai all,
I am dynamically creating button at runtime in a c#.net Windows Application, I want to draw a border for the dynamically created button how can i do it, i tried this
private void DrawBorder(Button bt)
{
Graphics g = bt.CreateGraphics();
ControlPaint.DrawBorder(g, bt.DisplayRectangle, Color.FromArgb(229, 227, 227), ButtonBorderStyle.Solid);
g.Dispose();
}
but it is not showing any error but i can't see any border
Please Help
If you want a button that draws differently, you should probably create a new class inheriting from Button, and in it you would override OnPaint and/or OnPaintBackground and implement your drawing logics, such as drawing a border.
The reason your current solution is not working probably depends on when you are calling your DrawBorder method. If your drawing code is not executed as part of handling an OnPaint event, then the graphics you draw will be drawn-over in the next paint.
But anyway, instead of drawing the border yourself, can't you set the Border proerpty on the Button object?

WinForms performance mystery: derived PictureBox control slower than the original?

In my .NET 2.0 project, I made an empty derived class of System.Windows.Forms.PictureBox:
public class NewPictureBox : PictureBox
{
//absolutely nothing
}
Then I did the following:
set both the derived control's and the base control's Image property to a rather large image (800x600), SizeMode is Normal (only the upper-left portion is displayed);
hooked up several of the NewPictureBox's and PictureBox's events so a selection box can be drawn when dragging the mouse on the surface;
set it up so the selection box's properties (Width/Height) will be updated on NumericUpDown controls in real time.
The problem is when dragging the mouse real fast on the derived PB, there is considerable "choppiness" compared to doing the same on the base PB. The Width/Height values are not updated in real time.
Does anybody know why is it like this? How do I achieve the same smoothness with the derived control? Thanks!
For anyone who wishes to check out the minimal sample project with the problem described:
http://www.mediafire.com/?i2nq2tmmjzx
Getting an image resized by PB to fit the control is very expensive. GDI+ has a very good filter but it doesn't come for free. Resize the image yourself before you assign it to the Image property so the PB doesn't have to resize it.
Using a bitmap created with Format32bppPArgb can make a big difference too, it is 10 times faster than any other format.

Categories

Resources