123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499 |
- using LYFZ.OtherExpansion.SkinClass;
- using LYFZ.OtherExpansion.Win32;
- using LYFZ.OtherExpansion.Win32.Struct;
- using System;
- using System.Drawing;
- using System.Runtime.InteropServices;
- using System.Windows.Forms;
- namespace LYFZ.OtherExpansion.SkinControl
- {
- internal class ScrollBarManager : NativeWindow, IDisposable
- {
- private class ScrollBarMaskControl : MaskControlBase
- {
- private ScrollBarManager _owner;
- public ScrollBarMaskControl(ScrollBarManager owner) : base(owner.OwnerHWnd)
- {
- this._owner = owner;
- }
- protected override void OnPaint(IntPtr hWnd)
- {
- this._owner.DrawScrollBar(this._owner.OwnerHWnd, hWnd);
- }
- protected override void Dispose(bool disposing)
- {
- if (disposing)
- {
- this._owner = null;
- }
- base.Dispose(disposing);
- }
- }
- private bool _bPainting;
- private ScrollBar _owner;
- private ScrollBarManager.ScrollBarMaskControl _maskControl;
- private ScrollBarHistTest _lastMouseDownHistTest;
- private bool _disposed;
- private IntPtr OwnerHWnd
- {
- get
- {
- return this._owner.Handle;
- }
- }
- private Orientation Direction
- {
- get
- {
- if (this._owner is HScrollBar)
- {
- return Orientation.Horizontal;
- }
- return Orientation.Vertical;
- }
- }
- private int ArrowCx
- {
- get
- {
- return SystemInformation.HorizontalScrollBarArrowWidth;
- }
- }
- private int ArrowCy
- {
- get
- {
- return SystemInformation.VerticalScrollBarArrowHeight;
- }
- }
- internal ScrollBarManager(ScrollBar owner)
- {
- this._owner = owner;
- this.CreateHandle();
- }
- ~ScrollBarManager()
- {
- this.Dispose(false);
- }
- protected override void WndProc(ref Message m)
- {
- try
- {
- int msg = m.Msg;
- if (msg <= 125)
- {
- if (msg != 15)
- {
- if (msg == 71)
- {
- WINDOWPOS pos = (WINDOWPOS)Marshal.PtrToStructure(m.LParam, typeof(WINDOWPOS));
- bool hide = ((long)pos.flags & 128L) != 0L;
- bool show = ((long)pos.flags & 64L) != 0L;
- if (hide)
- {
- this._maskControl.SetVisibale(false);
- }
- else
- {
- if (show)
- {
- this._maskControl.SetVisibale(true);
- }
- }
- this._maskControl.CheckBounds(m.HWnd);
- base.WndProc(ref m);
- goto IL_22D;
- }
- if (msg == 125)
- {
- this.DrawScrollBar(m.HWnd, this._maskControl.Handle, false, true);
- base.WndProc(ref m);
- goto IL_22D;
- }
- }
- else
- {
- if (!this._bPainting)
- {
- PAINTSTRUCT ps = default(PAINTSTRUCT);
- this._bPainting = true;
- NativeMethods.BeginPaint(m.HWnd, ref ps);
- this.DrawScrollBar(m.HWnd, this._maskControl.Handle);
- NativeMethods.ValidateRect(m.HWnd, ref ps.rcPaint);
- NativeMethods.EndPaint(m.HWnd, ref ps);
- this._bPainting = false;
- m.Result = Result.TRUE;
- goto IL_22D;
- }
- base.WndProc(ref m);
- goto IL_22D;
- }
- }
- else
- {
- if (msg == 233)
- {
- this.DrawScrollBar(m.HWnd, this._maskControl.Handle, true, false);
- base.WndProc(ref m);
- goto IL_22D;
- }
- switch (msg)
- {
- case 512:
- case 514:
- this.DrawScrollBar(m.HWnd, this._maskControl.Handle);
- base.WndProc(ref m);
- goto IL_22D;
- case 513:
- this._lastMouseDownHistTest = this.ScrollBarHitTest(m.HWnd);
- this.DrawScrollBar(m.HWnd, this._maskControl.Handle);
- base.WndProc(ref m);
- goto IL_22D;
- default:
- if (msg == 675)
- {
- this.DrawScrollBar(m.HWnd, this._maskControl.Handle);
- base.WndProc(ref m);
- goto IL_22D;
- }
- break;
- }
- }
- base.WndProc(ref m);
- IL_22D:;
- }
- catch
- {
- }
- }
- private void DrawScrollBar(IntPtr scrollBarHWnd, IntPtr maskHWnd)
- {
- this.DrawScrollBar(scrollBarHWnd, maskHWnd, false, false);
- }
- private void DrawScrollBar(IntPtr scrollBarHWnd, IntPtr maskHWnd, bool sbm, bool styleChanged)
- {
- Orientation direction = this.Direction;
- bool bHorizontal = direction == Orientation.Horizontal;
- Rectangle bounds;
- Rectangle trackRect;
- Rectangle topLeftArrowRect;
- Rectangle bottomRightArrowRect;
- Rectangle thumbRect;
- this.CalculateRect(scrollBarHWnd, bHorizontal, out bounds, out trackRect, out topLeftArrowRect, out bottomRightArrowRect, out thumbRect);
- ScrollBarHistTest histTest;
- ControlState topLeftArrowState;
- ControlState bottomRightArrowState;
- ControlState thumbState;
- this.GetState(scrollBarHWnd, bHorizontal, out histTest, out topLeftArrowState, out bottomRightArrowState, out thumbState);
- if (sbm)
- {
- if (histTest == ScrollBarHistTest.None)
- {
- thumbState = ControlState.Pressed;
- }
- else
- {
- if (this._lastMouseDownHistTest == ScrollBarHistTest.Track)
- {
- thumbState = ControlState.Normal;
- }
- }
- }
- if (styleChanged)
- {
- thumbState = ControlState.Normal;
- }
- this.DrawScrollBar(maskHWnd, bounds, trackRect, topLeftArrowRect, bottomRightArrowRect, thumbRect, topLeftArrowState, bottomRightArrowState, thumbState, direction);
- }
- private void DrawScrollBar(ControlState topLeftArrowState, ControlState bottomRightArrowState, ControlState thumbState)
- {
- Orientation direction = this.Direction;
- bool bHorizontal = direction == Orientation.Horizontal;
- Rectangle bounds;
- Rectangle trackRect;
- Rectangle topLeftArrowRect;
- Rectangle bottomRightArrowRect;
- Rectangle thumbRect;
- this.CalculateRect(this.OwnerHWnd, bHorizontal, out bounds, out trackRect, out topLeftArrowRect, out bottomRightArrowRect, out thumbRect);
- this.DrawScrollBar(this._maskControl.Handle, bounds, trackRect, topLeftArrowRect, bottomRightArrowRect, thumbRect, topLeftArrowState, bottomRightArrowState, thumbState, direction);
- }
- private void DrawScrollBar(IntPtr maskHWnd, Rectangle bounds, Rectangle trackRect, Rectangle topLeftArrowRect, Rectangle bottomRightArrowRect, Rectangle thumbRect, ControlState topLeftArrowState, ControlState bottomRightArrowState, ControlState thumbState, Orientation direction)
- {
- bool bHorizontal = direction == Orientation.Horizontal;
- bool bEnabled = this._owner.Enabled;
- IScrollBarPaint paint = this._owner as IScrollBarPaint;
- if (paint == null)
- {
- return;
- }
- ImageDc tempDc = new ImageDc(bounds.Width, bounds.Height);
- IntPtr hdc = NativeMethods.GetDC(maskHWnd);
- try
- {
- using (Graphics g = Graphics.FromHdc(tempDc.Hdc))
- {
- using (PaintScrollBarTrackEventArgs te = new PaintScrollBarTrackEventArgs(g, trackRect, direction, bEnabled))
- {
- paint.OnPaintScrollBarTrack(te);
- }
- ArrowDirection arrowDirection = bHorizontal ? ArrowDirection.Left : ArrowDirection.Up;
- using (PaintScrollBarArrowEventArgs te2 = new PaintScrollBarArrowEventArgs(g, topLeftArrowRect, topLeftArrowState, arrowDirection, direction, bEnabled))
- {
- paint.OnPaintScrollBarArrow(te2);
- }
- arrowDirection = (bHorizontal ? ArrowDirection.Right : ArrowDirection.Down);
- using (PaintScrollBarArrowEventArgs te3 = new PaintScrollBarArrowEventArgs(g, bottomRightArrowRect, bottomRightArrowState, arrowDirection, direction, bEnabled))
- {
- paint.OnPaintScrollBarArrow(te3);
- }
- using (PaintScrollBarThumbEventArgs te4 = new PaintScrollBarThumbEventArgs(g, thumbRect, thumbState, direction, bEnabled))
- {
- paint.OnPaintScrollBarThumb(te4);
- }
- }
- NativeMethods.BitBlt(hdc, 0, 0, bounds.Width, bounds.Height, tempDc.Hdc, 0, 0, 13369376);
- }
- finally
- {
- NativeMethods.ReleaseDC(maskHWnd, hdc);
- tempDc.Dispose();
- }
- }
- private void CalculateRect(IntPtr scrollBarHWnd, bool bHorizontal, out Rectangle bounds, out Rectangle trackRect, out Rectangle topLeftArrowRect, out Rectangle bottomRightArrowRect, out Rectangle thumbRect)
- {
- RECT rect = default(RECT);
- NativeMethods.GetWindowRect(scrollBarHWnd, ref rect);
- NativeMethods.OffsetRect(ref rect, -rect.Left, -rect.Top);
- int arrowWidth = bHorizontal ? this.ArrowCx : this.ArrowCy;
- bounds = rect.Rect;
- Point point = this.GetScrollBarThumb(bounds, bHorizontal, arrowWidth);
- trackRect = bounds;
- if (!bHorizontal)
- {
- topLeftArrowRect = new Rectangle(0, 0, bounds.Width, arrowWidth);
- bottomRightArrowRect = new Rectangle(0, bounds.Height - arrowWidth, bounds.Width, arrowWidth);
- thumbRect = new Rectangle(0, point.X, bounds.Width, point.Y - point.X);
- return;
- }
- topLeftArrowRect = new Rectangle(0, 0, arrowWidth, bounds.Height);
- bottomRightArrowRect = new Rectangle(bounds.Width - arrowWidth, 0, arrowWidth, bounds.Height);
- if (this._owner.RightToLeft == RightToLeft.Yes)
- {
- thumbRect = new Rectangle(point.Y, 0, point.X - point.Y, bounds.Height);
- return;
- }
- thumbRect = new Rectangle(point.X, 0, point.Y - point.X, bounds.Height);
- }
- private void GetState(IntPtr scrollBarHWnd, bool bHorizontal, out ScrollBarHistTest histTest, out ControlState topLeftArrowState, out ControlState bottomRightArrowState, out ControlState thumbState)
- {
- histTest = this.ScrollBarHitTest(scrollBarHWnd);
- bool bLButtonDown = Win32Helper.LeftKeyPressed();
- bool bEnabled = this._owner.Enabled;
- topLeftArrowState = ControlState.Normal;
- bottomRightArrowState = ControlState.Normal;
- thumbState = ControlState.Normal;
- switch (histTest)
- {
- case ScrollBarHistTest.TopArrow:
- case ScrollBarHistTest.LeftArrow:
- if (bEnabled)
- {
- topLeftArrowState = (bLButtonDown ? ControlState.Pressed : ControlState.Hover);
- return;
- }
- break;
- case ScrollBarHistTest.BottomArrow:
- case ScrollBarHistTest.RightArrow:
- if (bEnabled)
- {
- bottomRightArrowState = (bLButtonDown ? ControlState.Pressed : ControlState.Hover);
- return;
- }
- break;
- case ScrollBarHistTest.Thumb:
- if (bEnabled)
- {
- thumbState = (bLButtonDown ? ControlState.Pressed : ControlState.Hover);
- }
- break;
- default:
- return;
- }
- }
- protected void CreateHandle()
- {
- base.AssignHandle(this.OwnerHWnd);
- this._maskControl = new ScrollBarManager.ScrollBarMaskControl(this);
- this._maskControl.OnCreateHandle();
- }
- internal void ReleaseHandleInternal()
- {
- if (base.Handle != IntPtr.Zero)
- {
- base.ReleaseHandle();
- }
- }
- private SCROLLBARINFO GetScrollBarInfo(IntPtr hWnd)
- {
- SCROLLBARINFO sbi = default(SCROLLBARINFO);
- sbi.cbSize = Marshal.SizeOf(sbi);
- NativeMethods.SendMessage(hWnd, 235, 0, ref sbi);
- return sbi;
- }
- private SCROLLBARINFO GetScrollBarInfo(IntPtr hWnd, uint objid)
- {
- SCROLLBARINFO sbi = default(SCROLLBARINFO);
- sbi.cbSize = Marshal.SizeOf(sbi);
- NativeMethods.GetScrollBarInfo(hWnd, objid, ref sbi);
- return sbi;
- }
- private Point GetScrollBarThumb()
- {
- bool bHorizontal = this.Direction == Orientation.Horizontal;
- int arrowWidth = bHorizontal ? this.ArrowCx : this.ArrowCy;
- return this.GetScrollBarThumb(this._owner.ClientRectangle, bHorizontal, arrowWidth);
- }
- private Point GetScrollBarThumb(Rectangle rect, bool bHorizontal, int arrowWidth)
- {
- ScrollBar scrollBar = this._owner;
- Point point = default(Point);
- int width;
- if (bHorizontal)
- {
- width = rect.Width - arrowWidth * 2;
- }
- else
- {
- width = rect.Height - arrowWidth * 2;
- }
- int value = scrollBar.Maximum - scrollBar.Minimum - scrollBar.LargeChange + 1;
- float thumbWidth = (float)width / ((float)value / (float)scrollBar.LargeChange + 1f);
- if (thumbWidth < 8f)
- {
- thumbWidth = 8f;
- }
- if (value != 0)
- {
- int curValue = scrollBar.Value - scrollBar.Minimum;
- if (curValue > value)
- {
- curValue = value;
- }
- point.X = (int)((float)curValue * (((float)width - thumbWidth) / (float)value));
- }
- point.X += arrowWidth;
- point.Y = point.X + (int)Math.Ceiling((double)thumbWidth);
- if (bHorizontal && scrollBar.RightToLeft == RightToLeft.Yes)
- {
- point.X = scrollBar.Width - point.X;
- point.Y = scrollBar.Width - point.Y;
- }
- return point;
- }
- private ScrollBarHistTest ScrollBarHitTest(IntPtr hWnd)
- {
- NativeMethods.Point point = default(NativeMethods.Point);
- RECT rect = default(RECT);
- Point thumbPoint = this.GetScrollBarThumb();
- int arrowCx = this.ArrowCx;
- int arrowCy = this.ArrowCy;
- NativeMethods.GetWindowRect(hWnd, ref rect);
- NativeMethods.OffsetRect(ref rect, -rect.Left, -rect.Top);
- RECT tp = rect;
- NativeMethods.GetCursorPos(ref point);
- NativeMethods.ScreenToClient(hWnd, ref point);
- if (this.Direction == Orientation.Horizontal)
- {
- if (NativeMethods.PtInRect(ref rect, point))
- {
- tp.Right = arrowCx;
- if (NativeMethods.PtInRect(ref tp, point))
- {
- return ScrollBarHistTest.LeftArrow;
- }
- tp.Left = rect.Right - arrowCx;
- tp.Right = rect.Right;
- if (NativeMethods.PtInRect(ref tp, point))
- {
- return ScrollBarHistTest.RightArrow;
- }
- if (this._owner.RightToLeft == RightToLeft.Yes)
- {
- tp.Left = point.y;
- tp.Right = point.x;
- }
- else
- {
- tp.Left = thumbPoint.X;
- tp.Right = thumbPoint.Y;
- }
- if (NativeMethods.PtInRect(ref tp, point))
- {
- return ScrollBarHistTest.Thumb;
- }
- return ScrollBarHistTest.Track;
- }
- }
- else
- {
- if (NativeMethods.PtInRect(ref rect, point))
- {
- tp.Bottom = arrowCy;
- if (NativeMethods.PtInRect(ref tp, point))
- {
- return ScrollBarHistTest.TopArrow;
- }
- tp.Top = rect.Bottom - arrowCy;
- tp.Bottom = rect.Bottom;
- if (NativeMethods.PtInRect(ref tp, point))
- {
- return ScrollBarHistTest.BottomArrow;
- }
- tp.Top = thumbPoint.X;
- tp.Bottom = thumbPoint.Y;
- if (NativeMethods.PtInRect(ref tp, point))
- {
- return ScrollBarHistTest.Thumb;
- }
- return ScrollBarHistTest.Track;
- }
- }
- return ScrollBarHistTest.None;
- }
- private void InvalidateWindow(bool messaged)
- {
- this.InvalidateWindow(this.OwnerHWnd, messaged);
- }
- private void InvalidateWindow(IntPtr hWnd, bool messaged)
- {
- if (messaged)
- {
- NativeMethods.RedrawWindow(hWnd, IntPtr.Zero, IntPtr.Zero, 2);
- return;
- }
- NativeMethods.RedrawWindow(hWnd, IntPtr.Zero, IntPtr.Zero, 257);
- }
- public void Dispose()
- {
- this.Dispose(true);
- GC.SuppressFinalize(this);
- }
- protected virtual void Dispose(bool disposing)
- {
- if (!this._disposed)
- {
- if (disposing)
- {
- if (this._maskControl != null)
- {
- this._maskControl.Dispose();
- this._maskControl = null;
- }
- this._owner = null;
- }
- this.ReleaseHandleInternal();
- }
- this._disposed = true;
- }
- }
- }
|