善于 调用Windows API
生活随笔
收集整理的這篇文章主要介紹了
善于 调用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、這是模擬按鍵 按下的方法
? ? ??
PS: 其中第三個參數有三種取值:
-
-
- 0:按下;
- 1:擴展鍵;
- 2:彈起。
-
?
?
?
3.相關實例
? ? ??
?
? ? ??
?
?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的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java:类与继承
- 下一篇: Jenkins TFS配置