Hi Experts;
I am creating a project and I want user can move my form inside of the screen only. How can I do this ? Please help.

Dani AI

Generated

Short answer for : avoid chasing the mouse. Intercept the window-position message and clamp the proposed rectangle to the screen working area. That keeps the form from ever being placed off-screen and is less flaky than trying to handle non-client mouse messages. was right that you can subclass and intercept messages, but it is easier and more robust to adjust the position in WM_MOVING or WM_WINDOWPOSCHANGING.

Example (VB.NET): override WndProc, read the RECT pointer for WM_MOVING, clamp it to the monitor WorkingArea, then write the RECT back.

Imports System.Runtime.InteropServices

<StructLayout(LayoutKind.Sequential)>
Private Structure RECT
  Public Left As Integer
  Public Top As Integer
  Public Right As Integer
  Public Bottom As Integer
End Structure

Protected Overrides Sub WndProc(ByRef m As Message)
  Const WM_MOVING As Integer = &H216
  If m.Msg = WM_MOVING Then
    Dim r As RECT = Marshal.PtrToStructure(Of RECT)(m.LParam)
    Dim wa As Rectangle = Screen.FromHandle(Me.Handle).WorkingArea

    Dim dx As Integer = 0
    Dim dy As Integer = 0
    If r.Left < wa.Left Then dx = wa.Left - r.Left
    If r.Right > wa.Right Then dx = wa.Right - r.Right
    If r.Top < wa.Top Then dy = wa.Top - r.Top
    If r.Bottom > wa.Bottom Then dy = wa.Bottom - r.Bottom

    If dx <> 0 OrElse dy <> 0 Then
      r.Left += dx : r.Right += dx
      r.Top += dy : r.Bottom += dy
      Marshal.StructureToPtr(r, m.LParam, True)
    End If
  End If
  MyBase.WndProc(m)
End Sub

If you want a quick but coarser solution, clamp in the Form.Move event (simpler, but the form will jump back during dragging). For borderless/custom-drag forms handle the mouse drag and clamp the Location while dragging.

Notes and caveats: handle multi-monitor setups by using Screen.FromHandle or Screen.FromRectangle so you clamp against the right monitor. Use WorkingArea (excludes the taskbar) rather than Bounds if you want the form to stay out of the taskbar. Watch DPI and OS features (Aero Snap, auto-hide taskbar) when testing. If you’re on classic VB6, use a well-tested subclass helper (avoid raw, unsafe subclass code in the IDE).

Recommended Answers

All 2 Replies

It can be done, but it isn't pretty. First, you have to intercept the mouse event message WM_NCLBUTTONUP from the non-client area (e.g. the title bar) then check the position of the window being dragged and re-set the top and left properties. You have to create your own message loop to intercept the message because the default event procedures given by VB for a form don't include non-client area events.

If you don't know what I'm talking about, or don't know how to intercept messages, then you have quite a bit of reading to do. Here's a link to a VERY short introduction of how to do this: Read it, and if it still doesn't make sense then you should think again about whether you want to implement the functionality.

Good luck!

Apparently there is a problem trying to use the WM_NCLBUTTONUP message... there is a workaround if you decide to do this. Look at this URL, near the bottom under "Community Content": http://msdn.microsoft.com/en-us/library/ms645621(v=vs.85).aspx

Again, good luck!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.