/// ------------------------------------------------------------------------------------ protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); if (!Application.RenderWithVisualStyles) { ControlPaint.DrawBorder3D(e.Graphics, ClientRectangle, Border3DStyle.Sunken); } else { VisualStyleRenderer renderer = new VisualStyleRenderer(Enabled ? VisualStyleElement.TextBox.TextEdit.Normal : VisualStyleElement.TextBox.TextEdit.Disabled); renderer.DrawBackground(e.Graphics, ClientRectangle, e.ClipRectangle); // When the textbox background is drawn in normal mode (at least when the // theme is one of the standard XP themes), it's drawn with a white background // and not the System Window background color. Therefore, we need to create // a rectangle that doesn't include the border. Then fill it with the text // box's background color. Rectangle rc = renderer.GetBackgroundExtent(e.Graphics, ClientRectangle); int dx = (rc.Width - ClientRectangle.Width) / 2; int dy = (rc.Height - ClientRectangle.Height) / 2; rc = ClientRectangle; rc.Inflate(-dx, -dy); using (SolidBrush br = new SolidBrush(_textBox.BackColor)) e.Graphics.FillRectangle(br, rc); } }
/// <summary> /// Calculates the rectangles for the tab area, the client area, /// the display area, and the transformed display area. /// </summary> private void CalculateRectangles() { Rectangle olddisp = displayRectangle; positions.Clear(); Graphics g = this.CreateGraphics(); if (Controls.Count > 0) { int width = Width; width -= tabExpand.Horizontal; tabHeight = 0; rows = 0; int used = 0; int pos = 0; foreach (BpTabPage page in Controls) { VisualStyleElement element; VisualStyleRenderer renderer; Size closebutton = new Size(0, 0); if (IncludesCloseButton(page)) { element = VisualStyleElement.ToolTip.Close.Normal; if (Application.RenderWithVisualStyles && VisualStyleRenderer.IsElementDefined(element)) { renderer = new VisualStyleRenderer(element); closebutton = renderer.GetPartSize(g, ThemeSizeType.Draw); } else { Font closefont = new Font("Marlett", SystemFonts.MenuFont.Size); closebutton = TextRenderer.MeasureText("r", closefont); closebutton.Height += SystemInformation.Border3DSize.Height * 2; closebutton.Width += SystemInformation.Border3DSize.Width * 2; } } Size size = page.GetPreferredTabSize(g); size.Height = Math.Max(size.Height, closebutton.Height); size.Width += closebutton.Width; element = GetGuessedElement(page); if (Application.RenderWithVisualStyles && VisualStyleRenderer.IsElementDefined(element)) { renderer = new VisualStyleRenderer(element); Rectangle fake = new Rectangle(0, 0, size.Width, size.Height); fake = renderer.GetBackgroundExtent(g, fake); size = new Size(fake.Width, fake.Height); } size.Height += SystemInformation.Border3DSize.Height * 2; size.Width += SystemInformation.Border3DSize.Width * 2; if (size.Width > width) { size.Width = width; } if ((size.Width + used) > width) { rowCounts.Add(pos); rows++; used = 0; pos = 0; } TabPosition position = new TabPosition(rows, used, size, pos); positions[page] = position; tabHeight = Math.Max(tabHeight, size.Height); used += size.Width; pos++; if (page == selectedTab) { selectedRow = rows; } } rowCounts.Add(pos); rows++; tabsRectangle = new Rectangle(0, 0, Width, (rows * tabHeight) + tabExpand.Top); paneRectangle = new Rectangle(0, tabsRectangle.Bottom, Width, Height - tabsRectangle.Bottom); } else { paneRectangle = ClientRectangle; tabsRectangle = Rectangle.Empty; } if (Application.RenderWithVisualStyles && VisualStyleRenderer.IsElementDefined(VisualStyleElement.Tab.Pane.Normal)) { VisualStyleRenderer renderer = new VisualStyleRenderer(VisualStyleElement.Tab.Pane.Normal); displayRectangle = renderer.GetBackgroundContentRectangle(g, paneRectangle); } else { Padding paneMargin = new Padding(SystemInformation.Border3DSize.Width, SystemInformation.Border3DSize.Height, SystemInformation.Border3DSize.Width, SystemInformation.Border3DSize.Height); displayRectangle = Rectangle.Empty; displayRectangle.Y = paneRectangle.Y + paneMargin.Top; displayRectangle.Height = paneRectangle.Height - paneMargin.Vertical; displayRectangle.X = paneRectangle.X + paneMargin.Left; displayRectangle.Width = paneRectangle.Width - paneMargin.Horizontal; } if (olddisp != displayRectangle) { Invalidate(); PerformLayout(); Update(); } else { Rectangle badrect = tabsRectangle; badrect.Height += tabExpand.Bottom; Invalidate(badrect); Update(); } }
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e) { if (treeView1.SelectedNode != null) { Graphics g = Graphics.FromHwnd(this.Handle); g.Clear(this.BackColor); TextY = 0; if (VisualStyleInformation.IsSupportedByOS) { DrawText(g, "VisualStyles supported by OS"); } else { DrawText(g, "VisualStyles not supported by OS."); return; } if (VisualStyleInformation.IsEnabledByUser) { DrawText(g, "VisualStyles enabled by user."); } else { DrawText(g, "VisualStyles not enabled by user."); return; } // Create the VisualStyleElement MethodInfo m = (MethodInfo)treeView1.SelectedNode.Tag; VisualStyleElement vse = (VisualStyleElement)m.Invoke(null, null); if (!VisualStyleRenderer.IsElementDefined(vse)) { DrawText(g, treeView1.SelectedNode.Text + " is not defined by the current style."); return; } try { // Create the VisualStyleRenderer if (vsr == null) { vsr = new VisualStyleRenderer(vse); } else { vsr.SetParameters(vse); } // Draw some pretty graphics TextY += 25; vsr.DrawBackground(g, new Rectangle(250, TextY, 25, 25)); vsr.DrawBackground(g, new Rectangle(350, TextY, 50, 50)); vsr.DrawBackground(g, new Rectangle(450, TextY, 50, 50), new Rectangle(450, TextY, 25, 25)); TextY += 75; // Test some other methods DrawText(g, "GetBackgroundContentRectangle: " + vsr.GetBackgroundContentRectangle(g, new Rectangle(300, 0, 300, 50)).ToString()); DrawText(g, "GetBackgroundExtent: " + vsr.GetBackgroundExtent(g, new Rectangle(300, 0, 300, 50)).ToString()); DrawText(g, "GetBoolean: " + vsr.GetBoolean(BooleanProperty.MirrorImage).ToString()); DrawText(g, "GetEnumValue: " + vsr.GetEnumValue(EnumProperty.VerticalAlignment).ToString()); DrawText(g, "GetFilename: " + vsr.GetFilename(FilenameProperty.ImageFile).ToString()); DrawText(g, "GetInteger: " + vsr.GetInteger(IntegerProperty.BorderSize).ToString()); DrawText(g, "GetMargins: " + vsr.GetMargins(g, MarginProperty.CaptionMargins).ToString()); DrawText(g, "GetPartSize: " + vsr.GetPartSize(g, ThemeSizeType.Draw).ToString()); DrawText(g, "GetPoint: " + vsr.GetPoint(PointProperty.MinSize).ToString()); DrawText(g, "GetString: " + vsr.GetString(StringProperty.Text).ToString()); DrawText(g, "GetTextExtent: " + vsr.GetTextExtent(g, "HeyThere!", TextFormatFlags.Default).ToString()); DrawText(g, "GetTextMetrics: " + vsr.GetTextMetrics(g).Ascent.ToString()); DrawText(g, "GetBackgroundRegion: " + vsr.GetBackgroundRegion(g, this.ClientRectangle).GetBounds(g).ToString()); DrawText(g, "HitTestBackground: " + vsr.HitTestBackground(g, this.ClientRectangle, new Point(300, 300), HitTestOptions.Caption).ToString()); DrawText(g, "Author: " + VisualStyleInformation.Author); DrawText(g, "ColorScheme: " + VisualStyleInformation.ColorScheme); DrawText(g, "Company: " + VisualStyleInformation.Company); DrawText(g, "ControlHighlightHot: " + VisualStyleInformation.ControlHighlightHot.ToString()); DrawText(g, "Copyright: " + VisualStyleInformation.Copyright); DrawText(g, "Description: " + VisualStyleInformation.Description); DrawText(g, "DisplayName: " + VisualStyleInformation.DisplayName); DrawText(g, "MinimumColorDepth: " + VisualStyleInformation.MinimumColorDepth.ToString()); DrawText(g, "Size: " + VisualStyleInformation.Size.ToString()); DrawText(g, "SupportsFlatMenus: " + VisualStyleInformation.SupportsFlatMenus.ToString()); DrawText(g, "TextControlBorder: " + VisualStyleInformation.TextControlBorder.ToString()); DrawText(g, "Url: " + VisualStyleInformation.Url); DrawText(g, "Version: " + VisualStyleInformation.Version.ToString()); } catch (Exception ex) { System.Console.WriteLine(ex.ToString()); } } }