Click here to Skip to main content
15,885,875 members
Articles / Programming Languages / Visual Basic

Moving a Window

Rate me:
Please Sign up or sign in to vote.
1.52/5 (8 votes)
10 Jul 2006CPOL 26.6K   128   10  
Simple code for moving a window using Win32 API in .NET.
' Author  : Kiran Kumar B
' Company : Nanna Computers
' Email   : bk_kiran_kumar@yahoo.co.uk
'           bk_kiran_kumar@rediffmail.com
'           kirankumar@nannacomputers.com
' Mobile  : +91 98854 43215

Imports System.Runtime.InteropServices

Public Class frmMain

    ' All the below functions and constants are related to Win32 API

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

    <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

    Private Sub frmMain_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
        ' Using the below code you can just drag the form to any place
        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

        ' If you want to make the form without a titlebar and 
    End Sub

    Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub btnTitleForm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTitleForm.Click
        Call frmTitle.ShowDialog()
    End Sub
End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Architect
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions