17.9.4 drawing in the scrolling window
The size of the form is determined by the size property of the form, which includes the title bar and border of the form. The area of the form used to display customer documentation is called the workspace (ClientRectangle), and its size is represented by the form's ClientSize property (as shown in Figure 17-20).
In Figure 17-20, the form has a size of 308x234 pixels with a title bar of 30 pixels and a border of 4 pixels, so the size of the workspace is 300x200 pixels.
What happens if we want to display a 200x150 pixel rectangle and a 300x100 pixel ellipse in the 300x200 pixel workspace?
Figure 17-20 Drawing 17-21 Document size out of the workspace in the scrolling window
In order to describe the convenience, we have to display the text, graphics and other content called "document." Because the total height of the document is 250 pixels and the height of the window's workspace is only 200 pixels, there is always a part that cannot be displayed (as shown in Figure 17-21). If the document is too large for the workspace to be fully displayed, you need to add a scroll bar to the window to see the blocked part.
How can i display scroll bars? This can be done by setting the form's AutoScrollMinSize property.
This. AutoScrollMinSize = new Size (300, 250);
Because the document has an area of 300x250 pixels, we set the AutoScrollMinSize value to 300x250, and the form automatically displays the corresponding scroll bar when the area of the workspace is less than that value.
Create a new item named "ScrollWindow" with the form size set to 308x234 pixels (except for the title bar and border, the actual size of the workspace is 300x200 pixels), and then override the OnPaint () method.
Try it: drawing in a scrolling window
public partial class Form1:form
{
//constructor public
Form1 ()
{
InitializeComponent ();
Set the background color of the form to white this
. BackColor = Color.White;
Show scroll bar This when the workspace is less than 300x250 pixels
. AutoScrollMinSize = new Size (.)
Override the OnPaint () method
protected override void OnPaint (PaintEventArgs e)
{
base. OnPaint (e);
Graphics g = e.graphics;
Draw rectangles and elliptical
g.fillrectangle (brushes.lightpink, 0, 0, m);
G.fillellipse (brushes.lightgreen, 0, M, MB);
}