在WPF中,经常需要对窗口进行设置,下面讲讲常用的几个设置。
窗口样式
1、无边框窗口
无边框透明窗体 设置
WindowStyle="None"--无边框,如果需要其它按钮,如缩小、放大、收缩、关闭按钮,可以自定义
AllowsTransparency="True"--只有设置了AllowsTransparency,才有1、窗体真正的隐藏边框2、背景透明有效
Background="Transparent"
窗口阴影设置
可以用Border的CornerRadius设置窗口四周圆角
设置窗口阴影--值得一提的是,如果直接用Window.Effect,效果会影响CornerRadius设置的圆角效果。
<Window.Effect>
<DropShadowEffect BlurRadius="30" Color="Gray" ShadowDepth="0" RenderingBias="Quality" ></DropShadowEffect> </Window.Effect>举例:
2、窗口按钮
当我们设置无边窗口时,也即是需要重新设置一套窗口的操作按钮。
按钮,可以用修改Button的Background或者修改Template,往其中设置图标或者直接用Path、Border、Rectangle等基础元素绘制。
然后添加简单的动画Animation
1) 方法1.下面是自定义的按钮Style,往其中传入图标路径即可
2)方法2.直接用基础元素绘制
Path,通过Path来画线。当然一般水平、竖直也是可以用Rectangle/Border之类的替代
一些简单的线条图标用Path来做,还是很方便的。
3、窗口 resize 鼠标自由改变窗口大小 (转)
覆写一个OnSourceInitialized方法,在其中添加一个委托
//重写 protected override void OnSourceInitialized(EventArgs e) { base.OnSourceInitialized(e); HwndSource hwndSource = PresentationSource.FromVisual(this) as HwndSource; if (hwndSource != null) { hwndSource.AddHook(new HwndSourceHook(WndProc)); } } private const int WM_NCHITTEST = 0x0084; private Point mousePoint = new Point(); //鼠标坐标 private const int ResizeBorderAGWidth = 12;//转角宽度 private const int ResizeBorderThickness = 4;//边框宽度 ////// 委托方法 /// /// /// /// /// /// ///protected virtual IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) { switch (msg) { case WM_NCHITTEST: mousePoint.X = (lParam.ToInt32() & 0xFFFF); mousePoint.Y = (lParam.ToInt32() >> 16); // 窗口左上角 if (mousePoint.Y - Top <= ResizeBorderAGWidth && mousePoint.X - Left <= ResizeBorderAGWidth) { handled = true; return new IntPtr((int)HitTest.HTTOPLEFT); } // 窗口左下角 else if (ActualHeight + Top - mousePoint.Y <= ResizeBorderAGWidth && mousePoint.X - Left <= ResizeBorderAGWidth) { handled = true; return new IntPtr((int)HitTest.HTBOTTOMLEFT); } // 窗口右上角 else if (mousePoint.Y - Top <= ResizeBorderAGWidth && ActualWidth + Left - mousePoint.X <= ResizeBorderAGWidth) { handled = true; return new IntPtr((int)HitTest.HTTOPRIGHT); } // 窗口右下角 else if (ActualWidth + Left - mousePoint.X <= ResizeBorderAGWidth && ActualHeight + Top - mousePoint.Y <= ResizeBorderAGWidth) { handled = true; return new IntPtr((int)HitTest.HTBOTTOMRIGHT); } // 窗口左侧 else if (mousePoint.X - Left <= ResizeBorderThickness) { handled = true; return new IntPtr((int)HitTest.HTLEFT); } // 窗口右侧 else if (ActualWidth + Left - mousePoint.X <= ResizeBorderThickness) { handled = true; return new IntPtr((int)HitTest.HTRIGHT); } // 窗口上方 else if (mousePoint.Y - Top <= ResizeBorderThickness) { handled = true; return new IntPtr((int)HitTest.HTTOP); } // 窗口下方 else if (ActualHeight + Top - mousePoint.Y <= ResizeBorderThickness) { handled = true; return new IntPtr((int)HitTest.HTBOTTOM); } else // 窗口移动 { //handled = true; //return new IntPtr((int)HitTest.HTCAPTION); return IntPtr.Zero; } } return IntPtr.Zero; }
用到的枚举列表类:
public enum HitTest : int { HTERROR = -2, HTTRANSPARENT = -1, HTNOWHERE = 0, HTCLIENT = 1, HTCAPTION = 2, HTSYSMENU = 3, HTGROWBOX = 4, HTSIZE = HTGROWBOX, HTMENU = 5, HTHSCROLL = 6, HTVSCROLL = 7, HTMINBUTTON = 8, HTMAXBUTTON = 9, HTLEFT = 10, HTRIGHT = 11, HTTOP = 12, HTTOPLEFT = 13, HTTOPRIGHT = 14, HTBOTTOM = 15, HTBOTTOMLEFT = 16, HTBOTTOMRIGHT = 17, HTBORDER = 18, HTREDUCE = HTMINBUTTON, HTZOOM = HTMAXBUTTON, HTSIZEFIRST = HTLEFT, HTSIZELAST = HTBOTTOMRIGHT, HTOBJECT = 19, HTCLOSE = 20, HTHELP = 21, }
窗口拖动
1、在界面上对布局元素如Grid,添加委托事件: MouseLeftButtonDown="UIElement_OnMouseLeftButtonDown"
2、委托方法中处理一下就行了:
private void UIElement_OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e) { if (e.ButtonState==MouseButtonState.Pressed) { this.DragMove(); } }