Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dpiAwareness #37

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Demo/CustomStyleExampleWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ private void TabControl_TabDraggedOutsideBonds(object sender, TabDragEventArgs e
}
}

protected override bool TryDockWindow(Point position, TabBase dockedWindowVM)
protected override bool TryDockWindow(Point absoluteScreenPosition, TabBase dockedWindowVM)
{
Point relativePoint = PointFromScreen(absoluteScreenPosition);//The screen position relative to the tab control
//Hit test against the tab control
if (MyChromeTabControlWithCustomStyle.InputHitTest(position) is FrameworkElement element)
if (MyChromeTabControlWithCustomStyle.InputHitTest(relativePoint) is FrameworkElement element)
{
////test if the mouse is over the tab panel or a tab item.
if (CanInsertTabItem(element))
Expand Down
5 changes: 3 additions & 2 deletions Demo/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,11 @@ private void TabControl_TabDraggedOutsideBonds(object sender, TabDragEventArgs e
}
}

protected override bool TryDockWindow(Point position, TabBase dockedWindowVM)
protected override bool TryDockWindow(Point absoluteScreenPosition, TabBase dockedWindowVM)
{
Point relativePoint = PointFromScreen(absoluteScreenPosition);//The screen position relative to the tab control
//Hit test against the tab control
if (MyChromeTabControl.InputHitTest(position) is FrameworkElement element)
if (MyChromeTabControl.InputHitTest(relativePoint) is FrameworkElement element)
{
////test if the mouse is over the tab panel or a tab item.
if (CanInsertTabItem(element))
Expand Down
5 changes: 3 additions & 2 deletions Demo/PinnedTabExampleWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@ private void TabControl_TabDraggedOutsideBonds(object sender, TabDragEventArgs e
}
}

protected override bool TryDockWindow(Point position, TabBase dockedWindowVM)
protected override bool TryDockWindow(Point absoluteScreenPosition, TabBase dockedWindowVM)
{
Point relativePoint = PointFromScreen(absoluteScreenPosition);//The screen position relative to the tab control
//Hit test against the tab control
if (MyChromeTabControlWithPinnedTabs.InputHitTest(position) is FrameworkElement element)
if (MyChromeTabControlWithPinnedTabs.InputHitTest(relativePoint) is FrameworkElement element)
{
////test if the mouse is over the tab panel or a tab item.
if (CanInsertTabItem(element))
Expand Down
32 changes: 31 additions & 1 deletion Demo/Utilities/Win32.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Interop;

namespace Demo.Utilities
{
Expand Down Expand Up @@ -27,8 +29,36 @@ public class Win32
internal static extern IntPtr GetTopWindow(IntPtr hWnd);
[DllImport("User32")]
internal static extern IntPtr GetWindow(IntPtr hWnd, uint wCmd);

[DllImport("user32.dll", SetLastError=true)]
static extern bool GetWindowRect(IntPtr hwnd, out W32Rect lpRect);
[DllImport("shcore.dll",SetLastError = true)]
private static extern uint SetProcessDpiAwareness(int awareness);
#endregion

/// <summary>
/// Gets the window rect in device units.
/// </summary>
/// <param name="window">The window.</param>
/// <returns>Rect.</returns>
public static Rect GetWindowRect(Window window) {
var hwnd = new WindowInteropHelper(window).Handle;
GetWindowRect(hwnd, out var r);
return new Rect(r.Left, r.Top, r.Right - r.Left, r.Bottom - r.Top);
}

private const int PROCESS_PER_MONITOR_DPI_AWARE = 2;
/// <summary>
/// Sets the dpi awareness for the current process to: per monitor dpi aware.
/// </summary>
public static void SetProcessDpiAwarenessPerMonitor()
{
var result = SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE);
switch (result) {
case 0: return;
case 0x80070005: return; // The DPI awareness is already set, either by calling this API previously or through the application (.exe) manifest.
default: Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error()); return;
}
}
}

[StructLayout(LayoutKind.Sequential)]
Expand Down
8 changes: 4 additions & 4 deletions Demo/WindowBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public abstract partial class WindowBase : Window
//We use this collection to keep track of what windows we have open
protected List<DockingWindow> OpenWindows = new List<DockingWindow>();

protected abstract bool TryDockWindow(Point position, TabBase dockedWindowVM);
protected abstract bool TryDockWindow(Point absoluteScreenPosition, TabBase dockedWindowVM);

public WindowBase()
{
Expand Down Expand Up @@ -116,8 +116,8 @@ private void win_LocationChanged(object sender, EventArgs e)

if (windowUnder != null && windowUnder.Equals(this))
{
Point relativePoint = PointFromScreen(absoluteScreenPos);//The screen position relative to the main window
if (TryDockWindow(relativePoint, win.DataContext as TabBase))

if (TryDockWindow(absoluteScreenPos, win.DataContext as TabBase))
{
win.Close();
}
Expand Down Expand Up @@ -159,7 +159,7 @@ private Window FindWindowUnderThisAt(Window source, Point screenPoint) // WPF u
var allWindows = SortWindowsTopToBottom(Application.Current.Windows.OfType<Window>().Where(x => x.GetType().ToString() != "Microsoft.VisualStudio.DesignTools.WpfTap.WpfVisualTreeService.Adorners.AdornerWindow"
&& x.GetType().ToString() != "Microsoft.VisualStudio.DesignTools.WpfTap.WpfVisualTreeService.Adorners.AdornerLayerWindow"));
var windowsUnderCurrent = from win in allWindows
where (win.WindowState == WindowState.Maximized || new Rect(win.Left, win.Top, win.Width, win.Height).Contains(screenPoint))
where (win.WindowState == WindowState.Maximized || Win32.GetWindowRect(win).Contains(screenPoint))
&& !Equals(win, source)
select win;
return windowsUnderCurrent.FirstOrDefault();
Expand Down