c# 关于鼠标穿透失效的问题请教 5C

public partial class Form2 : Form
{
public double bs { get; internal set; }

    public Form2()
    {
        InitializeComponent();



    }
    public void settime(float n)
    {

    }
    public void quit()
    {
        Application.Exit();
    }

    Magnifier m = null;
    public void resize(float size)
    {
        m.magnification = size;
    }
    public void rerender()
    {


        FormBorderStyle = FormBorderStyle.None;
        //圆形
        GraphicsPath g = new GraphicsPath();
        g.AddArc(this.DisplayRectangle, 0, 360);
        this.Region = new Region(g);

        StartPosition = FormStartPosition.Manual;

        int xWidth = SystemInformation.PrimaryMonitorSize.Width;//获取显示器屏幕宽度
        int yHeight = SystemInformation.PrimaryMonitorSize.Height;//高度
        Location = new Point((xWidth / 2) - this.Width / 2, (yHeight / 2) - this.Height / 2);
        //传的3个参数 窗体,放大倍数,刷新频率
        float x = (float)this.bs;
        //MessageBox.Show(x.ToString());

        m = new Magnifier(this, x, NativeMethods.USER_TIMER_MINIMUM);

    }
    private void Form2_Load(object sender, EventArgs e)
    {
        this.ShowInTaskbar = false;//窗体不出现在Windows任务栏中
        CanPenetrate();
        this.TopMost = true;//使窗体始终在其它窗体之上

        rerender();


    }
    private const uint WS_EX_LAYERED = 0x80000;
    private const int WS_EX_TRANSPARENT = 0x20;
    private const int GWL_EXSTYLE = (-20);
    private string Var_genre = "";//记录当前操作的类型
    #region 在窗口结构中为指定的窗口设置信息
    /// <summary>
    /// 在窗口结构中为指定的窗口设置信息
    /// </summary>
    /// <param name="hwnd">欲为其取得信息的窗口的句柄</param>
    /// <param name="nIndex">欲取回的信息</param>
    /// <param name="dwNewLong">由nIndex指定的窗口信息的新值</param>
    /// <returns></returns>
    [DllImport("user32", EntryPoint = "SetWindowLong")]
    private static extern uint SetWindowLong(IntPtr hwnd, int nIndex, uint dwNewLong);
    #endregion

    #region 从指定窗口的结构中取得信息
    /// <summary>
    /// 从指定窗口的结构中取得信息
    /// </summary>
    /// <param name="hwnd">欲为其获取信息的窗口的句柄</param>
    /// <param name="nIndex">欲取回的信息</param>
    /// <returns></returns>
    [DllImport("user32", EntryPoint = "GetWindowLong")]
    private static extern uint GetWindowLong(IntPtr hwnd, int nIndex);
    #endregion
    #region 使窗口有鼠标穿透功能
    /// <summary>
    /// 使窗口有鼠标穿透功能
    /// </summary>
    private void CanPenetrate()
    {
        uint intExTemp = GetWindowLong(this.Handle, GWL_EXSTYLE);
        uint oldGWLEx = SetWindowLong(this.Handle, GWL_EXSTYLE, WS_EX_TRANSPARENT | WS_EX_LAYERED);
    }
    #endregion



    #region 设置颜色和透明度的状态
    /// <summary>
    /// 设置颜色和透明度的状态
    /// </summary>
    private void SetEstate(Form Frm, object sender)
    {
        Var_genre = ((ToolStripMenuItem)sender).Name;
        string Tem_Str = Var_genre;
        if (Var_genre.IndexOf('_') >= 0)
        {
            Var_genre = Tem_Str.Substring(0, Tem_Str.IndexOf('_'));
        }

        switch (Var_genre)
        {
            case "ToolColor":
                {
                    Color Tem_Color = Color.Gainsboro;
                    switch (Convert.ToInt32(((ToolStripMenuItem)sender).Tag.ToString()))
                    {
                        case 1: Tem_Color = Color.Gainsboro; break;
                        case 2: Tem_Color = Color.DarkOrchid; break;
                        case 3: Tem_Color = Color.RoyalBlue; break;
                        case 4: Tem_Color = Color.Gold; break;
                        case 5: Tem_Color = Color.LightGreen; break;
                    }
                    Frm.BackColor = Tem_Color;
                    break;
                }
            case "ToolClarity":
                {
                    double Tem_Double = 0.0;
                    switch (Convert.ToInt32(((ToolStripMenuItem)sender).Tag.ToString()))
                    {
                        case 1: Tem_Double = 0.1; break;
                        case 2: Tem_Double = 0.2; break;
                        case 3: Tem_Double = 0.3; break;
                        case 4: Tem_Double = 0.4; break;
                        case 5: Tem_Double = 0.5; break;
                        case 6: Tem_Double = 0.6; break;
                        case 7: Tem_Double = 0.7; break;
                        case 8: Tem_Double = 0.8; break;
                        case 9: Tem_Double = 0.9; break;

                    }
                    Frm.Opacity = Tem_Double;
                    break;
                }
            case "ToolAcquiescence":
                {
                    Frm.BackColor = Color.Gainsboro;
                    Frm.Opacity = 0.6;
                    break;
                }
            case "ToolClose":
                {
                    Close();
                    break;
                }

        }
    }


    #endregion

自己做了一个放大镜功能,想做鼠标穿透但是一直失效想看看怎么回事。

2个回答

我自己建了一个可穿透的winform试了一下,是可以的。
主要就是这三个属性,不知道是不是你想要的,你也试试吧

            this.BackColor = Color.White;
            this.TransparencyKey = Color.White;
            this.TopMost = true;

是昨天我给你的代码么?你有改动么?我测试过可以啊。

hcli168
waesaeasea 代码可以,但是加到我的放大镜上面就不行了
2 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问