private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) { RECT rect; switch ((WM)msg) { case WM.SHOWWINDOW: if ((int)lParam == 3 && this.Visibility != Visibility.Visible) // 3 == SW_PARENTOPENING { handled = true; //handle this message so window isn't shown until we want it to } break; case WM.MOUSEACTIVATE: handled = true; if (this.ownerHandle != IntPtr.Zero) { Standard.NativeMethods.SendMessage(this.ownerHandle, Standard.WM.ACTIVATE, wParam, lParam); } return(new IntPtr(3)); case WM.LBUTTONDOWN: if (this.ownerHandle != IntPtr.Zero && UnsafeNativeMethods.GetWindowRect(this.ownerHandle, out rect)) { Point pt; if (WinApiHelper.TryGetRelativeMousePosition(this.handle, out pt)) { NativeMethods.PostMessage(this.ownerHandle, (uint)WM.NCLBUTTONDOWN, (IntPtr)this.getHitTestValue(pt, rect), IntPtr.Zero); } } break; case WM.NCHITTEST: Cursor cursor = null; if (this._owner.ResizeMode == ResizeMode.NoResize || this._owner.ResizeMode == ResizeMode.CanMinimize) { cursor = this._owner.Cursor; } else { if (this.ownerHandle != IntPtr.Zero && UnsafeNativeMethods.GetWindowRect(this.ownerHandle, out rect)) { Point pt; if (WinApiHelper.TryGetRelativeMousePosition(this.handle, out pt)) { cursor = this.getCursor(pt, rect); } } } if (cursor != null && cursor != this.Cursor) { this.Cursor = cursor; } break; } return(IntPtr.Zero); }
/// <summary> /// Gets the relative mouse position to the given handle in client coordinates. /// </summary> /// <param name="hWnd">The handle for this method.</param> public static System.Windows.Point GetRelativeMousePosition(IntPtr hWnd) { if (hWnd == IntPtr.Zero) { return(new System.Windows.Point()); } var point = WinApiHelper.GetPhysicalCursorPos(); NativeMethods.ScreenToClient(hWnd, ref point); return(new System.Windows.Point(point.X, point.Y)); }