将近1个月没有更新了,不能再懒了,继续更新PDN的学习笔记!
本节将说明PDN中窗体的继承关系,实现过程等。

如上图所示,在PDN中,所有窗体都继承自PdnBaseForm类,该类继承自Form类与ISnapManagerHost接口,该接口定义了SnapManager的get方法,SnapManager是管理界面窗口“粘靠”效果的,该实现方法之后文章中讲述。
现在快速地过一下各个窗体的作用及特点。
BaseForm:
所有PDN里窗体的基类,主要提供常用窗体方法及注册、卸载热键。
1、RegisterFormHotKey(Keys,Function<bool,Keys>):注册热键
PDN拥有设置、处理、卸载热键的功能,这方便了滤镜开发者的开发。RegisterFormHotKey方法参数中,有个Function<bool,Keys>。该参数是一个泛型委托,该委托的类型有三种,说明实现该委托的方法可有多种重载形式。而且该委托的实例,必须实现IComponent和IHotKeyTarget接口,也就是说,该委托必须由一个窗体发出。在BaseForm中,hotkeyRegistrar是热键注册字典,保存了所有已注册的键及委托。接下来的事情就简单了,重写ProcessCmdKey方法,处理窗体的键盘事件,并从字典中找到该键所对应的委托,执行之。

窗体热键注册
1
/**//// <summary>
2
/// 注册窗体范围的热键,以及当热键摁下的委托。
3
/// 该委托的发起方必须是一个控件,不论是窗体还是窗体还是基本控件,都必须相应该热键,而且这些窗体都必须继承自PdnBaseForm
4
/// </summary>
5
public static void RegisterFormHotKey(Keys keys, Function<bool, Keys> callback)
6
{
7
IComponent targetAsComponent = callback.Target as IComponent;
8
IHotKeyTarget targetAsHotKeyTarget = callback.Target as IHotKeyTarget;
9
10
if (targetAsComponent == null && targetAsHotKeyTarget == null)
11
{
12
//检查委托是否由窗体发出
13
throw new ArgumentException("target instance must implement IComponent or IHotKeyTarget", "callback");
14
}
15
16
if (hotkeyRegistrar == null)
17
{
18
//初始化热键字典
19
hotkeyRegistrar = new Dictionary<Keys, Function<bool, Keys>>();
20
}
21
22
Function<bool, Keys> theDelegate = null;
23
24
if (hotkeyRegistrar.ContainsKey(keys))
25
{
26
如已注册热键,替换热键委托
27
theDelegate = hotkeyRegistrar[keys];
28
theDelegate += callback;
29
hotkeyRegistrar[keys] = theDelegate;
30
}
31
else
32
{
33
//把热键和委托添加到字典中
34
theDelegate = new Function<bool, Keys>(callback);
35
hotkeyRegistrar.Add(keys, theDelegate);
36
}
37
38
if (targetAsComponent != null)
39
{
40
targetAsComponent.Disposed += TargetAsComponent_Disposed;
41
}
42
else
43
{
44
targetAsHotKeyTarget.Disposed += TargetAsHotKeyTarget_Disposed;
45
}
46
}
2、UnregisterFormHotKey(Keys,Function<bool,Keys>):卸载热键。
该方法就简单多了,把键值从字典中删除就好了。
MainForm:
这个窗体就是我们运行PDN时看见的窗体了,该窗体提供了所有PDN运行时所支持的窗体功能,包括:浮动工具窗口、浮动窗口透明渐变效果、文件拖放自动打开功能等等,我们这里快速浏览几个比较重要的地方:
AppWorkspace:该变量就是我们所称的“画布”。
接下来的其他窗体还算中规中矩,没有什么特别需要注意的地方,唯一要说的,就是浮动窗体的“粘靠”效果了。该效果的实现下章节详细讲述。