Click here to Skip to main content
13,661,044 members
Click here to Skip to main content
Add your own
alternative version


10 bookmarked
Posted 10 Jul 2006
Licenced CPOL

Moving a Window

, 10 Jul 2006
Rate this:
Please Sign up or sign in to vote.
Simple code for moving a window using Win32 API in .NET.


Design your own forms with movement enabled. You can even add functionality for maximizing, minimizing, and restoring a form. The code below gives you a brief explanation, and you can just copy paste it in your form code.

You have to import System.Runtime.InteropServices in order to declare Win32 API functions:

Imports System.Runtime.InteropServices

In the beginning of the form code, declare all the constants and APIs:

Public Const GWL_STYLE = (-16)
Public Const WS_DLGFRAME = &H400000
Public Const HTCAPTION = 2

Public Const SW_HIDE = 0
Public Const SW_MAXIMIZE = 3
Public Const SW_MINIMIZE = 6
Public Const SW_RESTORE = 9

<DllImport("User32.dll")> _
Public Shared Function ShowWindow(ByVal hWnd As IntPtr, _
              ByVal nCmdShow As Integer) As Integer
End Function

<DllImport("User32.dll")> _
Public Shared Function ReleaseCapture() As Integer
End Function

<DllImport("User32.dll")> _
Public Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal wMsg As Integer, _
              ByVal wParam As Integer, ByVal lParam As Integer) As Integer
End Function

Paste this code in the mouse-down event of the form:

If e.Button = Windows.Forms.MouseButtons.Left Then
    Me.Cursor = Cursors.SizeAll
    Call ReleaseCapture()
    Call SendMessage(Me.Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0&)
    Me.Cursor = Cursors.Arrow
End If

Download the source code to view the entire code.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Kiran Kumar B
India India
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralMy vote of 1 Pin
babakzawari24-Sep-10 3:50
memberbabakzawari24-Sep-10 3:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01-2016 | 2.8.180810.1 | Last Updated 11 Jul 2006
Article Copyright 2006 by Kiran Kumar B
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid