By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,887 Members | 1,059 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,887 IT Pros & Developers. It's quick & easy.

Borderless Form Right Click

P: n/a
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

Mar 21 '07 #1
Share this Question
Share on Google+
8 Replies


P: n/a
On Mar 21, 1:35 pm, "James Arnold" <jar...@gmail.comwrote:
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

Mar 21 '07 #2

P: n/a
"James Arnold" <ja****@gmail.comschrieb:
>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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Mar 21 '07 #3

P: n/a
On 21 Mar, 22:03, "Herfried K. Wagner [MVP]" <hirf-spam-me-
h...@gmx.atwrote:
"James Arnold" <jar...@gmail.comschrieb:
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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
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)).

Mar 22 '07 #4

P: n/a
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

--
Newbie Coder
(It's just a name)
Mar 23 '07 #5

P: n/a
"Newbie Coder" <ne*********@spammeplease.comschrieb:
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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Mar 23 '07 #6

P: n/a
On 23 Mar, 13:52, "Herfried K. Wagner [MVP]" <hirf-spam-me-
h...@gmx.atwrote:
"Newbie Coder" <newbieco...@spammeplease.comschrieb:
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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
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!

Mar 23 '07 #7

P: n/a
Strange how I post 2 replies & they don't get added to this post

--
Newbie Coder
(Do a Goolge Groups search Herfried)
Mar 24 '07 #8

P: n/a
On 24 Mar, 03:10, "Newbie Coder" <newbieco...@spammeplease.comwrote:
Strange how I post 2 replies & they don't get added to this post

--
Newbie Coder
(Do a Goolge Groups search Herfried)
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

Mar 25 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.