Results 1 to 3 of 3

Thread: VB.Net - Moving a borderless/captionless form

  1. #1

    Thread Starter
    Frenzied Member MerrionComputin's Avatar
    Join Date
    Apr 2001
    Location
    Dublin, Ireland
    Posts
    1,616

    VB.Net - Moving a borderless/captionless form

    Often, especially where a customised or skinned look is desired, the form is created with the FormBorderStyle set to FixedSingle.
    However if you still want to move that skinned form you have a bit of a problem. Fortunately it is possible to handle the WM_NCHITTEST window message to make windows think the mouse is over the caption even where a form doesn't have one.

    1. Defining a rectangle that emulates the form caption
    In the form:
    VB Code:
    1. Public Class Form1
    2.     Inherits System.Windows.Forms.Form
    3.  
    4. #Region "Private memeber variables"
    5.     Dim rcCaption As Rectangle
    6. #End Region
    7.  
    8.     Public Sub New()
    9.         MyBase.New()
    10.  
    11.         'This call is required by the Windows Form Designer.
    12.         InitializeComponent()
    13.  
    14.         '\\ Make the fake caption rectangle
    15.         rcCaption = New Rectangle(0, 0, Me.Width, 29)
    16.  
    17.     End Sub
    18.  
    19.     Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
    20.  
    21.         e.Graphics.FillRectangle(New SolidBrush(Color.Blue), rcCaption)
    22.  
    23.     End Sub
    24.  
    25.     Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
    26.         '\\ We want to pretend that the rectangle rcCaption, is the caption bar so we can drag the form by it...
    27.         If m.Msg = WM_NCHITTEST Then
    28.             Call FakeCaptionForCaptionlessWindow(Me, m, rcCaption)
    29.         Else
    30.             MyBase.WndProc(m)
    31.         End If
    32.     End Sub
    33.  
    34. End Class

    2. The WM_NCHITTEST handler
    This is kept in a seperate module for reusability...
    VB Code:
    1. Imports System.Windows.Forms
    2.  
    3. '\\ --[ApiHitTestHandling]-------------------------------------
    4. '\\ Used to handle the WM_HITTEST message, to allow dragging of
    5. '\\ captionless windows...
    6. '\\ -----------------------------------------------------------
    7. Module ApiHitTestHandling
    8.  
    9.     Public Const WM_NCHITTEST = &H84
    10.  
    11.     Public Enum HitTestResult
    12.         HTBORDER = 18
    13.         HTBOTTOM = 15
    14.         HTBOTTOMLEFT = 16
    15.         HTBOTTOMRIGHT = 17
    16.         HTCAPTION = 2
    17.         HTCLIENT = 1
    18.         HTERROR = (-2)
    19.         HTGROWBOX = 4
    20.         HTHSCROLL = 6
    21.         HTLEFT = 10
    22.         HTMAXBUTTON = 9
    23.         HTMENU = 5
    24.         HTMINBUTTON = 8
    25.         HTNOWHERE = 0
    26.         HTRIGHT = 11
    27.         HTSYSMENU = 3
    28.         HTTOP = 12
    29.         HTTOPLEFT = 13
    30.         HTTOPRIGHT = 14
    31.         HTVSCROLL = 7
    32.         HTTRANSPARENT = (-1)
    33.         HTOBJECT = 19
    34.         HTCLOSE = 20
    35.         HTHELP = 21
    36.     End Enum
    37.  
    38.     Public Sub FakeCaptionForCaptionlessWindow(ByVal fParent As Form, ByRef m As Message, ByVal CaptionRectangle As Rectangle)
    39.  
    40.         If m.Msg = WM_NCHITTEST Then
    41.             '\\ If the mouse is in the rectangle that is considered to be the caption set the return value to HTCAPTION
    42.             Dim ptClickLocation As New Point(m.LParam.ToInt32)
    43.             ptClickLocation = fParent.PointToClient(ptClickLocation)
    44.  
    45.             If CaptionRectangle.Contains(ptClickLocation) Then
    46.                 m.Result = New IntPtr(HitTestResult.HTCAPTION)
    47.             Else
    48.                 m.Result = New IntPtr(HitTestResult.HTCLIENT)
    49.             End If
    50.         End If
    51.     End Sub
    52.  
    53. End Module
    ----8<---------------------------------------
    NEW - The .NET printer queue monitor component
    ----8<---------------------------------------
    Now with Examples of use

  2. #2
    Addicted Member Latin4567's Avatar
    Join Date
    Jan 2005
    Posts
    202

    Re: VB.Net - Moving a borderless/captionless form

    you could just do this....

    VB Code:
    1. Dim oldloc As New Point(0,0)
    2. Dim oldcur As New point(0,0)
    3. Dim timer1 As New Timer
    4.  
    5. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    6.        timer1.Interval = 2 'every 2 millaseconds
    7.        timer1.Enabled = True
    8.        UpdatePositions()
    9.     End Sub
    10.  
    11. Private Sub timer1_tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles timer.Tick
    12.         Me.Location = oldloc - oldcur + System.Windows.Forms.Cursor.Position
    13.         UpdatePositions()
    14. End Sub
    15.  
    16. Private Sub UpdatePositions()
    17.          oldloc = Me.Location
    18.          oldcur = System.Windows.Forms.Cursor.Position
    19. End Sub
    20.  
    21. Private Sub Form1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
    22.          timer1.Start()
    23. End Sub
    24.  
    25. Private Sub Form1_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseUp
    26.          timer1.Stop()
    27. End Sub

    I have found that there are no performance differences between using this code, and using the API's.... and with this code you can manually changing the per-millasecond update speed by change timer1's interval
    Last edited by Latin4567; Feb 18th, 2006 at 03:55 PM.

  3. #3
    Addicted Member Latin4567's Avatar
    Join Date
    Jan 2005
    Posts
    202

    Re: VB.Net - Moving a borderless/captionless form

    hmm.. maybe I should submit this code as a seperate codebank submission...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width