一个Win32 API实例类(代码收集)
2010-04-13 20:57
435 查看
最近看到别人代码中一个很好的功能类,该类是一个Win32 API实例类,该类功能包括:同一程序禁止启动多次;获取任意窗体;恢复窗体状态;设置窗体焦点等。
该类很实用,与大家分享一下:
1 /// Summary description for ProcessUtils.
2 public static class ProcessUtils
3 {
4 private static Mutex mutex = null;
5
6 /// Determine if the current process is already running
7 public static bool ThisProcessIsAlreadyRunning()
8 {
9 // Only want to call this method once, at startup.
10 Debug.Assert(mutex == null);
11
12 // createdNew needs to be false in .Net 2.0, otherwise, if another instance of
13 // this program is running, the Mutex constructor will block, and then throw
14 // an exception if the other instance is shut down.
15 bool createdNew = false;
16
17 mutex = new Mutex(false, Application.ProductName, out createdNew);
18
19 Debug.Assert(mutex != null);
20
21 return !createdNew;
22 }
23
24 [DllImport("user32.dll", SetLastError = true)]
25 static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
26
27 [DllImport("user32.dll")]
28 [return: MarshalAs(UnmanagedType.Bool)]
29 static extern bool SetForegroundWindow(IntPtr hWnd);
30
31 [DllImport("user32.dll")]
32 static extern bool IsIconic(IntPtr hWnd);
33
34 [DllImport("user32.dll")]
35 static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
36
37 const int SW_RESTORE = 9;
38
39 [DllImport("user32.dll")]
40 static extern IntPtr GetLastActivePopup(IntPtr hWnd);
41
42 [DllImport("user32.dll")]
43 static extern bool IsWindowEnabled(IntPtr hWnd);
44
45 /// Set focus to the previous instance of the specified program.
46 public static void SetFocusToPreviousInstance(string windowCaption)
47 {
48 // Look for previous instance of this program.
49 IntPtr hWnd = FindWindow(null, windowCaption);
50
51 // If a previous instance of this program was found...
52 if (hWnd != null)
53 {
54 // Is it displaying a popup window?
55 IntPtr hPopupWnd = GetLastActivePopup(hWnd);
56
57 // If so, set focus to the popup window. Otherwise set focus
58 // to the program's main window.
59 if (hPopupWnd != null && IsWindowEnabled(hPopupWnd))
60 {
61 hWnd = hPopupWnd;
62 }
63
64 SetForegroundWindow(hWnd);
65
66 // If program is minimized, restore it.
67 if (IsIconic(hWnd))
68 {
69 ShowWindow(hWnd, SW_RESTORE);
70 }
71 }
72 }
73 }
该类很实用,与大家分享一下:
1 /// Summary description for ProcessUtils.
2 public static class ProcessUtils
3 {
4 private static Mutex mutex = null;
5
6 /// Determine if the current process is already running
7 public static bool ThisProcessIsAlreadyRunning()
8 {
9 // Only want to call this method once, at startup.
10 Debug.Assert(mutex == null);
11
12 // createdNew needs to be false in .Net 2.0, otherwise, if another instance of
13 // this program is running, the Mutex constructor will block, and then throw
14 // an exception if the other instance is shut down.
15 bool createdNew = false;
16
17 mutex = new Mutex(false, Application.ProductName, out createdNew);
18
19 Debug.Assert(mutex != null);
20
21 return !createdNew;
22 }
23
24 [DllImport("user32.dll", SetLastError = true)]
25 static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
26
27 [DllImport("user32.dll")]
28 [return: MarshalAs(UnmanagedType.Bool)]
29 static extern bool SetForegroundWindow(IntPtr hWnd);
30
31 [DllImport("user32.dll")]
32 static extern bool IsIconic(IntPtr hWnd);
33
34 [DllImport("user32.dll")]
35 static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
36
37 const int SW_RESTORE = 9;
38
39 [DllImport("user32.dll")]
40 static extern IntPtr GetLastActivePopup(IntPtr hWnd);
41
42 [DllImport("user32.dll")]
43 static extern bool IsWindowEnabled(IntPtr hWnd);
44
45 /// Set focus to the previous instance of the specified program.
46 public static void SetFocusToPreviousInstance(string windowCaption)
47 {
48 // Look for previous instance of this program.
49 IntPtr hWnd = FindWindow(null, windowCaption);
50
51 // If a previous instance of this program was found...
52 if (hWnd != null)
53 {
54 // Is it displaying a popup window?
55 IntPtr hPopupWnd = GetLastActivePopup(hWnd);
56
57 // If so, set focus to the popup window. Otherwise set focus
58 // to the program's main window.
59 if (hPopupWnd != null && IsWindowEnabled(hPopupWnd))
60 {
61 hWnd = hPopupWnd;
62 }
63
64 SetForegroundWindow(hWnd);
65
66 // If program is minimized, restore it.
67 if (IsIconic(hWnd))
68 {
69 ShowWindow(hWnd, SW_RESTORE);
70 }
71 }
72 }
73 }
相关文章推荐
- Win32 API 多线程编程——一个简单实例(含消息参数传递)
- Zookeeper实例Curator API-使用curator来创建一个含隔离命名空间的ZooKeeper客户端
- 判断输入的一个数是否为回文数c++代码实例及运行结果
- 一个简单的Ext.XTemplate的实例代码
- 同一个midlet下不同canvas切换实例代码
- PHP图灵机器人问答机器人API调用代码实例 4000
- 基于JAVA的万年历API调用代码实例源码
- 微信小程序 canvas API详解及实例代码
- 一个PHP并发访问实例代码
- js 计时器,倒计时完整实例代码附效果图 制作一个当前时间的动态时钟
- 一个js控制的导航菜单实例代码
- [专家讲座] ArcGIS API for Silverlight开发入门(2):一个基础地图实例(转)
- js查看一个函数的执行时间实例代码
- 使用库函数API和C代码中嵌入汇编代码两种方式使用同一个系统调用
- 实验四——使用库函数API和C代码中嵌入汇编代码两种方式使用同一个系统调用
- linux内核分析作业4:使用库函数API和C代码中嵌入汇编代码两种方式使用同一个系统调用
- WIN32 API写一个窗口
- web中收集用户数据实例代码
- asp.net 将一个图片以二进制值的形式存入Xml文件中的实例代码
- 多个TermQuery或一个MultiFieldQueryParser构建BooleanQuery多个域的检索学习实例代码