带有透明背景的Windows窗体无法通过被点击 [英] Windows form with a transparent background that cannot be clicked through

查看:155
本文介绍了带有透明背景的Windows窗体无法通过被点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用C#/ Windows窗体。试图使窗体的背景透明,没有它失去其获得点击的能力。

Using c# / windows forms. Trying to make a form's background transparent, without it losing its ability to receive clicks.


  1. this.Opacity使得整个形式透明(不只是背景

  1. this.Opacity makes the whole form transparent (not just the background

背景色= Color.Red;

TransparencyKey =背景色;

BackColor = Color.Red;
TransparencyKey = BackColor;

使窗体的背景透明,点击通干练。我希望表单是透明的,但它应该能够获得的点击次数。

makes the background of the form transparent and click through-able. I want the form to be transparent , but it should be able to receive clicks

怎么样?

推荐答案

您需要处理WM_NCHITTEST。注意,在下面的代码片段这m.lParam包含打包X和Y坐标鼠标位置的,相对于屏幕的左上角,你需要检查你的位置透明区域相匹配。

You need to handle WM_NCHITTEST. Note in the snippet below that m.lParam contains packed X and Y coordinates of the mouse position, relative to the top left corner of the screen, and you need to check if the location matches your transparent region.

在这个例子中,我返回HTCAPTION,这意味着该地区将像窗口的标题,即用户可以通过单击并拖动这个位置拖动窗口中看到的这里可以返回什么其他价值和他们的意思

In this example I'm returning HTCAPTION, which means this region will behave like a caption of the window, i.e. user will be able to drag the window by clicking and dragging this location. See here what other values can be returned and what they mean

protected override void WndProc(ref Message m) {
    switch (m.Msg) {
    case 0x84: // this is WM_NCHITTEST
        base.WndProc(ref m);
        if ((/*m.LParam.ToInt32() >> 16 and m.LParam.ToInt32() & 0xffff fit in your transparen region*/) 
          && m.Result.ToInt32() == 1) {
            m.Result = new IntPtr(2);   // HTCAPTION
        }
        break;
    default:
        base.WndProc(ref m);
        break;
    }
}

这篇关于带有透明背景的Windows窗体无法通过被点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆