WPF设计の自定义窗体
2021-07-01 19:07
标签:tle handler round lpm triggers str 图片 work 委托 效果图如下: 实现思路: 1.继承Window类 2.为自定义的CustomWindow类设计窗体样式(使用Blend很方便!) 3.为窗体增加最大最小化和关闭按钮,并实现鼠标拖拽改变窗体大小(使用Derek Bartram的WindowResizer.dll库) 代码说明: 1.继承Window类 创建CustomWindow类,继承自System.Window // 加载事件委托 // 解决最大化覆盖任务栏问题 2.为自定义的CustomWindow类设计窗体样式 窗体样式的设计可以使用Expression Blend来进行可视化开发,非常方便 Blend会自动生成样式的xmal文件: 3.为窗体增加最大最小化和关闭按钮,并实现鼠标拖拽改变窗体大小 按钮事件比较简单,通过分别为三个按钮添加Click事件即可 Button minBtn = (Button)baseWindowTemplate.FindName("btnMin", this); Button maxBtn = (Button)baseWindowTemplate.FindName("btnMax", this); Button closeBtn = (Button)baseWindowTemplate.FindName("btnClose", this); Border tp = (Border)baseWindowTemplate.FindName("topborder", this); tp.MouseLeftButtonDown += delegate 仅仅这样实现的话还不够,因为窗体最大化后会覆盖任务栏,这是我们不希望看到的,所以还必须通过WINDOW API的窗口句柄来定义最大化后的尺寸 [DllImport("User32")] 最后是实现用鼠标拖拽改变窗体大小 然后在Windows1这个CustomWindow类的实例中绘制左右及底部5个拖拽热区(矩形) 最后在Window1.xaml.cs中添加事件委托即可 大功告成了! WPF设计の自定义窗体 标签:tle handler round lpm triggers str 图片 work 委托 原文地址:https://www.cnblogs.com/lonelyxmas/p/9941859.html
{
public CustomWindow()
{
// 加载样式
InitializeStyle();
this.Loaded += delegate { InitializeEvent(); };
this.SourceInitialized += new EventHandler(win_SourceInitialized);
}
}
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:src="http://www.soscw.com/clr-namespace:windowStyle1"
Title="CustomWindow"
Height="358" Width="649" AllowsTransparency="True" WindowStyle="None">
/// 加载按钮事件委托
///
private void InitializeEvent()
{
ControlTemplate baseWindowTemplate = (ControlTemplate)App.Current.Resources["CustomWindowControlTemplate"];
minBtn.Click += delegate
{
this.WindowState = WindowState.Minimized;
};
maxBtn.Click += delegate
{
this.WindowState = (this.WindowState == WindowState.Normal ? WindowState.Maximized : WindowState.Normal);
};
closeBtn.Click += delegate
{
this.Close();
};
{
this.DragMove();
};
}
/// 重绘窗体大小
///
void win_SourceInitialized(object sender, EventArgs e)
{
System.IntPtr handle = (new WinInterop.WindowInteropHelper(this)).Handle;
WinInterop.HwndSource.FromHwnd(handle).AddHook(new WinInterop.HwndSourceHook(WindowProc));
}
...
[DllImport("user32")]
internal static extern bool GetMonitorInfo(IntPtr hMonitor, MONITORINFO lpmi);
internal static extern IntPtr MonitorFromWindow(IntPtr handle, int flags);
...
private static System.IntPtr WindowProc(System.IntPtr hwnd, int msg, System.IntPtr wParam, System.IntPtr lParam, ref bool handled)
{
switch (msg)
{
case 0x0024:
WmGetMinMaxInfo(hwnd, lParam);
handled = true;
break;
}
return (System.IntPtr)0;
}
/// 加载Resize委托
///
public void InitializeResizeHandle()
{
WindowResizer wr = new WindowResizer(this);
wr.addResizerRight(right);
wr.addResizerLeft(left);
wr.addResizerDown(bottom);
wr.addResizerLeftDown(leftbottom);
wr.addResizerRightDown(rightbottom);
//wr.addResizerUp(topSizeGrip);
//wr.addResizerLeftUp(topLeftSizeGrip);
//wr.addResizerRightUp(topRightSizeGrip);
}