News:

MyKidsDiary.in :: Capture your kids magical moment and create your Online Private Diary for your kids

Main Menu

Add a Size-to-Fit Option

Started by thiruvasagamani, Mar 14, 2009, 08:26 PM

Previous topic - Next topic

thiruvasagamani

Add a Size-to-Fit Option
      Adding scroll bars to the form is one way to handle large images. Another option is to ignore the physical size of the image and render it so that it fits the form exactly. Let's let the user decide which way to handle this by adding a pair of commands to the Options menu. One command, Size Image to Fit Window, will fit the image to the form. The other, Show Image in Native Size, will display images in their native resolution, but provide scroll bars (if needed) to permit viewing of large images.
      Figure 9 shows how to go about it. First you add a field (_NativeSize) to store the user's preference. Next, you modify MyForm's constructor to add two new menu items (three if you count the extra separator bar). Third, you add command handlers that set _NativeSize to true or false, turn scrolling on or off, and call Invalidate to force a repaint. Finally, you modify OnPaint to match the size of the rendered image to the form size if _NativeSize is false. Matching the image size to the form size is a simple matter of passing a Rectangle containing the dimensions of the form's client area in DrawImage's second parameter. That Rectangle is readily available as a System.WinForms.Form property named ClientRectangle.
      The only new code in Figure 9 that might raise an eyebrow is a pair of calls to SetStyle. Here's why I added them. By default, a resizing operation doesn't invalidate the entire form; it only invalidates the portion of the form exposed by the resizing operation. That's OK if you're not scaling the image to fit the form size, but if you are, then you want the entire client area to be invalidated when its size changes so the entire form will be repainted. In Windows, you'd accomplish that by including CS_HREDRAW and CS_ VREDRAW flags in the WNDCLASS style. In Windows Forms, you do it by calling the form's SetStyle method with a ControlStyles.ResizeRedraw parameter. ControlStyles is an enumeration defined in System. WinForms; ResizeRedraw is a member of that enumeration.

Source : MSDN
Thiruvasakamani Karnan