views:

308

answers:

4

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

No an answer, but have you tried to go with he ControlDesigner instead of rolling your own deigner mode interaction?

danbystrom
A: 

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. i.e. 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.

Chris Becke
A: 

To prevent Z-order change you should catch WM_WINDOWPOSCHANGING message and set SWP_NOZORDER flag.

Kirill V. Lyadvinsky
A: 

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.