今天找到一個名叫LICEcap的錄屏軟件,錄制界面是這樣的:
這個炫酷的空心窗口就是鏡頭,調整好大小,然后對準需要錄制的地方按下Record就可以生成gif了。
臥槽太NB了我也要做一個!
根據StackOverflow站的提示(在這里),我們需要使用一個在Windows2000及之后平臺可用的,用以實現不規則窗體的分層窗口API (SetLayerWindowAttributes).根據百度我們先需要使用一個名為SetWindowLong的Win32 API來把窗體設定為分層窗體。
為了在.NET平臺中調用Win32 API,我們需要復習下P/Invoke的內容:
1.什么是P/Invoke
P/Invoke 的全稱是Platform Invoke。.是一種在托管平臺下使用非托管dll中導出函數的一種調用機制。
2.如何用P/Invoke
它長這樣:
[DllImportAttribute("user32.dll", EntryPoint="SetCursorPos")]public static extern bool SetCursorPos(int X, int Y) ;
依次指明調用的dll名稱,導出函數名,然后定義成C#標準的方法就行了。
所以,我們需要: 打開百度百科,搜索API名稱,查看宿主dll,抄來函數原型,按照說明定義需要的常量。
不,我找到了更方便的辦法:打開pinvoke.net,搜索API名稱:
按照里邊的C#Signature復制過來,再根據Sample Code改改,就OK了。
然后在Visual Studio里新建一個Winform項目,在主窗口代碼里這樣寫:
public partial class Form1 : Form{public Form1(){InitializeComponent();this.TopMost = true;SetWindowLong(this.Handle, GWL_EXSTYLE, WS_EX_LAYERED);SetLayeredWindowAttributes(this.Handle, 65280, 255, LWA_COLORKEY);}private const uint WS_EX_LAYERED = 0x80000;private const int GWL_EXSTYLE = -20;private const int LWA_COLORKEY = 1;[DllImport("user32", EntryPoint = "SetWindowLong")]private static extern uint SetWindowLong(IntPtr hwnd,int nIndex,uint dwNewLong);[DllImport("user32", EntryPoint = "SetLayeredWindowAttributes")]private static extern int SetLayeredWindowAttributes(IntPtr hwnd,int crKey,int bAlpha,int dwFlags);}
先使用SetWindowLong將窗口定義為分層窗體,然后調用SetLayeredWindowAttributes方法設置透明。
其中第二個參數crKey為一個int型的顏色值,轉換方式為(int)(0xRRGGBB),本例中Dec(0x00FF00)=65280為綠色。
第四個參數為透明方式,本例中使用LWA_COLORKEY = 1,表示將該窗口顏色為crKey的部分都設置為透明。
因此相應地,我們需要在窗口設計器中畫一個顏色為綠色的方塊。本例中使用了一個PictureBox,并設置了背景顏色。
F5運行,效果如圖:
能想到的用處之一就是,包裝幾個不相關的外部程序為一個整體.
新聞熱點
疑難解答