mirror of
https://github.com/VitalickS/BrightSharp.Toolkit.git
synced 2026-03-21 10:21:16 +00:00
Menu popup position fix;
move minmaxsize logic from behavior to avoid use behavior in MarkupExtensionProperties; WindowHeaderBrush improve for DevLab colors
This commit is contained in:
78
BrightSharp/Behaviors/MinMaxSize_Logic.cs
Normal file
78
BrightSharp/Behaviors/MinMaxSize_Logic.cs
Normal file
@@ -0,0 +1,78 @@
|
||||
using BrightSharp.Interop;
|
||||
using BrightSharp.Interop.Constants;
|
||||
using BrightSharp.Interop.Structures;
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Windows;
|
||||
using System.Windows.Interop;
|
||||
|
||||
namespace BrightSharp.Behaviors
|
||||
{
|
||||
internal class MinMaxSize_Logic
|
||||
{
|
||||
private HwndSource hwndSource;
|
||||
private Window associatedObject;
|
||||
public MinMaxSize_Logic(Window associatedObject)
|
||||
{
|
||||
this.associatedObject = associatedObject;
|
||||
}
|
||||
public void OnAttached()
|
||||
{
|
||||
this.associatedObject.SourceInitialized += AssociatedObject_SourceInitialized;
|
||||
}
|
||||
|
||||
private void AssociatedObject_SourceInitialized(object sender, EventArgs e)
|
||||
{
|
||||
IntPtr handle = new WindowInteropHelper(this.associatedObject).Handle;
|
||||
hwndSource = HwndSource.FromHwnd(handle);
|
||||
hwndSource.AddHook(WindowProc);
|
||||
}
|
||||
|
||||
public void OnDetaching()
|
||||
{
|
||||
this.associatedObject.SourceInitialized -= AssociatedObject_SourceInitialized;
|
||||
if (hwndSource != null)
|
||||
{
|
||||
hwndSource.RemoveHook(WindowProc);
|
||||
hwndSource.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
private IntPtr WindowProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
|
||||
{
|
||||
switch (msg)
|
||||
{
|
||||
case 0x0024:
|
||||
WmGetMinMaxInfo(hwnd, lParam);
|
||||
handled = true;
|
||||
break;
|
||||
}
|
||||
return (IntPtr)0;
|
||||
}
|
||||
|
||||
private void WmGetMinMaxInfo(IntPtr hwnd, IntPtr lParam)
|
||||
{
|
||||
var mmi = (MINMAXINFO)Marshal.PtrToStructure(lParam, typeof(MINMAXINFO));
|
||||
|
||||
// Adjust the maximized size and position to fit the work area of the correct monitor
|
||||
IntPtr monitor = NativeMethods.MonitorFromWindow(hwnd, (int)MonitorFromWindowFlags.MONITOR_DEFAULTTONEAREST);
|
||||
|
||||
if (monitor != IntPtr.Zero)
|
||||
{
|
||||
var monitorInfo = new MONITORINFO();
|
||||
NativeMethods.GetMonitorInfo(monitor, monitorInfo);
|
||||
RECT rcWorkArea = monitorInfo.rcWork;
|
||||
RECT rcMonitorArea = monitorInfo.rcMonitor;
|
||||
mmi.ptMaxPosition.x = Math.Abs(rcWorkArea.Left - rcMonitorArea.Left);
|
||||
mmi.ptMaxPosition.y = Math.Abs(rcWorkArea.Top - rcMonitorArea.Top);
|
||||
mmi.ptMaxSize.x = Math.Abs(rcWorkArea.Right - rcWorkArea.Left);
|
||||
mmi.ptMaxSize.y = Math.Abs(rcWorkArea.Bottom - rcWorkArea.Top);
|
||||
mmi.ptMinTrackSize.x = (int)associatedObject.MinWidth;
|
||||
mmi.ptMinTrackSize.y = (int)associatedObject.MinHeight;
|
||||
}
|
||||
|
||||
Marshal.StructureToPtr(mmi, lParam, true);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,4 @@
|
||||
using BrightSharp.Interop;
|
||||
using BrightSharp.Interop.Constants;
|
||||
using BrightSharp.Interop.Structures;
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Windows;
|
||||
using System.Windows;
|
||||
using System.Windows.Interactivity;
|
||||
using System.Windows.Interop;
|
||||
|
||||
@@ -12,67 +7,19 @@ namespace BrightSharp.Behaviors
|
||||
public class WindowMinMaxSizeBehavior : Behavior<Window>
|
||||
{
|
||||
private HwndSource hwndSource;
|
||||
|
||||
private MinMaxSize_Logic logic;
|
||||
protected override void OnAttached()
|
||||
{
|
||||
base.OnAttached();
|
||||
|
||||
AssociatedObject.SourceInitialized += AssociatedObject_SourceInitialized;
|
||||
}
|
||||
|
||||
private void AssociatedObject_SourceInitialized(object sender, EventArgs e)
|
||||
{
|
||||
IntPtr handle = (new WindowInteropHelper(AssociatedObject)).Handle;
|
||||
hwndSource = HwndSource.FromHwnd(handle);
|
||||
hwndSource.AddHook(WindowProc);
|
||||
logic = new MinMaxSize_Logic(AssociatedObject);
|
||||
logic.OnAttached();
|
||||
}
|
||||
|
||||
|
||||
protected override void OnDetaching()
|
||||
{
|
||||
AssociatedObject.SourceInitialized -= AssociatedObject_SourceInitialized;
|
||||
if (hwndSource != null)
|
||||
{
|
||||
hwndSource.RemoveHook(WindowProc);
|
||||
hwndSource.Dispose();
|
||||
}
|
||||
logic.OnDetaching();
|
||||
base.OnDetaching();
|
||||
}
|
||||
|
||||
private IntPtr WindowProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
|
||||
{
|
||||
switch (msg)
|
||||
{
|
||||
case 0x0024:
|
||||
WmGetMinMaxInfo(hwnd, lParam);
|
||||
handled = true;
|
||||
break;
|
||||
}
|
||||
return (IntPtr)0;
|
||||
}
|
||||
|
||||
private void WmGetMinMaxInfo(IntPtr hwnd, IntPtr lParam)
|
||||
{
|
||||
var mmi = (MINMAXINFO)Marshal.PtrToStructure(lParam, typeof(MINMAXINFO));
|
||||
|
||||
// Adjust the maximized size and position to fit the work area of the correct monitor
|
||||
IntPtr monitor = NativeMethods.MonitorFromWindow(hwnd, (int)MonitorFromWindowFlags.MONITOR_DEFAULTTONEAREST);
|
||||
|
||||
if (monitor != IntPtr.Zero)
|
||||
{
|
||||
var monitorInfo = new MONITORINFO();
|
||||
NativeMethods.GetMonitorInfo(monitor, monitorInfo);
|
||||
RECT rcWorkArea = monitorInfo.rcWork;
|
||||
RECT rcMonitorArea = monitorInfo.rcMonitor;
|
||||
mmi.ptMaxPosition.x = Math.Abs(rcWorkArea.Left - rcMonitorArea.Left);
|
||||
mmi.ptMaxPosition.y = Math.Abs(rcWorkArea.Top - rcMonitorArea.Top);
|
||||
mmi.ptMaxSize.x = Math.Abs(rcWorkArea.Right - rcWorkArea.Left);
|
||||
mmi.ptMaxSize.y = Math.Abs(rcWorkArea.Bottom - rcWorkArea.Top);
|
||||
mmi.ptMinTrackSize.x = (int)AssociatedObject.MinWidth;
|
||||
mmi.ptMinTrackSize.y = (int)AssociatedObject.MinHeight;
|
||||
}
|
||||
|
||||
Marshal.StructureToPtr(mmi, lParam, true);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user