簡體   English   中英

C# Winform 點擊通過

[英]C# Winform Click through

現在,我有一個簡單的表格 window。 它用於顯示圖片。

當我hover在這張window上的圖片上,我想將它的不透明度設置為50%,同時設置鼠標穿透。 這樣就可以看到圖片后面的桌面內容,可以點擊了。

主要方法:

[DllImport("user32.dll")]
public static extern uint SetWindowLong(IntPtr h, int n, uint x);
    
void Update(){
    if (ishover)
    {
        Form.Opacity = 128;
        SetPenetrate(true);
    }
    else
    {
        Form.Opacity = 255,
        SetPenetrate(false);
    }
}

public void SetPenetrate(bool value)
{
    if (value)
    {
            SetWindowLong(this.Handle, -20, 0x20 | 0x80000);
    }
    else
    {
            this.FormBorderStyle = FormBorderStyle.None;
    }
}

代碼中,ishover時,會調用setwindowlong方法,則ishover為false,然后恢復為不可點擊。 然后再次檢測到hover,循環繼續。

目前想到的解決方案是在當前的window上加一個hook判斷鼠標是否懸停,然后做相應的處理。 這里有更簡潔的想法或 api 嗎?

使用System.Runtime.InteropServices

using System.Runtime.InteropServices;

namespace TransparentWindow
{
  public partial class Form1 : Form
  {
     #region

     //The SendMessage function sends a message to a window or windows.
     [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
     static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);

     //ReleaseCapture releases a mouse capture
     [DllImportAttribute("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]

     public static extern bool ReleaseCapture();
    
     #endregion

     public Form1()
     {
         InitializeComponent();
     }

     private void Form1_Load(object sender, EventArgs e)
     {
         this.BackColor = Color.Lime;
         this.TransparencyKey = Color.Lime;
     }

     private void Form1_MouseDown(object sender, MouseEventArgs e)
     {
         if (e.Button == MouseButtons.Left)
         {
             ReleaseCapture();
             SendMessage(this.Handle, 0xa1, 0x2, 0);
         }
     }
   }
}

你會得到一個透明的表格,你可以點擊它。

樣品 Output:

示例圖像

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM