Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 108417
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T01:54:30+00:00 2026-05-11T01:54:30+00:00

I am trying to make a form move (using the titlebar) from a button

  • 0

I am trying to make a form move (using the titlebar) from a button click.

I thought this would be simple using SendMessage:

Const WM_LBUTTONDOWN As Integer = &H201  Button1.Capture = False Cursor.Position = Me.Location + New Size(50, 8)  SendMessage(Me.Handle, WM_LBUTTONDOWN, CType(1, IntPtr), IntPtr.Zero) 

However, although this sends the message if the cursor is in the forms client area, it does not seem to send it to the forms titlebar (the form captures the event somehow, despite the cursor being on the titlebar not in the client area).

I have tried the above code in both mousedown and click events on the button, moving the cursor and then pressing on the button1.

Any suggestions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T01:54:31+00:00Added an answer on May 11, 2026 at 1:54 am

    You would need WM_NCLBUTTONDOWN (and pass HTCAPTION as wParam). I’m still not entirely sure this would accomplish what you’re trying to do, though.

    Typically, the way to allow the user to move your form when clicking somewhere other than the title bar is to process the WM_NCHITTEST message and return HTCAPTION when the cursor is over the area from which you’d like to initiate moving. However, if this area is occupied by a child control, you also have to process WM_NCHITTEST from the child control and return HTTRANSPARENT.


    Incidentally, an easier—if slightly less correct—way to accomplish this is to do as Mehrdad Afshari suggested, and just set the form’s Location property. You commented to him that ‘it needs to move on the mouse move’, and that’s exactly what you can and should do.

    class MyForm : Form{     Point downAt;      MyForm(){         Label lbl      = new Label();         lbl.AutoSize   = true;         lbl.BackColor  = Color.Blue;         lbl.ForeColor  = Color.White;         lbl.Location   = new Point(50, 50);         lbl.Text       = 'Drag me to move this form.';         lbl.Parent     = this;         lbl.MouseDown += (s, e)=>downAt = e.Location;         lbl.MouseMove += (s, e)=>{if(lbl.Capture) Location += (Size)e.Location - (Size)downAt;};     } } 

    The problem with this approach is that it bypasses Windows’ code for moving a top-level window. This means that if the user has not selected the ‘Show window contents while dragging’ option in the Display Properties dialog, this will effectively ignore that setting (it won’t show a drag outline). There may be other drawbacks that I haven’t thought of as well.

    On the whole, though, this is a simple, easy way to accomplish this that is a fully .NET solution which doesn’t rely on any platform invoke (so it should be portable to Mono on Unix).


    Oops. I just realized that I gave you C# example code, but your code seems to be VB.NET. I guess what you would need would be:

    Sub New()     Dim lbl As New Label     lbl.AutoSize  = True     lbl.BackColor = Color.Blue     lbl.ForeColor = Color.White     lbl.Location  = New Point(50, 50)     lbl.Text      = 'Drag me to move this form.'     lbl.Parent    = Me     AddHandler lbl.MouseDown, Function(ByVal s As Object, ByVal e As MouseEventArgs)         Me.downAt = e.Location     End Function     AddHandler lbl.MouseMove, Function(ByVal s As Object, ByVal e As MouseEventArgs)         If lbl.Capture Then             Me.Location = Me.Location + DirectCast(e.Location, Size) - DirectCast(Me.downAt, Size)         End If     End Function End Sub 

    This may not be the most succinct way to express this in VB.NET. I used Reflector to help me translate it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 197k
  • Answers 197k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer OK, there's a couple of things Firstly, your set statement… May 12, 2026 at 7:17 pm
  • Editorial Team
    Editorial Team added an answer The usual answer is that string concatenation is more efficient… May 12, 2026 at 7:17 pm
  • Editorial Team
    Editorial Team added an answer The SqlDataSource1 object has not been initialized. May 12, 2026 at 7:17 pm

Related Questions

i am trying to make a c# WPF form where i can drag it
Many of the web pages I am working on use files such as images
In a Windows Form window, multiple events can trigger an asynchronous method. This method
The requirement: On an error (thrown exception), the file being processed should be moved

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.