A Win32 API instance class (Code collection)

Source: Internet
Author: User

See others recentlyCodeA good function class, which is a Win32 API instance class, which includes: the sameProgramDo not start multiple times; obtain any form; restore form status; set form focus, etc.

This class is very practical. Share it with you:

1 /// Summary Description for processutils.
2 Public   Static   Class Processutils
3 {
4 Private   Static 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
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 : Financialas (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 }

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.