private void DrawTabPages(Graphics g) { Rectangle tabRect; Point cusorPoint = PointToClient(MousePosition); bool hover; bool selected; bool hasSetClip = false; bool alignHorizontal = (Alignment == TabAlignment.Top || Alignment == TabAlignment.Bottom); LinearGradientMode mode = alignHorizontal ? LinearGradientMode.Vertical : LinearGradientMode.Horizontal; if (alignHorizontal) { IntPtr upDownButtonHandle = UpDownButtonHandle; bool hasUpDown = upDownButtonHandle != IntPtr.Zero; if (hasUpDown) { if (NativeMethods.IsWindowVisible(upDownButtonHandle)) { NativeMethods.RECT upDownButtonRect = new NativeMethods.RECT(); NativeMethods.GetWindowRect( upDownButtonHandle, ref upDownButtonRect); Rectangle upDownRect = Rectangle.FromLTRB( upDownButtonRect.Left, upDownButtonRect.Top, upDownButtonRect.Right, upDownButtonRect.Bottom); upDownRect = RectangleToClient(upDownRect); switch (Alignment) { case TabAlignment.Top: upDownRect.Y = 0; break; case TabAlignment.Bottom: upDownRect.Y = ClientRectangle.Height - DisplayRectangle.Height; break; } upDownRect.Height = ClientRectangle.Height; g.SetClip(upDownRect, CombineMode.Exclude); hasSetClip = true; } } } for (int index = 0; index < base.TabCount; index++) { TabPage page = TabPages[index]; tabRect = GetTabRect(index); hover = tabRect.Contains(cusorPoint); selected = SelectedIndex == index; Color baseColor = _baseColor; Color borderColor = _borderColor; if (selected) { baseColor = GetColor(_baseColor, 0, -45, -30, -14); } else if (hover) { baseColor = GetColor(_baseColor, 0, 35, 24, 9); } RenderTabBackgroundInternal( g, tabRect, baseColor, borderColor, .45F, mode); bool hasImage = DrawTabImage(g, page, tabRect); DrawtabText(g, page, tabRect, hasImage); } if (hasSetClip) { g.ResetClip(); } }
private void DrawBorder(Graphics g) { if (SelectedIndex != -1) { Rectangle tabRect = GetTabRect(SelectedIndex); Rectangle clipRect = ClientRectangle; Point[] points = new Point[6]; IntPtr upDownButtonHandle = UpDownButtonHandle; bool hasUpDown = upDownButtonHandle != IntPtr.Zero; if (hasUpDown) { if (NativeMethods.IsWindowVisible(upDownButtonHandle)) { NativeMethods.RECT upDownButtonRect = new NativeMethods.RECT(); NativeMethods.GetWindowRect( upDownButtonHandle, ref upDownButtonRect); Rectangle upDownRect = Rectangle.FromLTRB( upDownButtonRect.Left, upDownButtonRect.Top, upDownButtonRect.Right, upDownButtonRect.Bottom); upDownRect = RectangleToClient(upDownRect); tabRect.X = tabRect.X > upDownRect.X ? upDownRect.X : tabRect.X; tabRect.Width = tabRect.Right > upDownRect.X ? upDownRect.X - tabRect.X : tabRect.Width; } } switch (Alignment) { case TabAlignment.Top: points[0] = new Point( tabRect.X, tabRect.Bottom); points[1] = new Point( clipRect.X, tabRect.Bottom); points[2] = new Point( clipRect.X, clipRect.Bottom - 1); points[3] = new Point( clipRect.Right - 1, clipRect.Bottom - 1); points[4] = new Point( clipRect.Right - 1, tabRect.Bottom); points[5] = new Point( tabRect.Right, tabRect.Bottom); break; case TabAlignment.Bottom: points[0] = new Point( tabRect.X, tabRect.Y); points[1] = new Point( clipRect.X, tabRect.Y); points[2] = new Point( clipRect.X, clipRect.Y); points[3] = new Point( clipRect.Right - 1, clipRect.Y); points[4] = new Point( clipRect.Right - 1, tabRect.Y); points[5] = new Point( tabRect.Right, tabRect.Y); break; case TabAlignment.Left: points[0] = new Point( tabRect.Right, tabRect.Y); points[1] = new Point( tabRect.Right, clipRect.Y); points[2] = new Point( clipRect.Right - 1, clipRect.Y); points[3] = new Point( clipRect.Right - 1, clipRect.Bottom - 1); points[4] = new Point( tabRect.Right, clipRect.Bottom - 1); points[5] = new Point( tabRect.Right, tabRect.Bottom); break; case TabAlignment.Right: points[0] = new Point( tabRect.X, tabRect.Y); points[1] = new Point( tabRect.X, clipRect.Y); points[2] = new Point( clipRect.X, clipRect.Y); points[3] = new Point( clipRect.X, clipRect.Bottom - 1); points[4] = new Point( tabRect.X, clipRect.Bottom - 1); points[5] = new Point( tabRect.X, tabRect.Bottom); break; } using (Pen pen = new Pen(_borderColor)) { g.DrawLines(pen, points); } } }
/// <summary> /// 这个是描绘tab page的方法,我尝试一下描绘其他东西 /// </summary> /// <param name="g"></param> private void DrawTabPages(Graphics g) { Rectangle tabRect; Point cusorPoint = PointToClient(MousePosition); bool hover; bool selected; bool hasSetClip = false; IntPtr upDownButtonHandle = UpDownButtonHandle; bool hasUpDown = upDownButtonHandle != IntPtr.Zero; if (hasUpDown) { if (NativeMethods.IsWindowVisible(upDownButtonHandle)) { NativeMethods.RECT upDownButtonRect = new NativeMethods.RECT(); NativeMethods.GetWindowRect(upDownButtonHandle, ref upDownButtonRect); Rectangle upDownRect = Rectangle.FromLTRB( upDownButtonRect.Left, upDownButtonRect.Top, upDownButtonRect.Right, upDownButtonRect.Bottom); upDownRect = RectangleToClient(upDownRect); switch (Alignment) { case TabAlignment.Top: upDownRect.Y = 0; break; case TabAlignment.Bottom: upDownRect.Y = ClientRectangle.Height - DisplayRectangle.Height; break; } upDownRect.Height = ClientRectangle.Height; g.SetClip(upDownRect, CombineMode.Exclude); hasSetClip = true; } } for (int index = 0; index < base.TabCount; index++) { TabPage page = TabPages[index]; tabRect = GetTabRect(index); hover = tabRect.Contains(cusorPoint); selected = SelectedIndex == index; Color baseColor = _baseColor; Color borderColor = _borderColor; if (selected) { baseColor = GetColor(_baseColor, 0, -45, -30, -14); } else if (hover) { baseColor = GetColor(_baseColor, 0, 35, 24, 9); } RenderTabBackgroundInternal( g, tabRect, baseColor, borderColor, .45F, LinearGradientMode.Vertical); bool hasImage = false; g.InterpolationMode = InterpolationMode.HighQualityBilinear; if (ImageList != null) { Image image = null; if (page.ImageIndex != -1) { image = ImageList.Images[page.ImageIndex]; } else if (page.ImageKey != null) { image = ImageList.Images[page.ImageKey]; } if (image != null) { hasImage = true; g.DrawImage( image, new Rectangle( tabRect.X + Radius / 2 + 2, tabRect.Y + 2, tabRect.Height - 4, tabRect.Height - 4), 0, 0, image.Width, image.Height, GraphicsUnit.Pixel); } } Rectangle textRect = tabRect; textRect.Width = tabRect.Width - 16; textRect.X = tabRect.X + 4; if (hasImage) { textRect.X = tabRect.X + Radius / 2 + tabRect.Height - 7; textRect.Width = tabRect.Width - Radius - tabRect.Height; } /* * 绘制关闭图标 * 标签为NoClosePageName的不绘制关闭按钮 */ if (page.Name != NoClosePageName) { Bitmap oImage = CSharpWin.Properties.Resources.close; Bitmap OImageSelected = CSharpWin.Properties.Resources.clear; // g.DrawImage(oImage,new PointF(textRect.X+20,textRect.Height-20)); Rectangle closeRect = new Rectangle(tabRect.X + tabRect.Width - 12 - 7, tabRect.Y + tabRect.Height - 12 - 6, 16, 16); //Rectangle closeRect = new Rectangle(tabRect.X + tabRect.Width - 12 - 9, tabRect.Y + tabRect.Height - 12 - 6, 16, 16); if (closeRect.Contains(cusorPoint)) { g.DrawImage(OImageSelected, closeRect, 0, 0, 16, 16, GraphicsUnit.Pixel); } else { g.DrawImage(oImage, closeRect, 0, 0, 16, 16, GraphicsUnit.Pixel); } } /*绘制结束*/ /* * 绘制文字 */ TextRenderer.DrawText( g, page.Text, page.Font, textRect, page.ForeColor); } if (hasSetClip) { g.ResetClip(); } }