OSDN Git Service

DetailsListView.GetScrollInfo() メソッドと関連する列挙型などをNativeMethodsクラスに移動 (CA1060)
authorKimura Youichi <kim.upsilon@bucyou.net>
Sun, 13 Jul 2014 00:03:47 +0000 (09:03 +0900)
committerKimura Youichi <kim.upsilon@bucyou.net>
Mon, 21 Jul 2014 06:26:39 +0000 (15:26 +0900)
http://msdn.microsoft.com/ja-jp/library/ms182161.aspx

OpenTween/DetailsListView.cs
OpenTween/NativeMethods.cs

index 2b91f1a..d437b57 100644 (file)
@@ -223,44 +223,6 @@ namespace OpenTween.OpenTweenCustomControl
             }
         }
 
-        [StructLayout(LayoutKind.Sequential)]
-        private struct SCROLLINFO
-        {
-            public int cbSize;
-            public int fMask;
-            public int nMin;
-            public int nMax;
-            public int nPage;
-            public int nPos;
-            public int nTrackPos;
-        }
-
-        private enum ScrollBarDirection
-        {
-            SB_HORZ = 0,
-            SB_VERT = 1,
-            SB_CTL = 2,
-            SB_BOTH = 3,
-        }
-
-        private enum ScrollInfoMask
-        {
-            SIF_RANGE = 0x1,
-            SIF_PAGE = 0x2,
-            SIF_POS = 0x4,
-            SIF_DISABLENOSCROLL = 0x8,
-            SIF_TRACKPOS = 0x10,
-            SIF_ALL = (SIF_RANGE | SIF_PAGE | SIF_POS | SIF_TRACKPOS),
-        }
-
-        [DllImport("user32.dll")]
-        private static extern int GetScrollInfo(IntPtr hWnd, ScrollBarDirection fnBar, ref SCROLLINFO lpsi);
-
-        private SCROLLINFO si = new SCROLLINFO {
-            cbSize = Marshal.SizeOf(new SCROLLINFO()),
-            fMask = (int)ScrollInfoMask.SIF_POS
-        };
-
         [DebuggerStepThrough()]
         protected override void WndProc(ref Message m)
         {
@@ -304,10 +266,8 @@ namespace OpenTween.OpenTweenCustomControl
                 case WM_MOUSEWHEEL:
                 case WM_MOUSEHWHEEL:
                 case WM_KEYDOWN:
-                    if (GetScrollInfo(this.Handle, ScrollBarDirection.SB_VERT, ref si) != 0)
-                        vPos = si.nPos;
-                    if (GetScrollInfo(this.Handle, ScrollBarDirection.SB_HORZ, ref si) != 0)
-                        hPos = si.nPos;
+                    vPos = NativeMethods.GetScrollPosition(this, NativeMethods.ScrollBarDirection.SB_VERT);
+                    hPos = NativeMethods.GetScrollPosition(this, NativeMethods.ScrollBarDirection.SB_HORZ);
                     break;
                 case WM_CONTEXTMENU:
                     if (m.WParam != this.Handle)
@@ -338,11 +298,11 @@ namespace OpenTween.OpenTweenCustomControl
             if (this.IsDisposed) return;
 
             if (vPos != -1)
-                if (GetScrollInfo(this.Handle, ScrollBarDirection.SB_VERT, ref si) != 0 && vPos != si.nPos)
+                if (vPos != NativeMethods.GetScrollPosition(this, NativeMethods.ScrollBarDirection.SB_VERT))
                     if (VScrolled != null)
                         VScrolled(this, EventArgs.Empty);
             if (hPos != -1)
-                if (GetScrollInfo(this.Handle, ScrollBarDirection.SB_HORZ, ref si) != 0 && hPos != si.nPos)
+                if (hPos != NativeMethods.GetScrollPosition(this, NativeMethods.ScrollBarDirection.SB_HORZ))
                     if (HScrolled != null)
                         HScrolled(this, EventArgs.Empty);
         }
index 57f4c9b..669c248 100644 (file)
@@ -25,6 +25,7 @@
 // Boston, MA 02110-1301, USA.
 
 using System;
+using System.ComponentModel;
 using System.Diagnostics;
 using System.Linq;
 using System.Net;
@@ -385,5 +386,52 @@ namespace OpenTween
             RefreshProxyAccount(username, password);
         }
 #endregion
+
+        [StructLayout(LayoutKind.Sequential)]
+        private struct SCROLLINFO
+        {
+            public int cbSize;
+            public ScrollInfoMask fMask;
+            public int nMin;
+            public int nMax;
+            public int nPage;
+            public int nPos;
+            public int nTrackPos;
+        }
+
+        public enum ScrollBarDirection
+        {
+            SB_HORZ = 0,
+            SB_VERT = 1,
+            SB_CTL = 2,
+            SB_BOTH = 3,
+        }
+
+        private enum ScrollInfoMask
+        {
+            SIF_RANGE = 0x1,
+            SIF_PAGE = 0x2,
+            SIF_POS = 0x4,
+            SIF_DISABLENOSCROLL = 0x8,
+            SIF_TRACKPOS = 0x10,
+            SIF_ALL = (SIF_RANGE | SIF_PAGE | SIF_POS | SIF_TRACKPOS),
+        }
+
+        [DllImport("user32.dll")]
+        private static extern int GetScrollInfo(IntPtr hWnd, ScrollBarDirection fnBar, ref SCROLLINFO lpsi);
+
+        public static int GetScrollPosition(Control control, ScrollBarDirection direction)
+        {
+            var si = new SCROLLINFO
+            {
+                cbSize = Marshal.SizeOf<SCROLLINFO>(),
+                fMask = ScrollInfoMask.SIF_POS,
+            };
+
+            if (NativeMethods.GetScrollInfo(control.Handle, direction, ref si) == 0)
+                throw new Win32Exception();
+
+            return si.nPos;
+        }
     }
 }