Are AutoScrolling bounds setup in World or Device coordinates
AutoScrollingMinSize is setup in device coordinates. If you are using a world coordinate
system other than pixels this means that you have to translate between world and device
coordinates before you set these value. You can use the Graphics.TransformPoints API to
do this as shown below.
All this does is go from the GraphicsUnit that you are using to pixels. For example if your
GraphicsUnit is Inch, then with a Graphics object that represents a monitor screen you will
have translated values that come to about 100 pixels for every logical inch. Using this API
ensures that you are insulated from the underlying device. The values will be much higher
for printers. Another important point to remember is that your Transform in the painting code (please
refer earlier FAQ in this section on AutoScrolling implementation) will have to be in
logical coordinates and will hence have to translate between logical and device coordinates.
protectedvoidOnHandlePaint(object sender, PaintEventArgs args)
{
Graphics g = args.Graphics;
g.PageUnit = GraphicsUnit.Inch;
// this will always be in pixels
Point[] ptAutoScrollPos = new Point[]{this.AutoScrollPosition};
// We have to convert the pixel value (device) to inches (logical..world)
g.TransformPoints(CoordinateSpace.World, CoordinateSpace.Device, ptAutoScrollPos);
// set up a simple translation so that we draw with respect to the doc bounds// and not the physical bounds
g.TranslateTransform(ptAutoScrollPos[0].X, ptAutoScrollPos[0].Y);
g.DrawEllipse(_pen, _rectEllipse);
}
Play around with the sample and all should be clear. When you run the sample nothing will
be seen on the screen. Scroll around. Remember the logical units are in inches. You will
be looking at a huge ellipse!
What are some things to remember when drawing in Window Forms
Check out the Painting techniques using Windows Forms by Fred Balsigerat gotnetdot.com. It is a good basic discussion of how to get the best performance from Windows Forms drawing. His hints include leveraging the power of the .Net Framework by using the proper controls and control styles as well as consolidating painting code in the OnPaint and OnPaintBackground methods.
Why does my transparent Panel whose parent has a gradient background flicker a lot when resized?
Make sure to set these flags for your panel, in it’s constructor (for example).
You will have to derive a class from Panel otherwise if you were initially using the Panel directly.
Why does a Control not draw transparent even after setting it’s BackColor to Transparent?
This is possible if the Control is drawn by the system, rather than by the framework. This is the case for example with the Label control when it’s set to FlatStyle.System. You should then set to to something else other than System for your transparent drawing to work.
Why am I not being able to set a Color.Transparent color as a background to my control?
Sometimes the framework will throw an exception if you try to set the bg color to be transparent. This is because the Control doesn’t support transparent colors. To work around this you should call this method from within the Control:
To draw on any hwnd, you need to get a Graphics object from that hwnd. Once you have the Graphics object, you can then use its methods to draw. Of course, since this drawing is not done in the Paint handler, it will not be automatically refreshed when the control is redrawn for any reason.
Graphics g = Graphics.FromHwnd(this.Handle);
SolidBrush brush = new SolidBrush(Color.Red);
Rectangle rectangle = new Rectangle(25, 25, 100, 100);
g.FillRectangle(brush, rectangle);
How can I minimize flickering when drawing a control
The Window.Forms framework offers support for double buffering to avoid flickers through ControlStyles. Double buffering is a technique that attempts to reduce flicker by doing all the drawing operations on an off-screen canvas, and then exposing this canvas all at once.
To turn on a control’s double buffering, you need to set three styles.
publicUserPictureBox() //derived from System.Windows.Forms.Control
{
// This call is required by the Windows.Forms Form Designer.
InitializeComponent();
// Activates double buffering
SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
SetStyle(ControlStyles.DoubleBuffer, true);
// TODO: Add any initialization after the InitForm call
}
When can I not use AutoScrolling? In other words why would I ever need any other form of autoscrolling
AutoScrolling does not allow you to dynamically control the scrolling interval. If you are drawing a complex control such as grid then you want to be able to scroll based on the current row height, column width etc. With AutoScrolling you cannot do this. You have to implement Scrolling yourself in such cases.
Autoscrolling is also not very useful if you want multiple views to share a scrollbar. The most common place where you see this is with a workbook. There is no direct way in Winforms to hook up your own scrollbars with the AutoScrolling implementation.
Share with