使用TransparentKey和调整表单大小的错误

时间:2013-12-15 15:28:30

标签: c# visual-studio-2012 .net-4.0 transparent

重现此错误的步骤:

  • Visual Studio 2012,.net 4.0,Winforms;
  • 将背景和TransparenceKey设置为LimeGreen;
  • 将TopMost设为True;

运行应用程序,开始将表单调整为较小的尺寸,注意低于200像素,您将无法再单击表单框架(标题框和边)。

:(

enter image description here

颜色

仅在“点击”颜色中出现,例如:

            Black
            DarkGray
            DarkGreen
            DarkMagenta
            DimGray
            ForestGreen
            Fuchsia
            Gainsboro
            Gray
            Green
            Honeydew
            LightGray
            LightGreen
            Lime
            LimeGreen
            Magenta
            PaleGreen
            Plum
            Purple
            Silver
            Thistle
            Violet
            White
            WhiteSmoke

可能的出路

处理Windows消息可能是最后的手段......但为什么会这样呢?

    private const int WM_NCHITTEST = 0x84;
    private const int cGrip = 20;
    private const int cCaption = 35;
    private const int cBorder = 7;

    protected override void WndProc(ref Message m)
    {
        if (stage == 0 || stage == 4)
        {
            if (m.Msg == (int)WM_NCHITTEST)
            {
                #region Hit Test
                // Trap WM_NCHITTEST
                Point pos = new Point(m.LParam.ToInt32() & 0xffff, m.LParam.ToInt32() >> 16);
                pos = this.PointToClient(pos);

                //Bottom Left
                if (pos.X <= cGrip && pos.Y >= this.ClientSize.Height - cGrip)
                {
                    m.Result = (IntPtr)HitTest.HTBOTTOMLEFT;
                    return;
                }

                //Bottom Right
                else if (pos.X >= this.ClientSize.Width - cGrip &&
                    pos.Y >= this.ClientSize.Height - cGrip)
                {
                    m.Result = (IntPtr)HitTest.HTBOTTOMRIGHT;
                    return;
                }

                //Top Left
                else if (pos.Y <= cBorder && pos.X <= cBorder)
                {
                    m.Result = (IntPtr)HitTest.HTTOPLEFT;
                    return;
                }

                //Top Right
                else if (pos.Y <= cBorder && pos.X >= this.ClientSize.Width - cBorder)
                {
                    m.Result = (IntPtr)HitTest.HTTOPRIGHT;
                    return;
                }

                //Top
                else if (pos.Y <= cBorder)
                {
                    m.Result = (IntPtr)HitTest.HTTOP;
                    return;
                }

                //Caption
                else if (pos.Y < cCaption && pos.X > 50 && pos.Y < ClientSize.Width)
                {
                    m.Result = (IntPtr)HitTest.HTCAPTION;
                    return;
                }

                //Bottom
                else if (pos.Y >= this.ClientSize.Height - cBorder)
                {
                    m.Result = (IntPtr)HitTest.HTBOTTOM;
                    return;
                }

                //Left
                else if (pos.X <= cBorder)
                {
                    m.Result = (IntPtr)HitTest.HTLEFT;
                    return;
                }

                //Right
                else if (pos.X >= this.ClientSize.Width - cBorder)
                {
                    m.Result = (IntPtr)HitTest.HTRIGHT;
                    return;
                }
                #endregion
            }
        }

        base.WndProc(ref m);
    }

1 个答案:

答案 0 :(得分:0)

快速出路:

一个月之后,我终于发现了一些可以工作的东西(但目前还不错,因为我的程序需要尽可能少的闪烁)

只需制作ResizeBegin和ResizeEnd,然后更改透明度键的颜色和表单的背景颜色。

    private void Legacy_ResizeBegin(object sender, EventArgs e)
    {
        //By changing the this properties, the form, somehow, don't loose the hittest..
        panelTransparent.BackColor = Color.WhiteSmoke;
        this.TransparencyKey = Color.WhiteSmoke;
    }

    private void Legacy_ResizeEnd(object sender, EventArgs e)
    {
        panelTransparent.BackColor = Color.LimeGreen;
        this.TransparencyKey = Color.LimeGreen;
    }