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

  • Thread starter Thread starter Nikki
  • Start date Start date
N

Nikki

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,
 
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,

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
 
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
 
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);
}
}
 
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);
}
 
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.
 
Back
Top