把从
http://www.afterdawn.com/software/source_codes/paint.net.cfm下载到的PDN3.05源码下载下来后,解压出来src目录下就是PDN的解决方案目录了,可以使用VS2005或VS2008打开解决方案。
在所有工程中,核心的工程为 Data\Effects\Paintdotnet\PdnLib\SystemLayer。而其中Paintdotnet是启动项目,程序从Startup.cs启动。
现在我们就来看看PDN的启动有什么奥秘吧。
Startup的构造函数接受一个string[]作为启动参数。
应用程序从Main方法启动,我们一起一步一步看看PDN是怎么启动的。
使用Main方法的启动参数构造一个新的Startup对象,并调用Start方法。
Start方法中,一进来就注册了应用程序域的异常事件,这样做,就防止了应用程序域中的任何未捕捉到异常弹出.net异常对话窗口导致用户不知所措。
接下来检查系统DPI兼容,在这里我们一路跟踪定义,进入了SafeNativeMethods类中,发现此类不简单,这里除了方法定义外,没有方法的实现,都是清一色的[DllImport]。小弟系井底之蛙,一开始搞不懂这个属性是什么意思,于是翻查MSDN:
发现[DllImport]描述的方法,都是调用了windows本身的非托管代码,并一定要加上extend关键字以及都必须为静态方法。
而在此调用的SetProcessDPIAware说明在此:
http://msdn.microsoft.com/zh-cn/library/aa970067.aspx
接下来如果启动参数中不包含"/skipRepairAttempt",进入StartPart2。
PDN对应多种语言系统,并能加载不同的语言资源,这是怎么做到的呢?
跟踪进入Settings类,在类的顶部,定义了两个只读变量:
Code
public static readonly Settings SystemWide = new Settings(Microsoft.Win32.Registry.LocalMachine);
public static readonly Settings CurrentUser = new Settings(Microsoft.Win32.Registry.CurrentUser);
public static readonly Settings SystemWide = new Settings(Microsoft.Win32.Registry.LocalMachine);
public static readonly Settings CurrentUser = new Settings(Microsoft.Win32.Registry.CurrentUser);
这两个变量获取了注册表中LocalMachine和CurrentUser键的所有值,返回Startup中,StartPart2中第一句就是查询CurrentUser的"LanguageName"的值,这个键是在安装PDN时写入的,在注册表的路径“HKEY_CURRENT_USER\Software\Paint.Net\”中。在我机器上该值为“zh-CN”,使用该值构造CultureInfo,并设置到当前进程中:
本地化设置
1CultureInfo ci = new CultureInfo(locale, true);//new CultureInfo("zh-CN",true)
2Thread.CurrentThread.CurrentUICulture = ci;
3
1CultureInfo ci = new CultureInfo(locale, true);//new CultureInfo("zh-CN",true)
2Thread.CurrentThread.CurrentUICulture = ci;
3
为当前主进程设置了本地化信息后,并在以后使用ResourceManager,就能查找资源文件中的相应资源了。
接下来的事就比较琐碎了,检查系统版本,设置主窗体的尺寸等。贴下相关代码及注释:
Code
1 // 检查系统版本
2 if (!OS.CheckOSRequirement())
3 {
4 string message = PdnResources.GetString("Error.OSRequirement");
5 Utility.ErrorBox(null, message);
6 return;
7 }
8
9 // 检查启动参数是否含有升级参数
10 if (this.args.Length == 1 &&
11 this.args[0] == Updates.UpdatesOptionsDialog.CommandLineParameter)
12 {
13 Updates.UpdatesOptionsDialog.ShowUpdateOptionsDialog(null, false);
14 }
15 else
16 {
17 SingleInstanceManager singleInstanceManager = new SingleInstanceManager("PaintDotNet");
18
19 // If this is not the first instance of PDN.exe, then forward the command-line
20 // parameters over to the first instance.
21 if (!singleInstanceManager.IsFirstInstance)
22 {
23 singleInstanceManager.FocusFirstInstance();
24
25 foreach (string arg in this.args)
26 {
27 singleInstanceManager.SendInstanceMessage(arg, 30);
28 }
29
30 singleInstanceManager.Dispose();
31 singleInstanceManager = null;
32
33 return;
34 }
35
36 // 构造新窗口
37 this.mainForm = new MainForm(this.args);
38
39 // 设置屏幕旋转方位及主窗体尺寸
40 if (this.mainForm.ScreenAspect < 1.0)
41 {
42 int width = mainForm.Width;
43 int height = mainForm.Height;
44
45 this.mainForm.Width = height;
46 this.mainForm.Height = width;
47 }
48
49 // 使用主显示器显示窗口
50 Screen screen = Screen.FromControl(this.mainForm);
51
52 Rectangle intersect = Rectangle.Intersect(screen.Bounds, mainForm.Bounds);
53 if (intersect.Width == 0 || intersect.Height == 0)
54 {
55 mainForm.Location = new Point(screen.Bounds.Left + 16, screen.Bounds.Top + 16);
56 }
57
58 // 修正窗体尺寸
59 if (this.mainForm.Width < 200)
60 {
61 this.mainForm.Width = 200; // this value was chosen arbitrarily
62 }
63
64 if (this.mainForm.Height < 200)
65 {
66 this.mainForm.Height = 200; // this value was chosen arbitrarily
67 }
68
69 this.mainForm.SingleInstanceManager = singleInstanceManager;
70 singleInstanceManager = null; // mainForm owns it now
71
1 // 检查系统版本
2 if (!OS.CheckOSRequirement())
3 {
4 string message = PdnResources.GetString("Error.OSRequirement");
5 Utility.ErrorBox(null, message);
6 return;
7 }
8
9 // 检查启动参数是否含有升级参数
10 if (this.args.Length == 1 &&
11 this.args[0] == Updates.UpdatesOptionsDialog.CommandLineParameter)
12 {
13 Updates.UpdatesOptionsDialog.ShowUpdateOptionsDialog(null, false);
14 }
15 else
16 {
17 SingleInstanceManager singleInstanceManager = new SingleInstanceManager("PaintDotNet");
18
19 // If this is not the first instance of PDN.exe, then forward the command-line
20 // parameters over to the first instance.
21 if (!singleInstanceManager.IsFirstInstance)
22 {
23 singleInstanceManager.FocusFirstInstance();
24
25 foreach (string arg in this.args)
26 {
27 singleInstanceManager.SendInstanceMessage(arg, 30);
28 }
29
30 singleInstanceManager.Dispose();
31 singleInstanceManager = null;
32
33 return;
34 }
35
36 // 构造新窗口
37 this.mainForm = new MainForm(this.args);
38
39 // 设置屏幕旋转方位及主窗体尺寸
40 if (this.mainForm.ScreenAspect < 1.0)
41 {
42 int width = mainForm.Width;
43 int height = mainForm.Height;
44
45 this.mainForm.Width = height;
46 this.mainForm.Height = width;
47 }
48
49 // 使用主显示器显示窗口
50 Screen screen = Screen.FromControl(this.mainForm);
51
52 Rectangle intersect = Rectangle.Intersect(screen.Bounds, mainForm.Bounds);
53 if (intersect.Width == 0 || intersect.Height == 0)
54 {
55 mainForm.Location = new Point(screen.Bounds.Left + 16, screen.Bounds.Top + 16);
56 }
57
58 // 修正窗体尺寸
59 if (this.mainForm.Width < 200)
60 {
61 this.mainForm.Width = 200; // this value was chosen arbitrarily
62 }
63
64 if (this.mainForm.Height < 200)
65 {
66 this.mainForm.Height = 200; // this value was chosen arbitrarily
67 }
68
69 this.mainForm.SingleInstanceManager = singleInstanceManager;
70 singleInstanceManager = null; // mainForm owns it now
71
做完以上,就启动主窗体了!
总结一下,在PDN程序启动时,做了以下事情:
注册应用程序未捕捉异常事件、使用启动参数调用相应方法启动窗体、设置本地化资源、为主窗体显示尺寸及位置校正。