/// <summary> /// 绘制边框. /// </summary> /// <param name="g"></param> 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 RePaint() { ImageComboBoxItem item = _Owner.SelectedItem; var rcClient = new NativeMethods.RECT(); NativeMethods.GetClientRect(Handle, ref rcClient); bool rightToLeft = IsRightToLeft(Handle); IntPtr handle = Handle; IntPtr hdc = NativeMethods.GetDC(handle); if (hdc == IntPtr.Zero) { return; } try { using (Graphics g = Graphics.FromHdc(hdc)) { int itemSize = _Owner.ItemHeight; var imageRect = new Rectangle(0, rcClient.Top + (rcClient.Bottom - itemSize) / 2, itemSize, itemSize); var textRect = new Rectangle(0, 0, rcClient.Right - itemSize - 6, rcClient.Bottom); if (rightToLeft) { imageRect.X = rcClient.Right - itemSize - 2; textRect.X = 2; } else { imageRect.X = 2; textRect.X = imageRect.Right + 2; } if (_Owner.Text.Length == 0) { DrawImage(g, imageRect, _Owner.DefaultImage, _Owner.DefaultImageList, 0, _Owner.Focused); if (_Owner.Text.Length == 0 && !string.IsNullOrEmpty(_Owner.EmptyTextTip) && !_Owner.Focused) { TextFormatFlags format = TextFormatFlags.EndEllipsis | TextFormatFlags.VerticalCenter; if (_Owner.RightToLeft == RightToLeft.Yes) { format |= (TextFormatFlags.RightToLeft | TextFormatFlags.Right); } TextRenderer.DrawText(g, _Owner.EmptyTextTip, _Owner.Font, textRect, _Owner.EmptyTextTipColor, format); } return; } if (_Owner.Text.Length > 0) { using (var brush = new SolidBrush(_Owner.BackColor)) { g.FillRectangle(brush, imageRect); } } if (_Owner.Items.Count == 0) { DrawImage(g, imageRect, _Owner.DefaultImage, _Owner.DefaultImageList, 0, _Owner.Focused); return; } if (item == null) { return; } DrawImage(g, imageRect, item.Image, _Owner.ImageList, item.ImageIndexer.ActualIndex, _Owner.Focused); } } finally { NativeMethods.ReleaseDC(handle, hdc); } }
/// <summary> /// 绘制TabPages /// </summary> /// <param name="g"></param> 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); //绘制Text DrawtabText(g, page, tabRect, hasImage); } if (hasSetClip) { g.ResetClip(); } }
public static extern bool GetClientRect( IntPtr hWnd, [In, Out] ref NativeMethods.RECT rect);