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!
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.
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.
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.
[C#]
internalclassNativeMethods
{
[DllImport('user32.dll')]
publicexternstatic IntPtr GetDesktopWindow();
[System.Runtime.InteropServices.DllImport('user32.dll')]
publicstaticextern IntPtr GetWindowDC(IntPtr hwnd);
[System.Runtime.InteropServices.DllImport('gdi32.dll')]
publicstaticextern UInt64 BitBlt
(IntPtr hDestDC,
int x,
int y,
int nWidth,
int nHeight,
IntPtr hSrcDC,
int xSrc,
int ySrc,
System.Int32 dwRop);
}
// Save the screen capture into a jpgpublicvoidSaveScreen()
{
Image myImage = new Bitmap(Screen.PrimaryScreen.Bounds.Width,
Screen.PrimaryScreen.Bounds.Height);
Graphics gr1 = Graphics.FromImage(myImage);
IntPtr dc1 = gr1.GetHdc();
IntPtr dc2 = NativeMethods.GetWindowDC(NativeMethods.GetDesktopWindow());
NativeMethods.BitBlt(dc1, 0, 0, Screen.PrimaryScreen.Bounds.Width,
Screen.PrimaryScreen.Bounds.Height, dc2, 0, 0, 13369376);
gr1.ReleaseHdc(dc1);
myImage.Save('screenshot.jpg', ImageFormat.Jpeg);
}
[VB.Net]
Friend Class NativeMethods
_
Public Shared Function GetDesktopWindow() As IntPtr
End Function
_
Public Shared Function GetWindowDC(ByVal hwnd As IntPtr) As IntPtr
End Function
_
Public Shared Function BitBlt(ByVal hDestDC As IntPtr, ByVal x As Integer, ByVal y As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer, ByVal hSrcDC As IntPtr, ByVal xSrc As Integer, ByVal ySrc As Integer, ByVal dwRop As System.Int32) As UInt64
End Function
End Class ’NativeMethods
’Save the screen capture into a jpg
Private Sub SaveScreen()
Dim myImage = New Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height)
Dim gr1 As Graphics = Graphics.FromImage(myImage)
Dim dc1 As IntPtr = gr1.GetHdc()
Dim dc2 As IntPtr = NativeMethods.GetWindowDC(NativeMethods.GetDesktopWindow())
NativeMethods.BitBlt(dc1, 0, 0, Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, dc2, 0, 0, 13369376)
gr1.ReleaseHdc(dc1)
myImage.Save('screenshot.jpg', ImageFormat.Jpeg)
End Sub ’SaveScreen
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);
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
}
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.