zoukankan      html  css  js  c++  java
  • 谈论 在C#中调用windows API函数 [引]

     

    引用

    在C#中调用windows API函数 [引]

     

     对于windows 系统API函数的调用在程序设计中有时是必不可少的,各种编程语言都规范了调用的方法和接口,在C#语言中的调用方法如下(以下编程环境为Visual Studio .NET):

    1、  在工程项目中添加一个类新项,打开这个类文件,在文件头部加入对以下命名空间的引用:

         using System.Runtime.InteropServices; 

         在类定义主体中,以静态调用的方式加入对API的引用,本文以下的API调用为例:

            /// <summary>

                  /// 打开和关闭CD托盘.

                  /// </summary>

                  [DllImport("winmm.dll" , EntryPoint="mciSendString", CharSet=CharSet.Auto)]

                  public  static extern int  mciSendString (string  lpstrCommand,string  lpstrReturnstring ,int  uReturnLength,int  hwndCallback);

          

                  /// <summary>

                  /// 显示和隐藏鼠标指针.

                  /// </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、  有了上面的文件后,就可以在自己的窗体对象的事件处理中调用以上的API,方法如下:

         以下strReturn是string类型的公有变量,ApiCalls指代第一步创建的类名。

    打开CD托盘:

    long lngReturn = ApiCalls.mciSendString("set CDAudio door open", strReturn, 127, 0);

         关闭CD托盘:

    long lngReturn = ApiCalls.mciSendString("set CDAudio door closed", strReturn, 127, 0);

    在应用程序窗体中显示鼠标指针:

    ApiCalls.ShowCursor(1);

    在应用程序窗体中隐藏鼠标指针:

    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);

    恢复正常大小窗口:

    ApiCalls.ShowWindow(Form.ActiveForm.Handle,1);

  • 相关阅读:
    洛谷 P2922 [USACO08DEC]秘密消息Secret Message
    HDU 1542 Atlantis
    洛谷 P2146 软件包管理器
    rabbitmq
    POJ——T2446 Chessboard
    洛谷—— P3375 【模板】KMP字符串匹配
    洛谷——P3370 【模板】字符串哈希
    POJ——T1860 Currency Exchange
    洛谷—— P3386 【模板】二分图匹配
    python(1)- 初识python
  • 原文地址:https://www.cnblogs.com/draeag/p/876138.html
Copyright © 2011-2022 走看看