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

How to move a borderless windows form in c#.NET

P: n/a
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 mouse.
Thanks,

Nov 17 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Hi Nikki,

In Your case you've got to handle MouseMove events of the
main form. Check if left-button is pressed and the change
the form location according to previous point (e.X, e.Y) of
handled event.

HTH
Marcin
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 mouse.
Thanks,

Nov 17 '05 #2

P: n/a
Hi,

In addition to what Marcin has said, another solution is to handle the
WM_NCHITTEST message and return HTCAPTION if the form must be dragged. If
you add the following code to your Form, the form will be dragged when you
click on the client area and drag your mouse.

const int WM_NCHITTEST = 0x0084;
const int HTCAPTION = 2;
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_NCHITTEST)
{
Point pt = this.PointToClient(new Point(m.LParam.ToInt32()));
if (ClientRectangle.Contains(pt))
{
m.Result = new IntPtr(HTCAPTION);
return;
}
}

base.WndProc (ref m);
}

Hope this helps

--
Chris Taylor
http://dotnetjunkies.com/weblog/chris.taylor

"Nikki" <ap*********@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
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 mouse.
Thanks,

Nov 17 '05 #3

P: n/a
Is this the only way to respond to messages sent to a form? meaning having
to override the WndProc.

I've done Delphi development for quite a while now and in Delphi you can
bind a message to a method and the method would be called for you
automagiclly when the message was received. This prevented you from having
to have a giant case statement for each message you wanted to handle.

--
Thanks
Wayne Sepega
Jacksonville, Fl

Enterprise Library Configuration Console Module Generator
http://workspaces.gotdotnet.com/elccmg

"When a man sits with a pretty girl for an hour, it seems like a minute. But
let him sit on a hot stove for a minute and it's longer than any hour.
That's relativity." - Albert Einstein

"Chris Taylor" <ch*************@hotmail.com> wrote in message
news:Ok**************@TK2MSFTNGP14.phx.gbl...
Hi,

In addition to what Marcin has said, another solution is to handle the
WM_NCHITTEST message and return HTCAPTION if the form must be dragged. If
you add the following code to your Form, the form will be dragged when you
click on the client area and drag your mouse.

const int WM_NCHITTEST = 0x0084;
const int HTCAPTION = 2;
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_NCHITTEST)
{
Point pt = this.PointToClient(new Point(m.LParam.ToInt32()));
if (ClientRectangle.Contains(pt))
{
m.Result = new IntPtr(HTCAPTION);
return;
}
}

base.WndProc (ref m);
}

Hope this helps

--
Chris Taylor
http://dotnetjunkies.com/weblog/chris.taylor

"Nikki" <ap*********@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
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 mouse.
Thanks,


Nov 17 '05 #4

P: n/a
Hi,

I've got some code for your main Form.

Cheers!
Marcin

<code>
private Point downLocation;
private Point downMousePosition;
private bool isMouseDown=false;

protected override void OnMouseMove(MouseEventArgs e) {
lock(this) {
if( e.Button==MouseButtons.Left ) {
if( isMouseDown ) {
Point mousePos=Form.MousePosition;
Point location=downLocation;
location.X=location.X +
(mousePos.X-downMousePosition.X);
location.Y=location.Y +
(mousePos.Y-downMousePosition.Y);
base.Location=location;
}
else {
downLocation=base.Location;
downMousePosition=Form.MousePosition;
isMouseDown=true;
}
}
else {
isMouseDown=false;
}

base.OnMouseMove(e);
}
}

</code>
Hi Nikki,

In Your case you've got to handle MouseMove events of the
main form. Check if left-button is pressed and the change
the form location according to previous point (e.X, e.Y) of
handled event.

HTH
Marcin
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 mouse. Thanks,

Nov 17 '05 #5

P: n/a
Joe
Could also use the API directly:

private const int WM_NCLBUTTONDOWN = 0xA1;
private const int HTCAPTION = 0x2;

[ DllImport( "user32.dll" ) ]
public static extern bool ReleaseCapture();

[ DllImport( "user32.dll" ) ]
public static extern int SendMessage( IntPtr hWnd, int Msg, int wParam, int
lParam );

public static void MoveForm( IntPtr Handle )
{
ReleaseCapture();
int Status = SendMessage( Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0);
}
....

Then call this MoveForm function however you'd want to trigger the form
moving, say by clicking and holding on a different control:

private void TitleBar_MouseDown(object sender,
System.Windows.Forms.MouseEventArgs e)
{
//simulate clicking on windows standard title bar
if (e.Button == MouseButtons.Left) MoveForm(this.Handle);
}

"Nikki" wrote:
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 mouse.
Thanks,

Nov 17 '05 #6

P: n/a
Hi Wayne,

..NET does not support a method binding mechanism like that of Delphi for
windows messages.
In defence of C# had they included such a mechanism this would tightly tie
the language to the Windows platform.

Of course before going this route you should always make sure that there is
not an existing event that corresponds to the underlying message.
--
Chris Taylor
http://dotnetjunkies.com/weblog/chris.taylor
"Wayne" <Me******@community.nospam> wrote in message
news:##**************@TK2MSFTNGP14.phx.gbl...
Is this the only way to respond to messages sent to a form? meaning having
to override the WndProc.

I've done Delphi development for quite a while now and in Delphi you can
bind a message to a method and the method would be called for you
automagiclly when the message was received. This prevented you from having
to have a giant case statement for each message you wanted to handle.

--
Thanks
Wayne Sepega
Jacksonville, Fl

Enterprise Library Configuration Console Module Generator
http://workspaces.gotdotnet.com/elccmg

"When a man sits with a pretty girl for an hour, it seems like a minute. But let him sit on a hot stove for a minute and it's longer than any hour.
That's relativity." - Albert Einstein

"Chris Taylor" <ch*************@hotmail.com> wrote in message
news:Ok**************@TK2MSFTNGP14.phx.gbl...
Hi,

In addition to what Marcin has said, another solution is to handle the
WM_NCHITTEST message and return HTCAPTION if the form must be dragged. If you add the following code to your Form, the form will be dragged when you click on the client area and drag your mouse.

const int WM_NCHITTEST = 0x0084;
const int HTCAPTION = 2;
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_NCHITTEST)
{
Point pt = this.PointToClient(new Point(m.LParam.ToInt32()));
if (ClientRectangle.Contains(pt))
{
m.Result = new IntPtr(HTCAPTION);
return;
}
}

base.WndProc (ref m);
}

Hope this helps

--
Chris Taylor
http://dotnetjunkies.com/weblog/chris.taylor

"Nikki" <ap*********@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
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 mouse.
Thanks,



Nov 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.