private void DrawTrackBar(IntPtr hWnd) { ControlState state = ControlState.Normal; bool horizontal = base.Orientation == Orientation.Horizontal; ImageDc tempDc = new ImageDc(base.Width, base.Height); RECT trackRect = new RECT(); RECT thumbRect = new RECT(); Graphics g = Graphics.FromHdc(tempDc.Hdc); NativeMethods.SendMessage( hWnd, TBM.TBM_GETCHANNELRECT, 0, ref trackRect); NativeMethods.SendMessage( hWnd, TBM.TBM_GETTHUMBRECT, 0, ref thumbRect); Rectangle trackRectangle = horizontal ? trackRect.Rect : Rectangle.FromLTRB( trackRect.Top, trackRect.Left, trackRect.Bottom, trackRect.Right); if (ThumbHovering(thumbRect)) { if (Helper.LeftKeyPressed()) { state = ControlState.Pressed; } else { state = ControlState.Hover; } } using (PaintEventArgs pe = new PaintEventArgs( g, ClientRectangle)) { OnRenderBackground(pe); } int ticks = NativeMethods.SendMessage( hWnd, TBM.TBM_GETNUMTICS, 0, 0); if (ticks > 0) { List <float> tickPosList = new List <float>(ticks); int thumbOffset = horizontal ? thumbRect.Rect.Width : thumbRect.Rect.Height; int trackWidth = trackRect.Right - trackRect.Left; float tickSpace = (trackWidth - thumbOffset) / (float)(ticks - 1); float offset = trackRect.Left + thumbOffset / 2f; for (int pos = 0; pos < ticks; pos++) { tickPosList.Add(offset + tickSpace * pos); } using (PaintTickEventArgs pte = new PaintTickEventArgs( g, trackRectangle, tickPosList)) { OnRenderTick(pte); } } using (PaintEventArgs pe = new PaintEventArgs( g, trackRectangle)) { OnRenderTrack(pe); } using (PaintThumbEventArgs pe = new PaintThumbEventArgs( g, thumbRect.Rect, state)) { OnRenderThumb(pe); } g.Dispose(); IntPtr hDC = NativeMethods.GetDC(hWnd); NativeMethods.BitBlt( hDC, 0, 0, base.Width, base.Height, tempDc.Hdc, 0, 0, 0xCC0020); NativeMethods.ReleaseDC(hWnd, hDC); tempDc.Dispose(); }
protected virtual void OnRenderThumb(PaintThumbEventArgs e) { Graphics g = e.Graphics; Rectangle rect = e.ClipRectangle; ControlState state = e.ControlState; ThumbArrowDirection direction = ThumbArrowDirection.None; Color begin = ColorTable.ThumbBackNormal; Color end = ColorTable.TrackInnerBorder; Color border = ColorTable.ThumbBorderNormal; float mode = base.Orientation == Orientation.Horizontal ? 90f : 0f; switch (base.Orientation) { case Orientation.Horizontal: switch (base.TickStyle) { case TickStyle.None: case TickStyle.BottomRight: direction = ThumbArrowDirection.Down; break; case TickStyle.TopLeft: direction = ThumbArrowDirection.Up; break; case TickStyle.Both: direction = ThumbArrowDirection.None; break; } break; case Orientation.Vertical: switch (base.TickStyle) { case TickStyle.TopLeft: direction = ThumbArrowDirection.Left; break; case TickStyle.None: case TickStyle.BottomRight: direction = ThumbArrowDirection.Right; break; case TickStyle.Both: direction = ThumbArrowDirection.None; break; } break; } switch (state) { case ControlState.Hover: begin = ColorTable.ThumbBackHover; border = ColorTable.ThumbBorderHover; break; } using (SmoothingModeGraphics sg = new SmoothingModeGraphics(g)) { using (GraphicsPath path = GraphicsPathHelper.CreateTrackBarThumbPath( rect, direction)) { using (LinearGradientBrush brush = new LinearGradientBrush( rect, begin, end, mode)) { Blend blend = new Blend(); blend.Positions = new float[] { 0, .2f, .5f, .8f, 1f }; blend.Factors = new float[] { 1f, .7f, 0, .7f, 1f }; brush.Blend = blend; g.FillPath(brush, path); } using (Pen pen = new Pen(border)) { g.DrawPath(pen, path); } } rect.Inflate(-1, -1); using (GraphicsPath path = GraphicsPathHelper.CreateTrackBarThumbPath( rect, direction)) { using (Pen pen = new Pen(ColorTable.TrackInnerBorder)) { g.DrawPath(pen, path); } } } }