你好,游客 登录 注册 搜索
背景:
阅读新闻

使用WPF来创建 Metro UI

[日期:2012-06-11] 来源:oschina  作者:junwong [字体:大 中 小]
该如何任意改变窗体大小?
在主窗体的四个角分别添加一个Shape(比如Rectangle)然后为它们都订阅PreviewMouseDown事件处理:
 
01 Rectangle clickedRectangle = (Rectangle)sender;
02      
03 switch (clickedRectangle.Name)
04 {
05   case "top":
06       Cursor = Cursors.SizeNS;
07       ResizeWindow(ResizeDirection.Top);
08       break;
09   case "bottom":
10       Cursor = Cursors.SizeNS;
11       ResizeWindow(ResizeDirection.Bottom);
12       break;
13   // ...
14 }


下面就是用鼠标重新调整窗体大小的代码
 
01 /// <summary>
02 /// Resizes the window.
03 /// </summary>
04 /// <param name="direction">The direction.</param>
05 private void ResizeWindow(ResizeDirection direction)
06 {
07   NativeMethods.SendMessage(m_hwndSource.Handle, WM_SYSCOMMAND,
08       (IntPtr)(61440 + direction), IntPtr.Zero);
09 }
10   
11 [DllImport("user32.dll", CharSet = CharSet.Auto)]
12 internal static extern IntPtr SendMessage(
13   IntPtr hWnd,
14   UInt32 msg,
15   IntPtr wParam,
16   IntPtr lParam);


如何为窗体添加阴影效果。
实际上有两种做法:
第一种就是试用DWM API。这个方法需要订阅SourceInitialized事件。
 
01 /// <summary>
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.
06 /// </summary>
07 /// <param name="e">The <see cref="T:RoutedEventArgs"/>
08 /// that contains the event data.</param>
09 protected override void OnInitialized(EventArgs e)
10 {
11   AllowsTransparency    = false;
12   ResizeMode            = ResizeMode.NoResize;
13   Height                = 480;
14   Width                 = 852; 
15   WindowStartupLocation = WindowStartupLocation.CenterScreen;
16   WindowStyle           = WindowStyle.None;
17   
18   SourceInitialized    += HandleSourceInitialized;
19   
20   base.OnInitialized(e);
21 }
22      
23 /// <summary>
24 /// Handles the source initialized.
25 /// </summary>
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)
30 {
31   m_hwndSource = (HwndSource)PresentationSource.FromVisual(this);
32   
33   // Returns the HwndSource object for the window
34   // which presents WPF content in a Win32 window.
35   HwndSource.FromHwnd(m_hwndSource.Handle).AddHook(
36       new HwndSourceHook(NativeMethods.WindowProc));
37   
38   // http://msdn.microsoft.com/en-us/library/aa969524(VS.85).aspx
39   Int32 DWMWA_NCRENDERING_POLICY = 2;
40   NativeMethods.DwmSetWindowAttribute(
41       m_hwndSource.Handle,
42       DWMWA_NCRENDERING_POLICY,
43       ref DWMWA_NCRENDERING_POLICY,
44       4);
45   
46   // http://msdn.microsoft.com/en-us/library/aa969512(VS.85).aspx
47   NativeMethods.ShowShadowUnderWindow(m_hwndSource.Handle);
48 }

无阴影的窗体

有阴影的窗体



第二种方法就是使用四个外部的透明窗体来制造了阴影的假象,如下图所示


1,用代码的方式创建一个透明的窗体
2,找到Main Window 在屏幕上的坐标,尤其是左上角
3,计算4个透明窗口的坐标
4,当我们移动Main Window时,4个边框透明窗口也需要跟着移动
5,当我们重新设定 Main Window大小时,4个边框透明窗口也要跟着变化大小。
linux
相关资讯       Metro UI  WPF 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款