简体   繁体   中英

How to prevent a control from changing Z order?

I have user control in .Net where I use a hit test in WndProc to allow resizing it in runtime with the mouse.

The problem is that after the hit test succedes (mouse press, drag to resize, mouse release) the control jumps upwards in the Z order and ruins it position in the form.

I need the hit test since it's a very customized control.

Is there a way in WndProc to stop the control from changing it's Z order ?

Thanks.

The hit test code:

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;
    }
  }

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

Are you sure it's the hit test that's causing the problem? How are you resizing the control? One option is to call SetWindowPos using p-invoke passing it SWP_NOZORDER flag.

没有答案,但是您是否尝试过使用ControlDesigner,而不是滚动自己的Deigner模式交互作用?

Window dialogs manage tab order and focus through the controls windows' z-order, the control that is given focus is raised to the top.

If you want your custom control to retain its relative z-positioning, then ensure that its properties do not indicate its a TABSTOP or otherwise capable of receiving focus. ie will it work if disabled?

The flip side of this is, even if you successfullly stop your control's z-order from changing, its implicitly going to be re-positioned as the user interacts with other controls.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM