The call of API functions in Windows is sometimes essential in programming. Various programming languages have standardized the calling methods and interfaces, the calling method in C # is as follows (the following programming environment is Visual Studio. net ):
1. Add a new class item to the project, open the class file, and add a reference to the following namespace in the file header:
Using system. runtime. interopservices;
In the class definition subject, the reference to the API is added to the static call method. The following API call is used as an example:
/// <Summary>
/// Open and close the CD tray.
/// </Summary>
[Dllimport ("winmm. dll", entrypoint = "mcisendstring", charset = charset. Auto)]
Public static extern int mcisendstring (string lpstrcommand, string lpstrreturnstring, int ureturnlength, int hwndcallback );
/// <Summary>
/// Display and hide the mouse pointer.
/// </Summary>
[Dllimport ("user32.dll", entrypoint = "showcursor", charset = charset. Auto)]
Public static extern int showcursor (INT bshow );
/// <Summary>
/// Clear the recycle bin.
/// </Summary>
[Dllimport ("shell32.dll", entrypoint = "shemptyrecyclebin", charset = charset. Auto)]
Public static extern long shemptyrecyclebin (intptr hwnd, string pszrotpath, long dwflags );
/// <Summary>
/// Open the browser
/// </Summary>
[Dllimport ("shell32.dll", entrypoint = "ShellExecute", charset = charset. Auto)]
Public static extern int ShellExecute (intptr hwnd, string lpoperation, string lpfile, string lpparameters, string lpdirectory, int nshowcmd );
/// <Summary>
/// Maximize the window, minimize the window, normal size window;
/// </Summary>
[Dllimport ("user32.dll", entrypoint = "showwindow", charset = charset. Auto)]
Public static extern int showwindow (intptr hwnd, int ncmdshow );
2. With the above file, you can call the above API in the event processing of your own form object. The method is as follows:
The following strreturn is a public variable of the string type. apicils refers to the class name created in the first step.
Open the CD tray:
Long lngreturn = apicils. mcisendstring ("set cdaudio door open", strreturn, 127, 0 );
Close the CD tray:
Long lngreturn = apicils. mcisendstring ("set cdaudio door closed", strreturn, 127, 0 );
Show the mouse pointer in the application form:
Apicwalls. showcursor (1 );
Hide the mouse pointer in the application form:
Apicwalls. showcursor (0 );
Clear the recycle bin:
Apicils. shemptyrecyclebin (Form. activeform. Handle, "", 0x00000000 );
Open the browser window. textbox1.text indicates the URL to be accessed:
Long lngreturn = apicils. ShellExecute (Form. activeform. Handle, "open", textbox1.text, "", "", 1 );
Maximum window:
Apicwalls. showwindow (Form. activeform. Handle, 3 );
Minimize window:
Apicwalls. showwindow (Form. activeform. Handle, 2 );
Restore normal size window:
Apicwalls. showwindow (Form. activeform. Handle, 1 );
[Reprinted from http://www.wangchao.net.cn/bbsdetail_28075.html]