轉(zhuǎn)帖|其它|編輯:郝浩|2010-10-19 15:42:42.000|閱讀 861 次
概述:對(duì)于windows 系統(tǒng)API函數(shù)的調(diào)用在程序設(shè)計(jì)中有時(shí)是必不可少的,各種編程語言都規(guī)范了調(diào)用的方法和接口,本文將介紹在C#語言中的調(diào)用方法,希望對(duì)大家有幫助。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
對(duì)于windows 系統(tǒng)API函數(shù)的調(diào)用在程序設(shè)計(jì)中有時(shí)是必不可少的,各種編程語言都規(guī)范了調(diào)用的方法和接口,在C#語言中的調(diào)用方法如下(以下編程環(huán)境為Visual Studio .NET):
1、 在工程項(xiàng)目中添加一個(gè)類新項(xiàng),打開這個(gè)類文件,在文件頭部加入對(duì)以下命名空間的引用:
using System.Runtime.InteropServices;
在類定義主體中,以靜態(tài)調(diào)用的方式加入對(duì)API的引用,本文以下的API調(diào)用為例:
/// <summary>
/// 打開和關(guān)閉CD托盤
/// </summary>
[DllImport("winmm.dll" , EntryPoint="mciSendString", CharSet=CharSet.Auto)]
public static extern int mciSendString (string lpstrCommand,string lpstrReturnstring ,int uReturnLength,int hwndCallback);
/// <summary>
/// 顯示和隱藏鼠標(biāo)指針.
/// </summary>
[DllImport("user32.dll", EntryPoint="ShowCursor", CharSet=CharSet.Auto)]
public static extern int ShowCursor(int bShow);
/// <summary>
/// 清空回收站.
/// </summary>
[DllImport("shell32.dll", EntryPoint="SHEmptyRecycleBin", CharSet=CharSet.Auto)]
public static extern long SHEmptyRecycleBin(IntPtr hwnd, string pszRootPath, long dwFlags);
/// <summary>
/// 打開瀏覽器
/// </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>
/// 最大化窗口,最小化窗口,正常大小窗口;
/// </summary>
[DllImport("user32.dll", EntryPoint="ShowWindow", CharSet=CharSet.Auto)]
public static extern int ShowWindow(IntPtr hwnd,int nCmdShow);
2、 有了上面的文件后,就可以在自己的窗體對(duì)象的事件處理中調(diào)用以上的API,方法如下:
以下strReturn是string類型的公有變量,ApiCalls指代第一步創(chuàng)建的類名。
打開CD托盤:
long lngReturn = ApiCalls.mciSendString("set CDAudio door open", strReturn, 127, 0);
關(guān)閉CD托盤:
long lngReturn = ApiCalls.mciSendString("set CDAudio door closed", strReturn, 127, 0);
在應(yīng)用程序窗體中顯示鼠標(biāo)指針:
ApiCalls.ShowCursor(1);
在應(yīng)用程序窗體中隱藏鼠標(biāo)指針:
ApiCalls.ShowCursor(0);
清空回收站:
ApiCalls.SHEmptyRecycleBin(Form.ActiveForm.Handle,"",0x00000000);
打開瀏覽器窗口,textBox1.Text中表示要訪問的URL地址:
Long lngReturn= ApiCalls.ShellExecute(Form.ActiveForm.Handle,"Open",textBox1.Text,"","",1);
最大化窗口:
ApiCalls.ShowWindow(Form.ActiveForm.Handle,3);
最小化窗口:
ApiCalls.ShowWindow(Form.ActiveForm.Handle,2);
恢復(fù)正常大小窗口:
ApiCalls.ShowWindow(Form.ActiveForm.Handle,1);
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自:網(wǎng)絡(luò)轉(zhuǎn)載