该如何任意改变窗体大小?
在主窗体的四个角分别添加一个Shape(比如Rectangle)然后为它们都订阅PreviewMouseDown事件处理:
01 |
Rectangle clickedRectangle = (Rectangle)sender; |
03 |
switch (clickedRectangle.Name) |
06 |
Cursor = Cursors.SizeNS; |
07 |
ResizeWindow(ResizeDirection.Top); |
10 |
Cursor = Cursors.SizeNS; |
11 |
ResizeWindow(ResizeDirection.Bottom); |
下面就是用鼠标重新调整窗体大小的代码
02 |
/// Resizes the window. |
04 |
/// <param name="direction">The direction.</param> |
05 |
private void ResizeWindow(ResizeDirection direction) |
07 |
NativeMethods.SendMessage(m_hwndSource.Handle, WM_SYSCOMMAND, |
08 |
(IntPtr)(61440 + direction), IntPtr.Zero); |
11 |
[DllImport("user32.dll", CharSet = CharSet.Auto)] |
12 |
internal static extern IntPtr SendMessage( |
如何为窗体添加阴影效果。
实际上有两种做法:
第一种就是试用DWM API。这个方法需要订阅SourceInitialized事件。
02 |
/// Raises the <see cref="FrameworkElement.Initialized"/> event. |
03 |
/// This method is invoked whenever |
04 |
/// <see cref="P:FrameworkElement.IsInitialized"/> |
05 |
/// is set to true internally. |
07 |
/// <param name="e">The <see cref="T:RoutedEventArgs"/> |
08 |
/// that contains the event data.</param> |
09 |
protected override void OnInitialized(EventArgs e) |
11 |
AllowsTransparency = false; |
12 |
ResizeMode = ResizeMode.NoResize; |
15 |
WindowStartupLocation = WindowStartupLocation.CenterScreen; |
16 |
WindowStyle = WindowStyle.None; |
18 |
SourceInitialized += HandleSourceInitialized; |
20 |
base.OnInitialized(e); |
24 |
/// Handles the source initialized. |
26 |
/// <param name="sender">The sender.</param> |
27 |
/// <param name="e">The <see cref="System.EventArgs"/> |
28 |
/// instance containing the event data.</param> |
29 |
private void HandleSourceInitialized(Object sender, EventArgs e) |
31 |
m_hwndSource = (HwndSource)PresentationSource.FromVisual(this); |
35 |
HwndSource.FromHwnd(m_hwndSource.Handle).AddHook( |
36 |
new HwndSourceHook(NativeMethods.WindowProc)); |
39 |
Int32 DWMWA_NCRENDERING_POLICY = 2; |
40 |
NativeMethods.DwmSetWindowAttribute( |
42 |
DWMWA_NCRENDERING_POLICY, |
43 |
ref DWMWA_NCRENDERING_POLICY, |
47 |
NativeMethods.ShowShadowUnderWindow(m_hwndSource.Handle); |
无阴影的窗体

有阴影的窗体
第二种方法就是使用四个外部的透明窗体来制造了阴影的假象,如下图所示
1,用代码的方式创建一个透明的窗体
2,找到Main Window 在屏幕上的坐标,尤其是左上角
3,计算4个透明窗口的坐标
4,当我们移动Main Window时,4个边框透明窗口也需要跟着移动
5,当我们重新设定 Main Window大小时,4个边框透明窗口也要跟着变化大小。
