Windows Manager program (2)

Source: Internet
Author: User
Tags checkstyle

1. traverse all visible forms on the desktop.

This function can be implemented through the Windows Api.

First, define the Api EnumWindows. EnumWindows is used to traverse all windows and return through the callback function, as shown below:

Private delegate bool WNDENUMPROC (IntPtr hWnd, int lParam );
[DllImport ("user32.dll")]
Private static extern bool EnumWindows (WNDENUMPROC lpEnumFunc, int lParam );

 

Then the. Code is called as follows:

Public void GetProcessInfo ()
{
Try
{
This. UpdateList (3, "", false );
EnumWindows (new WNDENUMPROC (GerProcessInfo_back), 0 );
This. ShowStatus ("Process Information Retrieval ends ");
}
Catch (System. Exception exp)
{
This. ShowStatus ("error in obtaining process information:" + exp. Message );
}
Finally
{
ShowButtonStatus (1, true );

}
}

 

Public bool GerProcessInfo_back (IntPtr handle, int lpararm)
{
If (handle. ToInt32 () = this. meHandle)
{
Return true;
}

If (APIs. GetWindow (handle, APIs. getwindow_assist.gw_owner) = (IntPtr) 0 & APIs. IsWindowVisible (handle) & CheckStyle (handle ))
{
StringBuilder sb = new StringBuilder (256 );
APIs. GetWindowTextW (handle, sb, sb. Capacity );
String title = sb. ToString ();
If (title! = "")
{
This. UpdateList (1, title, "", handle. ToInt32 (). ToString (), false );
}
}
Return true;

}

During callback, you also need to determine or obtain whether the Handle is a sub-form and whether it is visible. If it is not a sub-form and visible, it is displayed in the list.

You can use the GetWindow Api to determine whether a sub-form can be used. If it is Get GW_OWNER, It is 0, indicating that it is already the main form.

Determine whether it is visible. You can use the IsWindowVisble Api

CheckStyle is a method used to determine whether the form is a popup.

Public bool CheckStyle (IntPtr handle)
{

Int Style = APIs. GetWindowLong (handle,-10 );
Console. WriteLine (Style. ToString ());
If (Style & APIs. WindowStyles. WS_POPUP) = APIs. WindowStyles. WS_POPUP)
{

If (Style & APIs. WindowStyles. WS_GROUP) = APIs. WindowStyles. WS_GROUP) & (Style & APIs. WindowStyles. WS_DISABLED )! = APIs. WindowStyles. WS_DISABLED & (Style & APIs. WindowStyles. WS_SYSMENU) = APIs. WindowStyles. WS_SYSMENU)
{
Return true;
}
Else
{
Return false;
}
}
Else
{
Return true;
}

}
The preceding method can be used to traverse all visible forms on all the desktops and save the Handle of each form in the List.

2. unpin the form to the top

This method is also displayed using the Api, because the Handle of each form has been saved through the above method.

Therefore, you can use the SetWindowPos Api to implement this function.

The Api is defined as follows:

[DllImportAttribute ("user32.dll")]
Public static extern bool SetWindowPos (IntPtr hwnd, int hwnd2, int x, int y, int cx, int cy, int uFlags );

If (hide)
{
IntPtr iprt = (IntPtr) int. Parse (item. SubItems [1]. Text );
APIs. setWindowPos (iprt,-1, 0, 0, 0, (int) (APIs. SWP. SWP_NOACTIVATE | APIs. SWP. SWP_SHOWWINDOW | APIs. SWP. SWP_NOMOVE | APIs. SWP. SWP_NOSIZE ));
}
Else
{
IntPtr iprt = (IntPtr) int. Parse (item. SubItems [1]. Text );
APIs. setWindowPos (iprt,-2, 0, 0, 0, 0, (int) (APIs. SWP. SWP_NOACTIVATE | APIs. SWP. SWP_SHOWWINDOW | APIs. SWP. SWP_NOMOVE | APIs. SWP. SWP_NOSIZE ));

}

SWP is defined as follows:

Public enum SWP: int
{
SWP_NOSIZE = 0 x0001,
SWP_NOMOVE = 0x0002,
SWP_NOZORDER = 0x0004,
SWP_NOREDRAW = 0x0008,
SWP_NOACTIVATE = 0x0010,
SWP_FRAMECHANGED = 0x0020,
SWP_SHOWWINDOW = 0x0040,
SWP_HIDEWINDOW = 0x0080,
SWP_NOCOPYBITS = 0x0100,
SWP_NOOWNERZORDER = 0x0200,
SWP_NOSENDCHANGING = 0x0400,
SWP_DEFERERASE = 0x2000,
SWP_ASYNCWINDOWPOS = 0x4000
}

 

3. Display and hide forms

This function uses the ShowWindow method of the Api. The method is defined as follows:

[DllImport ("user32.dll")]
Public static extern bool ShowWindow (IntPtr hWnd, ShowWindowStyles State );
# Region ShowWindow Styles
Public enum ShowWindowStyles: short
{
HIDE = 0,
SHOWNORMAL = 1,
NORMAL = 1,
SHOWMINIMIZED = 2,
SHOWMAXIMIZED = 3,
MAXIMIZE = 3,
SHOWNOACTIVATE = 4,
SHOW = 5,
MINIMIZE = 6,
SHOWMINNOACTIVE = 7,
SHOWNA = 8,
RESTORE = 9,
SHOWDEFAULT = 10,
FORCEMINIMIZE = 11,
MAX = 11
}
# Endregion

Call method:

APIs. ShowWindow (iprt, APIs. ShowWindowStyles. HIDE );

APIs. ShowWindow (iprt, APIs. ShowWindowStyles. SHOW );

The Handle of the form corresponding to iprt.

4. Get the corresponding Process through the Handle of the form and close the Process.

 

The method is as follows:

IntPtr iprt = (IntPtr) int. Parse (item. SubItems [1]. Text );
Uint pid = 0;

// Obtain the corresponding ProcessID
APIs. GetWindowThreadProcessId (iprt, out pid );
If (pid! = 0)
{
Try
{

// Obtain the corresponding Process
Process p = Process. GetProcessById (int) pid );

// Kill the process

P. Kill ();
}
Catch {}
}

This is the first time you write this article. Next time, we will introduce how to set global mouse hooks and global shortcut keys.

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.