예제 #1
0
파일: MouseEx.cs 프로젝트: dxm007/Droppy
        /// <summary>
        /// Uses Win32 API to return mouse pointer position relative to a specific Visual. This
        /// function is provided because WPF's mouse API becomes unreliable under certain circumstances,
        /// like during drag-drop operations.
        /// </summary>
        /// <param name="relativeTo">Visual against which mouse position is reported</param>
        /// <returns>Mouse pointer location relative to passed in Visual</returns>
        public static Point GetPosition(Visual relativeTo)
        {
            Win32.Point w32Mouse = new Win32.Point();

            Win32.GetCursorPos( ref w32Mouse );

            return relativeTo.PointFromScreen( new Point( w32Mouse.X, w32Mouse.Y ) );
        }
예제 #2
0
        /// <summary>
        /// Returns the mouse cursor location.  This method is necessary during 
        /// a drag-drop operation because the WPF mechanisms for retrieving the
        /// cursor coordinates are unreliable.
        /// </summary>
        /// <param name="relativeTo">The Visual to which the mouse coordinates will be relative.</param>
        public static Point GetMousePosition(Visual relativeTo)
        {
            Win32Point mouse = new Win32Point();
            GetCursorPos(ref mouse);

            // Using PointFromScreen instead of Dan Crevier's code (commented out below)
            // is a bug fix created by William J. Roberts.  Read his comments about the fix
            // here: http://www.codeproject.com/useritems/ListViewDragDropManager.asp?msg=1911611#xx1911611xx
            return relativeTo.PointFromScreen(new Point((double)mouse.X, (double)mouse.Y));
        }
예제 #3
0
        /// <summary>
        /// 指定したコントロールに対するマウスカーソルの相対位置を返します
        /// </summary>
        /// <param name="v"></param>
        /// <returns></returns>
        public static Point GetPosition(System.Windows.Media.Visual v)
        {
            POINT p;

            GetCursorPos(out p);

            Point converted = v.PointFromScreen(new Point(p.X, p.Y));

            //System.Diagnostics.Debug.WriteLine("GetNowPosition PointFromScreen ({0},{1}) => ({2},{3})",
            //    p.X, p.Y, converted.X, converted.Y);
            return(converted);
        }
예제 #4
0
        /// <summary>
        /// Returns the mouse cursor location.  This method is necessary during
        /// a drag-drop operation because the WPF mechanisms for retrieving the
        /// cursor coordinates are unreliable.
        /// </summary>
        /// <param name="relativeTo">The Visual to which the mouse coordinates will be relative.</param>
        public static Point GetMousePosition(System.Windows.Media.Visual relativeTo)
        {
            var mousePoint = GetMousePosition();

            return(relativeTo.PointFromScreen(mousePoint));

            #region Commented Out
            //System.Windows.Interop.HwndSource presentationSource =
            //    (System.Windows.Interop.HwndSource)PresentationSource.FromVisual( relativeTo );
            //ScreenToClient( presentationSource.Handle, ref mouse );
            //GeneralTransform transform = relativeTo.TransformToAncestor( presentationSource.RootVisual );
            //Point offset = transform.Transform( new Point( 0, 0 ) );
            //return new Point( mouse.X - offset.X, mouse.Y - offset.Y );
            #endregion // Commented Out
        }
예제 #5
0
        /// <summary>
        /// Returns the mouse cursor location.  This method is necessary during 
        /// a drag-drop operation because the WPF mechanisms for retrieving the
        /// cursor coordinates are unreliable.
        /// </summary>
        /// <param name="relativeTo">The Visual to which the mouse coordinates will be relative.</param>
        public static Point GetMousePosition( Visual relativeTo )
        {
            Win32Point mouse = new Win32Point();
            GetCursorPos( ref mouse );

            // Using PointFromScreen instead of Dan Crevier's code (commented out below)
            // is a bug fix created by William J. Roberts.  Read his comments about the fix
            // here: http://www.codeproject.com/useritems/ListViewDragDropManager.asp?msg=1911611#xx1911611xx
            return relativeTo.PointFromScreen( new Point( (double)mouse.X, (double)mouse.Y ) );

            #region Commented Out
            //System.Windows.Interop.HwndSource presentationSource =
            //    (System.Windows.Interop.HwndSource)PresentationSource.FromVisual( relativeTo );
            //ScreenToClient( presentationSource.Handle, ref mouse );
            //GeneralTransform transform = relativeTo.TransformToAncestor( presentationSource.RootVisual );
            //Point offset = transform.Transform( new Point( 0, 0 ) );
            //return new Point( mouse.X - offset.X, mouse.Y - offset.Y );
            #endregion // Commented Out
        }
예제 #6
0
        /* NOTE: CloneUsingXaml is temporarily removed.
           The method is currently not used. To avoid any confusions in the API documentation it is
           commented out because it would be listed as extension method for every other type.
     
        /// <summary>
        /// Clones an object using XAML serialization rules.
        /// </summary>
        /// <param name="obj">The <see cref="Object"/> to be cloned.</param>
        /// <returns>
        /// A clone of <paramref name="obj"/> where all public fields are cloned (deep copy). Fields or
        /// events are not copied and have their default values.
        /// </returns>
        public static Object CloneUsingXaml(this object obj)
        {
            // NOTE: This helper method is based on the tip provided by Mike Hillberg.
            // See http://blogs.msdn.com/mikehillberg/archive/2007/05/01/CloneWithXamlWriterXamlReader.aspx

            string xaml = XamlWriter.Save(obj);
            return XamlReader.Load(new XmlTextReader(new StringReader(xaml)));
        }
        */


        /// <summary>
        /// Gets location of the mouse cursor relative to the specified <see cref="Visual"/>.
        /// </summary>
        /// <param name="visual">The <see cref="Visual"/>.</param>
        /// <returns>The mouse position relative to <paramref name="visual"/>.</returns>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="visual"/> is <see langword="null"/>.
        /// </exception>
        public static Point GetMousePosition(Visual visual)
        {
            if (visual == null)
                throw new ArgumentNullException(nameof(visual));

            POINT pointNative = new POINT();
            Win32.GetCursorPos(ref pointNative);
            Point point = new Point(pointNative.X, pointNative.Y);

            // Convert mouse position from screen coordinates into local coordinates of visual.
            return visual.PointFromScreen(point);
        }
예제 #7
0
 private static Rect GetBoundsInLocalCoordinates(Visual visual, Rect boundsInScreenCoordinates)
 {
     var localBoundsUpperLeft = visual.PointFromScreen(new Point(boundsInScreenCoordinates.X, boundsInScreenCoordinates.Y));
     var localBoundsLowerRight = visual.PointFromScreen(new Point(
                                                     boundsInScreenCoordinates.X + boundsInScreenCoordinates.Width,
                                                     boundsInScreenCoordinates.Y + boundsInScreenCoordinates.Height));
     return new Rect(localBoundsUpperLeft, localBoundsLowerRight);
 }
예제 #8
0
 public static Point GetCursorPos(Visual relativeTo)
 {
     Win32Point pt = new Win32Point();
     GetCursorPos(ref pt);
     return relativeTo.PointFromScreen(new Point((double)pt.X, (double)pt.Y));
 }
예제 #9
0
 /// <summary>
 /// Returns the mouse cursor location.  This method is necessary during
 /// a drag-drop operation because the WPF mechanisms for retrieving the
 /// cursor coordinates are unreliable.
 /// </summary>
 /// <param name="relativeTo">The Visual to which the mouse coordinates will be relative.</param>
 public static Point GetMousePosition(Visual relativeTo)
 {
     Win32Point mouse = new Win32Point();
     GetCursorPos(ref mouse);
     return relativeTo.PointFromScreen(new Point((double)mouse.X, (double)mouse.Y));
 }
 public static Point GetPosition(Visual relativeTo)
 {
     return relativeTo.PointFromScreen(GetPosition());
 }
예제 #11
0
 public static Point CorrectGetPosition(Visual relativeTo)
 {
     var w32Mouse = new NativeMethods.Win32Point();
     NativeMethods.GetCursorPos(ref w32Mouse);
     return relativeTo.PointFromScreen(new Point(w32Mouse.X, w32Mouse.Y));
 }
예제 #12
0
파일: Win32Calls.cs 프로젝트: sbambach/ATF
 internal static Point GetPosition(Visual relativeTo)
 {
     var w32Mouse = new User32.POINT();
     GetCursorPos(ref w32Mouse);
     return relativeTo.PointFromScreen(new Point(w32Mouse.X, w32Mouse.Y));
 }
예제 #13
0
파일: Icon.cs 프로젝트: sbambach/ATF
 private static Point GetPixelSnappingOffset(Visual visual, Visual rootVisual)
 {
     var point = new Point();
     if (rootVisual != null)
     {
         var transform = visual.TransformToAncestor(rootVisual) as Transform;
         if ((transform != null) && transform.Value.HasInverse)
         {
             point = visual.PointFromScreen(visual.PointToScreen(point));
         }
     }
     
     return point;
 }
예제 #14
0
파일: BaseWindow.cs 프로젝트: D-Key/LordJZ
 private static Point GetCorrectPosition(Visual relativeTo)
 {
     UnsafeNativeMethods.Win32Point w32Mouse;
     UnsafeNativeMethods.GetCursorPos(out w32Mouse);
     return relativeTo.PointFromScreen(new Point(w32Mouse.X, w32Mouse.Y));
 }