Пример #1
0
        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();

            System.Drawing.Graphics g = System.Drawing.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();
        }
Пример #2
0
        protected virtual void OnRenderTick(PaintTickEventArgs e)
        {
            System.Drawing.Graphics g = e.Graphics;
            Rectangle trackRect = e.TrackRect;
            bool bHorizontal = base.Orientation == Orientation.Horizontal;
            int posFirst = 0;
            int posSecond = 0;
            bool bTickBoth = base.TickStyle == TickStyle.Both;

            if (bHorizontal)
            {
                switch (base.TickStyle)
                {
                    case TickStyle.TopLeft:
                        posFirst = trackRect.Top - 15;
                        break;
                    case TickStyle.BottomRight:
                        posFirst = trackRect.Bottom + 13;
                        break;
                    case TickStyle.Both:
                        posFirst = trackRect.Top - 15;
                        posSecond = trackRect.Bottom + 13;
                        break;
                }
            }
            else
            {
                switch (base.TickStyle)
                {
                    case TickStyle.TopLeft:
                        posFirst = trackRect.Left - 15;
                        break;
                    case TickStyle.BottomRight:
                        posFirst = trackRect.Right + 13;
                        break;
                    case TickStyle.Both:
                        posFirst = trackRect.Left - 15;
                        posSecond = trackRect.Right + 13;
                        break;
                }
            }

            Pen lightPen = new Pen(ColorTable.TickLight);
            Pen darkPen = new Pen(ColorTable.TickDark);

            if (bHorizontal)
            {
                foreach (int tickPos in e.TickPosList)
                {
                    g.DrawLine(
                        lightPen, new Point(tickPos, posFirst),
                        new Point(tickPos, posFirst + 2));
                    g.DrawLine(
                        darkPen, new Point(tickPos + 1, posFirst),
                        new Point(tickPos + 1, posFirst + 2));
                    if (bTickBoth)
                    {
                        g.DrawLine(
                            lightPen, new Point(tickPos, posSecond),
                            new Point(tickPos, posSecond + 2));
                        g.DrawLine(
                            darkPen, new Point(tickPos + 1, posSecond),
                            new Point(tickPos + 1, posSecond + 2));
                    }
                }
            }
            else
            {
                foreach (int tickPos in e.TickPosList)
                {
                    g.DrawLine(
                        lightPen, new Point(posFirst + 2, tickPos),
                        new Point(posFirst, tickPos));
                    g.DrawLine(
                        darkPen, new Point(posFirst, tickPos + 1),
                        new Point(posFirst + 2, tickPos + 1));

                    if (bTickBoth)
                    {
                        g.DrawLine(
                            lightPen, new Point(posSecond + 2, tickPos),
                            new Point(posSecond, tickPos));
                        g.DrawLine(
                            darkPen, new Point(posSecond, tickPos + 1),
                            new Point(posSecond + 2, tickPos + 1));
                    }
                }
            }

            lightPen.Dispose();
            darkPen.Dispose();
        }