如何防止控件更改 Z 顺序?

ada*_*ams 3 c# windows messaging winapi

我在 .Net 中有用户控制权,我在 WndProc 中使用命中测试来允许在运行时用鼠标调整它的大小。

问题是在命中测试成功(鼠标按下、拖动以调整大小、鼠标释放)之后,控件在 Z 顺序中向上跳跃并破坏了它在表单中的位置。

我需要命中测试,因为它是一个非常定制的控件。

WndProc 有没有办法阻止控件改变它的 Z 顺序?

谢谢。

命中测试代码:

protected override void WndProc(ref Message m) {
  if (!DesignMode && Sizeable && (m.Msg == Win32Wrapper.WM_NCHITTEST)) {
    Point Hit = new Point((int)m.LParam & 0xFFFF, (int)m.LParam >> 16);
    Hit = this.PointToClient(Hit);
    int DistToBorder = 5;
    if (Hit.X < DistToBorder) {
      if (Hit.Y < DistToBorder) {
        m.Result = (IntPtr)Win32Wrapper.HTTOPLEFT;
        return;
      }
      if (Hit.Y > this.ClientRectangle.Bottom - DistToBorder) {
        m.Result = (IntPtr)Win32Wrapper.HTBOTTOMLEFT;
        return;
      }
      m.Result = (IntPtr)Win32Wrapper.HTLEFT;
      return;
    }
    else if (Hit.X > ClientRectangle.Right - DistToBorder) {
      if (Hit.Y < DistToBorder) {
        m.Result = (IntPtr)Win32Wrapper.HTTOPRIGHT;
        return;
      }
      else if (Hit.Y > this.ClientRectangle.Bottom - DistToBorder) {
        m.Result = (IntPtr)Win32Wrapper.HTBOTTOMRIGHT;
        return;
      }
      m.Result = (IntPtr)Win32Wrapper.HTRIGHT;
      return;
    }
    else if (Hit.Y < DistToBorder) {
      m.Result = (IntPtr)Win32Wrapper.HTTOP;
      return;
    }
    else if (Hit.Y > this.ClientRectangle.Bottom - DistToBorder) {
      m.Result = (IntPtr)Win32Wrapper.HTBOTTOM;
      return;
    }
  }
Run Code Online (Sandbox Code Playgroud)

Kir*_*sky 6

为了防止 Z 顺序更改,您应该捕获WM_WINDOWPOSCHANGING消息并设置SWP_NOZORDER标志。