日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

一个Win32 API实例类(代码收集)

發布時間:2025/3/20 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 一个Win32 API实例类(代码收集) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

  最近看到別人代碼中一個很好的功能類,該類是一個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?????}

?

?

總結

以上是生活随笔為你收集整理的一个Win32 API实例类(代码收集)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。