close
- 說明
- 在WPF透明視窗或傳統WindowsForm不全透明部分可以被游標點擊到,如果要做到純顯示的視窗(如桌面工具、桌面歌詞等...)是無法直接用Xaml或者屬性設定來達成
- 需要引用到Windows API中user32.dll的Get/SetWindowLong
- 概觀
- 引用user32.dll中的Get/SetWindowLong
- 取得欲設定Window之Handle(句抦 簡稱為Hwnd)
- 使用GetWindowLong對該Hwnd視窗取得-20屬性,該值為可以點擊到的值
- 將取得的值使用|(or)符號進行|32(就是+32,只是如果本來就點不到會沒事)
- 使用SetWindowLong將該Hwnd視窗取得值|32後設定回去-20屬性上就會點不到該視窗
- 實作
- 引用dll內的Get/SetWindowLong
- 要using System.Runtime.InteropServices;才可使用DllImport
- 可以直接將function如Win32 class中的function 引入而不用Win32包覆起來(就不需要使用static),如下這樣做則用Win.Get/SetWindowLong來存取
-
using System.Runtime.InteropServices; namespace digitClock { class Win32 { [DllImport("user32.dll")] public static extern int SetWindowLong(int hwnd, int nIndex, int dwNewLong); [DllImport("user32.dll")] public static extern int GetWindowLong(int hwnd, int nIndex); } }
- 取得Handle使用
-
System.IntPtr hwnd = new System.Windows.Interop.WindowInteropHelper(this).Handle;
- | 32 (將原始值存入extendedStyle[1] +32的值存入extendedStyle[0]
-
this.extendedStyle[1] = Win32.GetWindowLong(hwnd.ToInt32(), -20); this.extendedStyle[0] = this.extendedStyle[1] | 32;
- 設定視窗屬性 hwnd為物件所以要Toint32轉成數值,透過剛剛取得的extendStyle陣列設定([0]為|32過的[隱藏的] [1]為原始值(不隱藏) ,布林值需要使用Convert轉換成Int才能做index
-
private void touchableSet(bool touchable) { Win32.SetWindowLong(hwnd.ToInt32(), -20, extendedStyle[Convert.ToInt32(touchable)]); }
- 以下是使用物件之Check屬性來做index
文章標籤
全站熱搜