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); } } }
private void DrawTabPagesMouseMove(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);//写入text if (this.SelectedIndex == index) { if (this.TabCount != 1) { if (ShowClose) { ////再画一个矩形框 using (Pen p = new Pen(Color.White)) { tabRect.Offset(tabRect.Width - (CLOSE_SIZE + 3), 5); tabRect.Width = CLOSE_SIZE; tabRect.Height = CLOSE_SIZE; g.DrawRectangle(p, tabRect); } //画关闭符号 using (Pen p = new Pen(Color.Brown, 2)) //鼠标指向关闭键时,颜色Color.Brown为早红 { //画"\"线 Point p1 = new Point(tabRect.X + 3, tabRect.Y + 3); Point p2 = new Point(tabRect.X + tabRect.Width - 3, tabRect.Y + tabRect.Height - 3); g.DrawLine(p, p1, p2); //画"/"线 Point p3 = new Point(tabRect.X + 3, tabRect.Y + tabRect.Height - 3); Point p4 = new Point(tabRect.X + tabRect.Width - 3, tabRect.Y + 3); g.DrawLine(p, p3, p4); } } } } // DrawtabText(g, page, tabRect, hasImage);放在这里不行 } if (hasSetClip) { g.ResetClip(); } }