信息系统开发平台OpenExpressApp - WPF窗口控件支持Zoom功能

  客户使用软件时,提出字太小了,提出能够放大控件的要求,我知道WPF很容易处理这种功能,所以就在OpenExpressApp加入了简单的Zoom功能,本篇介绍一下OpenExpressApp如何支持Zoom功能。

增加Zoom类

现在简单的处理为支持Ctrol+滚轮放大缩小,Ctrl+鼠标中键点击恢复默认,Zoom类的EnableZoom给控件绑定鼠标事件,具体代码如下:

代码
1 1 /*******************************************************
2 2 *
3 3 * 作者:周金根
4 4 * 创建时间:20100330
5 5 * 说明:支持控件放大功能
6 6 * 版本号:1.0.0
7 7 *
8 8 * 历史记录:
9 9 * 创建文件 周金根 20100330
10 10 *
11 11 *******************************************************/
12  12
13  13 using System;
14  14 using System.Collections.Generic;
15  15 using System.Linq;
16  16 using System.Text;
17  17 using System.Windows.Controls;
18  18 using System.Windows.Media;
19 19 using System.Windows.Input;
20 20 using System.Windows;
21 21
22 22 namespace OpenExpressApp.Module.WPF
23 23 {
24 24 /// <summary>
25 25 ///
26 26 /// </summary>
27 27 static class Zoom
28 28 {
29 29 private const double MINScale = 0.8;
30 30 static internal void DisableZoom(FrameworkElement fe)
31 31 {
32 32 fe.PreviewMouseWheel -= new System.Windows.Input.MouseWheelEventHandler(ctrl_PreviewMouseWheel);
33 33 fe.PreviewMouseDown -= new MouseButtonEventHandler(ctrl_PreviewMouseDown);
34 34 }
35 35
36 36 static internal void EnableZoom(FrameworkElement fe)
37 37 {
38 38 fe.PreviewMouseWheel += new System.Windows.Input.MouseWheelEventHandler(ctrl_PreviewMouseWheel);
39 39 fe.PreviewMouseDown += new MouseButtonEventHandler(ctrl_PreviewMouseDown);
40 40 }
41 41
42 42 /// <summary>
43 43 /// 滚轮中键恢复默认
44 44 /// </summary>
45 45 /// <param name="sender"></param>
46 46 /// <param name="e"></param>
47 47 static void ctrl_PreviewMouseDown(object sender, MouseButtonEventArgs e)
48 48 {
49 49 FrameworkElement ctrl = sender as FrameworkElement;
50 50 if (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl))
51 51 {
52 52 if (e.MiddleButton == MouseButtonState.Pressed)
53 53 {
54 54 (ctrl.LayoutTransform as ScaleTransform).ScaleX = 1;
55 55 (ctrl.LayoutTransform as ScaleTransform).ScaleY = 1;
56 56 }
57 57
58 58 }
59 59 }
60 60
61 61 /// <summary>
62 62 /// 限制缩小值
63 63 /// </summary>
64 64 /// <param name="sender"></param>
65 65 /// <param name="e"></param>
66 66 static void ctrl_PreviewMouseWheel(object sender, System.Windows.Input.MouseWheelEventArgs e)
67 67 {
68 68 FrameworkElement ctrl = sender as FrameworkElement;
69 69 if (!(ctrl.LayoutTransform is ScaleTransform)) ctrl.LayoutTransform = new ScaleTransform();
70 70
71 71 if (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl))
72 72 {
73 73 ScaleTransform stf = ctrl.LayoutTransform as ScaleTransform;
74 74 stf.ScaleX += (e.Delta > 0) ? 0.1 : -0.1;
75 75 stf.ScaleY += (e.Delta > 0) ? 0.1 : -0.1;
76 76 if (stf.ScaleX < MINScale) stf.ScaleX = MINScale;
77 77 if (stf.ScaleY < MINScale) stf.ScaleY = MINScale;
78 78 }
79 79
80 80 }
81 81
82 82 }
83 83 }

使用Zoom类

修改窗口模板代码,使用Zoom类,在构造函数中调用EnableZoom方法,代码如下:

1 public partial class ListDetailForm : WindowTemplateBase, IWindowTemplate
2 {
.....
7 public ListDetailForm(Type boType)
8 : base(boType)
9 {
10 InitializeComponent();
11 Zoom.EnableZoom(this);
12 .....
29 }
30

演示效果

默认界面

放大效果

 

更多内容: 开源信息系统开发平台之OpenExpressApp框架.pdf

 

欢迎转载,转载请注明:转载自周金根 [ http://zhoujg.cnblogs.com/ ]

posted on 2010-03-30 17:46  周 金根  阅读(2431)  评论(0编辑  收藏  举报

导航