API抓屏

來源:互聯網
上載者:User

標籤:

需調用API函數

需在開頭引入命名空間 using System.Runtime.InteropServices;

 

擷取當前視窗控制代碼:GetForegroundWindow()

[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)] public static extern IntPtr GetForegroundWindow();

 

傳回值類型是IntPtr,即為當前獲得焦點視窗的控制代碼

使用方法 : IntPtr myPtr=GetForegroundWindow();

擷取到該視窗控制代碼後,可以對該視窗進行操作.比如,關閉該視窗或在該視窗隱藏後,使其顯示

[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)] public static extern int ShowWindow(IntPtr hwnd, int nCmdShow);

 

其中ShowWindow(IntPtr hwnd, int nCmdShow);

nCmdShow的含義

0 關閉視窗

1 正常大小顯示視窗

2 已最小化的視窗

3 已最大化的視窗

使用執行個體: ShowWindow(myPtr, 0);

 

擷取視窗大小及位置:需要調用方法GetWindowRect(IntPtr hWnd, ref RECT lpRect)

[DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)] static extern bool GetWindowRect(IntPtr hWnd, ref RECT lpRect);

[StructLayout(LayoutKind.Sequential)] public struct RECT { public int Left; //最左座標 public int Top; //最上座標 public int Right; //最右座標 public int Bottom; //最下座標 }

 

樣本:

InPtr awin = GetForegroundWindow(); //擷取當前視窗控制代碼 RECT rect = new RECT(); GetWindowRect(awin, ref rect); int width = rc.Right - rc.Left; //視窗的寬度 int height = rc.Bottom - rc.Top; //視窗的高度 int x = rc.Left; int y = rc.Top;

API抓屏

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.