473,396 Members | 1,884 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Borderless Form drag and drop.

Hi All,

I have a borderless form that i can drag around fine when dragging
anywhere on the form by using the following code:

protected override void WndProc(ref Message m)
{

const int WM_NCHITTEST = 0x84;
const int HTCLIENT = 0x01;
const int HTCAPTION = 0x02;

if (m.Msg == WM_NCHITTEST)
{
this.DefWndProc(ref m);
if (m.Result == new IntPtr(HTCLIENT))
{
m.Result = new IntPtr(HTCAPTION);
}
else
{
base.WndProc(ref m);
}
}
else
{
base.WndProc(ref m);
}
}

What i also need to be able to do is detect when the user stops the drag
and lets go of the mouse button. MouseUp does not seem to be firing
when using this code, is that because the code above is somehow 'heading
off' the message so the Up is not getting as far as MouseUp?

Anyway, if someone has any idea on how i could capture the mouseup when
using this drag code i would be most grateful.

Cheers,

Dave
Nov 7 '06 #1
4 6598
I have a simple piece of code that doesn't involve overriding the WndProc
method.

private bool moving;
private Point loc;

private void Form1_MouseDown(object sender,
System.Windows.Forms.MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
moving = true;
loc = new Point(e.X, e.Y);
}
}

private void Form1_MouseMove(object sender,
System.Windows.Forms.MouseEventArgs e)
{
if (moving)
Location = new Point(Location.X + e.X - loc.X, Location.Y + e.Y - loc.Y);
}

private void Form1_MouseUp(object sender,
System.Windows.Forms.MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
moving = false;
}

You will have to just register these methods for their respective events.
I've used it and it works.

"David Gouge" wrote:
Hi All,

I have a borderless form that i can drag around fine when dragging
anywhere on the form by using the following code:

protected override void WndProc(ref Message m)
{

const int WM_NCHITTEST = 0x84;
const int HTCLIENT = 0x01;
const int HTCAPTION = 0x02;

if (m.Msg == WM_NCHITTEST)
{
this.DefWndProc(ref m);
if (m.Result == new IntPtr(HTCLIENT))
{
m.Result = new IntPtr(HTCAPTION);
}
else
{
base.WndProc(ref m);
}
}
else
{
base.WndProc(ref m);
}
}

What i also need to be able to do is detect when the user stops the drag
and lets go of the mouse button. MouseUp does not seem to be firing
when using this code, is that because the code above is somehow 'heading
off' the message so the Up is not getting as far as MouseUp?

Anyway, if someone has any idea on how i could capture the mouseup when
using this drag code i would be most grateful.

Cheers,

Dave
Nov 7 '06 #2
Thanks for the reply!

I have used this code (or similar) before to obtain the dragging
functionality, but in this particular instance there are problems
arising and i thought i would try the WndProc method instead.

So does anyone know how to capture the mouseUp when using the WndProc
method?

Thanks again. :D

Mr Icekirby wrote:
I have a simple piece of code that doesn't involve overriding the WndProc
method.

private bool moving;
private Point loc;

private void Form1_MouseDown(object sender,
System.Windows.Forms.MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
moving = true;
loc = new Point(e.X, e.Y);
}
}

private void Form1_MouseMove(object sender,
System.Windows.Forms.MouseEventArgs e)
{
if (moving)
Location = new Point(Location.X + e.X - loc.X, Location.Y + e.Y - loc.Y);
}

private void Form1_MouseUp(object sender,
System.Windows.Forms.MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
moving = false;
}

You will have to just register these methods for their respective events.
I've used it and it works.

"David Gouge" wrote:
>Hi All,

I have a borderless form that i can drag around fine when dragging
anywhere on the form by using the following code:

protected override void WndProc(ref Message m)
{

const int WM_NCHITTEST = 0x84;
const int HTCLIENT = 0x01;
const int HTCAPTION = 0x02;

if (m.Msg == WM_NCHITTEST)
{
this.DefWndProc(ref m);
if (m.Result == new IntPtr(HTCLIENT))
{
m.Result = new IntPtr(HTCAPTION);
}
else
{
base.WndProc(ref m);
}
}
else
{
base.WndProc(ref m);
}
}

What i also need to be able to do is detect when the user stops the drag
and lets go of the mouse button. MouseUp does not seem to be firing
when using this code, is that because the code above is somehow 'heading
off' the message so the Up is not getting as far as MouseUp?

Anyway, if someone has any idea on how i could capture the mouseup when
using this drag code i would be most grateful.

Cheers,

Dave
Nov 8 '06 #3
I don't think you're going to get an actual MouseUp message, but a simple
workaround is to check for the first MouseMove message after the MouseDown
message. This message seems to be fired immediately after the mouse is
released whether there is an actual movement or not. A simple code example
follows:

private bool AwaitingRelease = false;

protected override void WndProc(ref Message m)
{
// Pre WndProc() checks.
// We just want to catch the message.
if (m.Msg == 0xa1) // WM_NCMOUSEDOWN
{
this.AwaitingRelease = true;
Point pt = PointToClient(new Point(m.LParam.ToInt32()));
MouseEventArgs ev = new MouseEventArgs(MouseButtons.Left, 0, pt.X,
pt.Y, 0);
base.OnMouseDown(ev);
}
else if (m.Msg == 0xa0) // WM_NCMOUSEMOVE
{
if (this.AwaitingRelease )
{
this.AwaitingRelease = false;
Point pt = PointToClient(new Point(m.LParam.ToInt32()));
MouseEventArgs ev = new MouseEventArgs(MouseButtons.Left, 0,
pt.X, pt.Y, 0);
base.OnMouseUp(ev);
}
}

base.WndProc(ref m);

// Post WndProc() checks.
// We want to modify the result.
if (m.Msg == 0x84) // WM_NCHITTEST
{
if (m.Result.ToInt32() == 0x1) // HTCLIENT
m.Result = (IntPtr)0x2; // HTCAPTION
}

}
--
Mick Doherty
http://dotnetrix.co.uk/nothing.html
Nov 8 '06 #4
Mick Doherty wrote:
I don't think you're going to get an actual MouseUp message, but a simple
workaround is to check for the first MouseMove message after the MouseDown
message. This message seems to be fired immediately after the mouse is
released whether there is an actual movement or not. A simple code example
follows:

private bool AwaitingRelease = false;

protected override void WndProc(ref Message m)
{
// Pre WndProc() checks.
// We just want to catch the message.
if (m.Msg == 0xa1) // WM_NCMOUSEDOWN
{
this.AwaitingRelease = true;
Point pt = PointToClient(new Point(m.LParam.ToInt32()));
MouseEventArgs ev = new MouseEventArgs(MouseButtons.Left, 0, pt.X,
pt.Y, 0);
base.OnMouseDown(ev);
}
else if (m.Msg == 0xa0) // WM_NCMOUSEMOVE
{
if (this.AwaitingRelease )
{
this.AwaitingRelease = false;
Point pt = PointToClient(new Point(m.LParam.ToInt32()));
MouseEventArgs ev = new MouseEventArgs(MouseButtons.Left, 0,
pt.X, pt.Y, 0);
base.OnMouseUp(ev);
}
}

base.WndProc(ref m);

// Post WndProc() checks.
// We want to modify the result.
if (m.Msg == 0x84) // WM_NCHITTEST
{
if (m.Result.ToInt32() == 0x1) // HTCLIENT
m.Result = (IntPtr)0x2; // HTCAPTION
}

}

That is just the job! Thanks very much. I wont pretend to completely
understand what's going on, but if it works its good enough for me. :D

Thanks again.
Nov 8 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: MP | last post by:
Hello I want to have a public property (and Brows able) in one control, I use this code: public System.Windows.Forms.Form recordForm get { return _recordForm;} set {_recordForm = value;}
4
by: Dave | last post by:
I need to add the ability to drag from a Windows Form and drop into a non dotNet application. For example, having a generated image in my app that I wish to drag out into explorer as a friendly way...
4
by: zav | last post by:
Hi all i`m having a small problem with windows forms, i`m attempting to provide the following functionality to a form. Ability to drag and drop another form onto a form and then to dock this...
6
by: Nikki | last post by:
Hi, Can anybody help me in solving this problem. I m developing an application in which the main application form is borderless. now i m not able to move the application form with the help of...
5
by: Brian Henry | last post by:
I haven't worked much with drag/drop but I am trying to make a form that accepts files to drug onto it from explorer and droped and have the form know the full path and file name of the files...
1
by: SeanR | last post by:
Hi, I wasn't happy with the customisability of the standard Windows form, so I decided to implement my own. Basically what I did was extend a borderless form and add my own borders and caption...
3
by: Rick | last post by:
I know I seen this before but IO cannot find it. How to you drag a borderless form in Vb.net 2005
8
by: James Arnold | last post by:
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...
6
by: Alfonso2968 | last post by:
Hello, If someone can help plz. I have searched everything and all I can find is how to drag and drop within a form from panel to panel. What I need to do is Drag an image from the desktop...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.