Borderless Form Right Click

J

James Arnold

I currently have a borderless form, which I am subclassing to allow
dragging & dropping:

Protected Overrides Sub WndProc(ByRef m As Message)
Select Case m.Msg
Case 132 'Click & Drag Form
m.Result = New IntPtr(2)
'Continue message
MyBase.WndProc(m)
End Select
End Sub

This works fine, but it also removes any processing of MouseEvents on
the form. A little background; Msg 132 is WM_NCHITTEST, which works
out which area of the window is being clicked. By settings the result
to 2 you are telling it you are clicking a caption, so it allows the
drag & drop. However, it overrides too much, making the following code
no longer executed:

Private Sub frmMain_MouseClick(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles Me.MouseClick
MsgBox("Clicky!")
End Sub

I have tried without using subclassing, using the MouseDown and
MouseMove events and manually calculating the offset. This worked, but
I also am using subclassing to snap the form to the screen edges and
dragging will not work once the window is snapped.

My question is, how can I allow dragging & dropping of the borderless
form, without overriding the click events (to pop up a context menu
etc)? Thanks (again!)

James
 
C

Charlie Brown

I currently have a borderless form, which I am subclassing to allow
dragging & dropping:

Protected Overrides Sub WndProc(ByRef m As Message)
Select Case m.Msg
Case 132 'Click & Drag Form
m.Result = New IntPtr(2)
'Continue message
MyBase.WndProc(m)
End Select
End Sub

This works fine, but it also removes any processing of MouseEvents on
the form. A little background; Msg 132 is WM_NCHITTEST, which works
out which area of the window is being clicked. By settings the result
to 2 you are telling it you are clicking a caption, so it allows the
drag & drop. However, it overrides too much, making the following code
no longer executed:

Private Sub frmMain_MouseClick(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles Me.MouseClick
MsgBox("Clicky!")
End Sub

I have tried without using subclassing, using the MouseDown and
MouseMove events and manually calculating the offset. This worked, but
I also am using subclassing to snap the form to the screen edges and
dragging will not work once the window is snapped.

My question is, how can I allow dragging & dropping of the borderless
form, without overriding the click events (to pop up a context menu
etc)? Thanks (again!)

James

Have you tried adding Case Else to your select?

Protected Overrides Sub WndProc(ByRef m As Message)
Select Case m.Msg
Case 132 'Click & Drag Form
m.Result = New IntPtr(2)
MyBase.WndProc(m)
Case Else
MyBase.WndProc(m)
End Select
End Sub
 
H

Herfried K. Wagner [MVP]

James Arnold said:
I currently have a borderless form, which I am subclassing to allow
dragging & dropping:

Protected Overrides Sub WndProc(ByRef m As Message)
Select Case m.Msg
Case 132 'Click & Drag Form
m.Result = New IntPtr(2)
'Continue message
MyBase.WndProc(m)
End Select
End Sub

This works fine, but it also removes any processing of MouseEvents on
the form.

'MyBase.WndProc(m)' in the code above will only be called for a certain
message although it should be called for the other messages too. Place the
line outside the 'Select Case' block.
 
J

James Arnold

'MyBase.WndProc(m)' in the code above will only be called for a certain
message although it should be called for the other messages too. Place the
line outside the 'Select Case' block.

I already have one outside of the Select Case, I posted a simplified
version as I have a number of things it is looking for (hotkeys,
window movement etc). Sorry - I should have should have said that in
the original post.

I believe the problem is that the WM_NCHITTEST is telling all messages
the mouse was on the caption & not the form, so the other messages are
never raised (regardless of the presence of MyBase.WndProc(m)).
 
N

Newbie Coder

Careful James,

Herfried will be doing a Google search soon which will turn up something
that is totally useless - The MVP approach

When I get the chance (time willing) I will have a proper look at the code
for you
 
H

Herfried K. Wagner [MVP]

Newbie Coder said:
Herfried will be doing a Google search soon which will turn up something
that is totally useless - The MVP approach

Huh?! I am at least not writing such useless replies as you do.
 
J

James Arnold

Huh?! I am at least not writing such useless replies as you do.

Thank you Newbie Coder for any time you can spend looking at it, and
thanks Herfried for the earlier reply.

I do not wish to start an argument. I just appreciate any help I can
get!
 
J

James Arnold

Strange how I post 2 replies & they don't get added to this post

I have found a solution. I think that this is very similar to the
original code I posted, but instead of redirecting any messages to the
caption it copies them. Thanks for the replies.

Private Declare Function SendMessage Lib "user32" Alias
"SendMessageA" (ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal
wParam As Integer, ByVal lParam As Integer) As Integer
Private Declare Function ReleaseCapture Lib "user32" Alias
"ReleaseCapture" () As Integer

Private Const HTCAPTION As Integer = &H2
Private Const WM_NCLBUTTONDOWN As Integer = &HA1

Private Sub frmMain_MouseDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown

ReleaseCapture()
SendMessage(Me.Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0)

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top