日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > windows >内容正文

windows

善于 调用Windows API

發布時間:2025/7/14 windows 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 善于 调用Windows API 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

前一段時間看見別人做的一個自動填寫信息并且點擊登錄的程序,覺得很有意思。

其實就是在程序中調用Windows的API,那么如何調用,下面就做個簡單的介紹。

寫的簡單粗暴, 不喜輕噴。

?

0、首先引入名稱空間System.Runtime.InteropServices用來導入Windows DLL.

?

1、下面是函數原型:

  1.1、這是模擬鼠標按下的方法

[DllImport("user32.dll", EntryPoint = "mouse_event")]public static extern void mouse_event(int dwFlags,int dx,int dy,int cButtons,int dwExtraInfo);

?


  1.2、這是模擬按鍵 按下的方法
? ? ??

[DllImport("user32.dll", EntryPoint = "keybd_event")]public static extern void keybd_event(byte bVk, // 虛擬鍵碼byte bScan, // 該鍵的硬件掃描碼(一般為0 ) dword dwFlags, // 函數操作的各個方面的一個標志位集 dword dwExtralnfo // 與擊鍵相關的附加的32位值 );

      
PS: 其中第三個參數有三種取值:
      •     0:按下;
      •     1:擴展鍵;
      •     2:彈起。

?

?

?

3.相關實例
? ? ??

    const int MOUSEEVENTF_MOVE = 0x0001; //移動鼠標 const int MOUSEEVENTF_LEFTDOWN = 0x0002; //模擬鼠標左鍵按下 const int MOUSEEVENTF_LEFTUP = 0x0004; //模擬鼠標左鍵抬起 const int MOUSEEVENTF_RIGHTDOWN = 0x0008; //模擬鼠標右鍵按下 const int MOUSEEVENTF_RIGHTUP = 0x0010; //模擬鼠標右鍵抬起 const int MOUSEEVENTF_MIDDLEDOWN = 0x0020; //模擬鼠標中鍵按下 const int MOUSEEVENTF_MIDDLEUP = 0x0040;// 模擬鼠標中鍵抬起 const int MOUSEEVENTF_ABSOLUTE = 0x8000; //標示是否采用絕對坐標


       

?




? ? ??

public Form1(){InitializeComponent();int X = 100;int Y = 100;mouse_event( MOUSEEVENTF_RIGHTDOWN, X , Y , 0, 0);mouse_event(MOUSEEVENTF_RIGHTUP, X , Y, 0, 0);X += 10;Y += 65;mouse_event(MOUSEEVENTF_MOVE, X, Y , 0, 0);mouse_event(MOUSEEVENTF_LEFTDOWN, X, Y , 0, 0);mouse_event(MOUSEEVENTF_LEFTUP, X, Y , 0, 0);keybd_event(65, 0, 0, 0);//akeybd_event(66, 0, 1, 0);//bkeybd_event(13, 0, 0, 0);//回車 }

?

?4、一個很實用的例子(實現了粘貼復制的功能)

//相當于按下 Ctrl+C keybd_event(Convert.ToInt32(System.Windows.Forms.Keys.ControlKey), 0, 0, 0); //按下Ctrl keybd_event(Convert.ToInt32(System.Windows.Forms.Keys.C), 0, 0, 0); keybd_event(Convert.ToInt32(System.Windows.Forms.Keys.ControlKey), 0, 0x2, 0);//彈起Ctrl,*******很重要,不然Ctrl會一直處于按下狀態,鍵盤就是失靈,我自己的親身經歷。。 //相當于按下 Ctrl+V keybd_event(Convert.ToInt32(System.Windows.Forms.Keys.ControlKey), 0, 0, 0); //按下Ctrl keybd_event(Convert.ToInt32(System.Windows.Forms.Keys.V), 0, 0, 0); keybd_event(Convert.ToInt32(System.Windows.Forms.Keys.ControlKey), 0, 0x2, 0);//彈起Ctrl

?

?5、其實Windows API還有很多,這里只說到了兩種,下面這些也挺常見

[DllImport("user32.dll")]private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);[DllImport("user32.dll")]private static extern bool SetForegroundWindow(IntPtr hWnd);[DllImport("user32.dll")]private static extern IntPtr FindWindow(string lpClassName,string lpWindowName);[DllImport("user32.dll")]private static extern int SendMessage(IntPtr hWnd,int Msg,int wParam,int lParam);[DllImport("user32.dll")]private static extern bool SetCursorPos(int X, int Y); [DllImport("user32.dll")]private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndlnsertAfter, int X, int Y, int cx, int cy, uint Flags);



?

?6、補充一些沒有說到的

??  用C#調用Windows API向指定窗口發送按鍵消息

?

轉載于:https://www.cnblogs.com/Percy_Lee/p/4847336.html

總結

以上是生活随笔為你收集整理的善于 调用Windows API的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。