Working with the Window ObjectThe Window object represents an Excel window. You can use the Window object to position a window associated with a workbook. You can also use the Window object to set display settings for a workbook such as whether to display gridlines and headings. Positioning a WindowThe Window object lets you position and change the way Excel displays a workbook within a window. Window has a WindowState property of type XlWindowState that can be used to set the window to xlMaximized, xlMinimized, or xlNormal. When the WindowState is set to xlNormal, you can position the window using the Left, Top, Width, and Height properties. These properties are double values that represent points, not screen pixels. You can use the Window's PointsToScreenPixelsX and PointsToScreenPixelsY methods to convert points to pixels. Display Settings Associated with a WindowA number of additional properties allow you to control the display of a window. Table 5-12 lists some of the most commonly used ones.
Listing 5-21 shows an example of using many of these properties. Note that we add a reference to System.Drawing.dll so that we can use the ColorTranslator object to set the GridlineColor property. The ColorTranslator object provides a method called ToOle, which takes a System.Drawing color and converts it to an Ole color formatthe kind of color format that Office methods and properties that take colors expect. Listing 5-21. A VSTO Customization That Controls the Display Options for a Windowprivate void ThisWorkbook_Startup(object sender, EventArgs e) { Excel.Window win = this.NewWindow(); win.WindowState = Excel.XlWindowState.xlNormal; win.Width = 200; win.Height = 200; win.Top = 8; win.Left = 8; win.DisplayGridlines = true; win.DisplayHeadings = false; win.DisplayHorizontalScrollBar = false; win.DisplayVerticalScrollBar = false; win.DisplayWorkbookTabs = false; win.EnableResize = false; win.GridlineColor = System.Drawing.ColorTranslator. ToOle(System.Drawing.Color.Blue); win.ScrollColumn = 10; win.ScrollRow = 20; win.Visible = true; win.Zoom = 150; } ![]() |