zoukankan      html  css  js  c++  java
  • C#中调用windows API函数总结

         我们在程序设计过程中,对Windows API函数调用是比不可少的,各种编程语言都规范了调用的方法和接口,在C#语言中的调用方法如下。

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

       using System.Runtime.InteropServices; 


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

          1、打开和关闭CD托盘


       /// <summary>
       /// 打开和关闭CD托盘
       /// </summary>
       [DllImport("winmm.dll" , EntryPoint="mciSendString", CharSet=CharSet.Auto)]
       public static extern int mciSendString (string lpstrCommand,string lpstrReturnstring ,int uReturnLength,int hwndCallback);
       

          2、显示和隐藏鼠标指针


       /// <summary>
       /// 显示和隐藏鼠标指针
       /// </summary>
       [DllImport("user32.dll", EntryPoint="ShowCursor", CharSet=CharSet.Auto)]
       public static extern int ShowCursor(int bShow);

          3、清空回收站


       /// <summary>
       /// 清空回收站
       /// </summary>
       [DllImport("shell32.dll", EntryPoint="SHEmptyRecycleBin", CharSet=CharSet.Auto)]
       public static extern long SHEmptyRecycleBin(IntPtr hwnd, string pszRootPath, long dwFlags);

          4、打开浏览器


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

          5、最大化窗口,最小化窗口,正常大小窗口

       /// <summary>
       /// 最大化窗口,最小化窗口,正常大小窗口;
       /// </summary>
       [DllImport("user32.dll", EntryPoint="ShowWindow", CharSet=CharSet.Auto)]
       public static extern int ShowWindow(IntPtr hwnd,int nCmdShow);
      

         5、模拟鼠标鼠标事件

       using System.Runtime.InteropServices;
       [DllImport("user32.dll")] 
       static extern void mouse_event(MouseEventFlag flags, int dx, int dy, uint data, UIntPtr extraInfo); 

       5、模拟键盘事件

            [DllImport("user32.dll")]

            static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, uint dwExtraInfo);

            [DllImport("user32.dll")]

            static extern byte MapVirtualKey(byte wCode, int wMap);

      二、 有了上面的文件后,就可以在自己的窗体对象的事件处理中调用以上的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);

        

        模拟鼠标事件

         

     1 using System.Runtime.InteropServices;
     2 [DllImport("user32.dll")] 
     3 static extern void mouse_event(MouseEventFlag flags, int dx, int dy, uint data, UIntPtr extraInfo); 
     4 [Flags] 
     5 enum MouseEventFlag : uint 
     6 { 
     7 Move = 0x0001, 
     8 LeftDown = 0x0002, 
     9 LeftUp = 0x0004, 
    10 RightDown = 0x0008, 
    11 RightUp = 0x0010, 
    12 MiddleDown = 0x0020, 
    13 MiddleUp = 0x0040, 
    14 XDown = 0x0080, 
    15 XUp = 0x0100, 
    16 Wheel = 0x0800, 
    17 VirtualDesk = 0x4000, 
    18 Absolute = 0x8000 
    19 }
    20 mouse_event(MouseEventFlag.Move, 0, 0, 0, UIntPtr.Zero)
    21 mouse_event(MouseEventFlag.LeftDown, 0, 0, 0, UIntPtr.Zero); 
    22 mouse_event(MouseEventFlag.LeftUp, 0, 0, 0, UIntPtr.Zero);


    模拟键盘事件



     1 [DllImport("user32.dll")]
     2         static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, uint dwExtraInfo);
     3         [DllImport("user32.dll")]
     4         static extern byte MapVirtualKey(byte wCode, int wMap);
     5         private void button2_Click(object sender, EventArgs e)
     6         {
     7             Process.Start(@"cs.txt");
     8             Thread.Sleep(Convert.ToInt32(2 * 1000));//开起程序后等待
     9             keybd_event(18, MapVirtualKey(18, 0), 0, 0); //按下CTRL鍵。   
    10             //keybd_event(70, MapVirtualKey(70, 0), 0, 0);//鍵下f鍵。   
    11             //keybd_event(70, MapVirtualKey(70, 0), 0x2, 0);//放開f鍵。  0x35 
    12             Thread.Sleep(Convert.ToInt32(10 * 1000));//开起程序后等待
    13             keybd_event(18, MapVirtualKey(18, 0), 0x2, 0);//放開CTRL鍵。   
    14         }
  • 相关阅读:
    input中只能填数字的限制
    免登陆:保存登录页面的用户名密码,用md5对密码加密保存
    SQL 插入日期时间 变量值
    Debugger Exception Notification
    CoHTMLDocument
    json数组,随便测试
    显示时间的毫秒
    赋值后再调整动态数组的元素个数
    数据集和非数据感知控件使用
    FR报表 自动缩小的代码
  • 原文地址:https://www.cnblogs.com/dawn-cn/p/4088871.html
Copyright © 2011-2022 走看看