Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


Show Recent Changes
Subscribe (RSS)
Misc. Pages
Comments
FAQ
Helpful Tools
Playground
Suggested Reading
Website TODO List
Download Visual Studio Add-In

ReleaseCapture (user32)
 
.
Summary

C# Signature:

[DllImport("user32.dll")]
static extern bool ReleaseCapture();

VB.NET Signature:

<DllImport("user32.dll")> _
Private Shared Function ReleaseCapture() As Boolean
End Function

User-Defined Types:

None.

Notes:

None.

ReleaseCapture will delegate mousedown events to underlying controls.

Tips & Tricks:

Create a form without a title bar and can be moved by mouse.

Create a window without a title bar and can be moved by mouse:

   'const and dll functions for moving form
    Public Const WM_NCLBUTTONDOWN As Integer = &HA1
    Public Const HT_CAPTION As Integer = &H2

    <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
    Private Shared Function SendMessage(ByVal hWnd As IntPtr,
            ByVal Msg As UInteger,
            ByVal wParam As IntPtr,
            ByVal lParam As IntPtr) As IntPtr
    End Function

    <DllImportAttribute("user32.dll")> _
    Public Shared Function ReleaseCapture() As Boolean
    End Function

    Private Sub Form1_MouseDown(sender As Object, e As MouseEventArgs) Handles Me.MouseDown
    If e.Button = MouseButtons.Left Then
        ReleaseCapture()
        SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0)
    End If
    End Sub

Sample Code:

You can use the ReleaseCapture() function to provide drag functionality. for example the following code will allow the user to drag a control around a form (you'll have to assign this code to the mousedown event):

[DllImport("User32.dll")]
private static extern int SendMessage(IntPtr hWnd, int msg , int wParam , ref int lParam);
[DllImport("user32")]
public static extern bool ReleaseCapture(IntPtr hwnd);
public static extern int ReleaseCapture(IntPtr hwnd);

private const int WM_SYSCOMMAND = 0x112;
private const int MOUSE_MOVE = 0xF012;

private void control_MouseDown(object sender, MouseEventArgs e)
{
   Control ctrl = sender as Control;
   ReleaseCapture(ctrl.Handle);
   int nul =0;
   SendMessage(ctrl.Handle, WM_SYSCOMMAND, MOUSE_MOVE, ref nul);
  }

Alternative Managed API:

Capture property on System.Windows.Forms.Control

Do you know one? Please contribute it!

Documentation

Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

 
Access PInvoke.net directly from VS:
Terms of Use
Edit This Page
Find References
Show Printable Version
Revisions