Call Win32 function enumwindows enumeration all windows in C #

xiaoxiao2021-03-06  54

We all know that Microsoft .NET, C # is the first language used to develop network solutions. However, in the fat client, C # also has excellent performance. For those programmers who are familiar with Win32 API, how is the key to the new characteristics of C # with the Win32 API.

The following example shows how to call WIN32 API in C #, all windows:

1. First, you need to declare a delegate function for Win32 API - EnumWindows callback function:

Public Delegate Bool Callback (int hwnd, int lparam);

2. Then use the platform in the C # to call the declaration from the USER32.DLL library, please refer to the MSDN - Win32 API for the specific parameters.

[DLLIMPORT ("User32")]]

Public Static Extern Int EnumWindows (Callback X, INT Y);

3. Finally, instantiate the entrustment, calling EnumWindows.

Callback mycallback = new callback (enumwindowsapp.report);

4. The full code is as follows:

Using system;

Using system.Runtime.InteropServices;

Public Delegate Bool Callback (int hwnd, int lparam);

Public Class EnumWindowsApp

{

[DLLIMPORT ("User32")]]

Public Static Extern Int EnumWindows (Callback X, INT Y);

Public static void main ()

{

Callback mycallback = new callback (enumwindowsapp.report);

ENUMWINDOWS (MyCallback, 0);

}

Public Static Bool Report (int hwnd, int lparam)

{

Console.write ("Window Handle IS:");

Console.WriteLine (hwnd);

Return True;

}

}

Note: Please refer to Visual .NET HELP for details on "Principal", "Platform Call" and "Instantiation".

转载请注明原文地址:https://www.9cbs.com/read-116658.html

New Post(0)