Drag form w/out controlbox

R

Ryan Joseph So

Hi,
I have a form which the formborderstyle was set to none hiding the
controlbox. My problem is I can't reposition the form on the screen by
using my mouse when I run the application because it has no controlbox.
Is it possible to drag the form to other location on the screen without
the controlbox? How? Thanks in advance.

Ryan.
 
P

Peter Jausovec

Hi,

I use this code:

private Point mouseOffset;
private bool isMouseDown = false;

private void frm_MouseDown(object sender,
System.Windows.Forms.MouseEventArgs e)
{
int xOffset;
int yOffset;
if (e.Button == MouseButtons.Left)
{
xOffset = -e.X - SystemInformation.FrameBorderSize.Width;
yOffset = -e.Y -
SystemInformation.CaptionHeight -SystemInformation.FrameBorderSize.Height;

mouseOffset = new Point(xOffset, yOffset);
isMouseDown = true;
}
}
private void frm_MouseMove(object sender,
System.Windows.Forms.MouseEventArgs e)
{
if (isMouseDown)
{
Point mousePos = Control.MousePosition;
mousePos.Offset(mouseOffset.X, mouseOffset.Y);
Location = mousePos;
}
}

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

Herfried K. Wagner [MVP]

Ryan Joseph So said:
I have a form which the formborderstyle was set to none hiding the
controlbox. My problem is I can't reposition the form on the screen by
using my mouse when I run the application because it has no controlbox.
Is it possible to drag the form to other location on the screen without
the controlbox?

<URL:http://groups.google.de/[email protected]>
 
G

Guest

I use this class...

when I want to Move a Form.
..
Eg on a mouse click..

Win32API.SendMessage_MouseDownInCaption( form)


//*********** Win32App*****************
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Drawing;

namespace Bon
{
/// <summary>
/// Static Methods and Constants to inferface with Win32API.
/// </summary>
public class Win32API
{
static Win32API()
{
}
/// <summary>
/// Method: Release capturing of Mouse Events..
/// </summary>
/// <returns></returns>
[DllImport("User32.dll")]
public static extern bool ReleaseCapture();

/// <summary>
/// Method: Access to the Win32 SendMessage API
/// </summary>
/// <param name="hWnd"></param>
/// <param name="msg"></param>
/// <param name="wParam"></param>
/// <param name="lParam"></param>
/// <returns></returns>
[DllImport("user32.dll", CharSet=CharSet.Auto)]
extern public static IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam,
IntPtr lParam) ;
/// <summary>
/// Send a WM_MOUSELEAVE manually to a window.
/// </summary>
/// <param name="hWnd"></param>
public static void SendMessage_WM_MOUSELEAVE(IntPtr hWnd)
{
SendMessage(hWnd,(int)WM_Message.WM_MOUSELEAVE, IntPtr.Zero, IntPtr.Zero);
}
/// <summary>
/// Sends Form a Message (WM_NCLBUTTONDOWN) that it is in Caption so form is
in
/// Move mode..
/// </summary>
/// <param name="form"></param>
public static void SendMessage_MouseDownInCaption(Form form)
{
if (form != null)
{
const int WM_NCLBUTTONDOWN = 0x00A1;
const int HTCAPTION = 2;
//Position cursor inside current Caption
Point locationOnScreen = form.Location;
if (form.Parent != null)
{
locationOnScreen = form.Parent.PointToScreen(form.Location);
}
Point pointInCaption =
new Point( locationOnScreen.X + 4, locationOnScreen.Y + 4 );
ReleaseCapture();
IntPtr lParam = GetLParam(pointInCaption);
Cursor.Position = pointInCaption;
Application.DoEvents();
SendMessage(form.Handle, WM_NCLBUTTONDOWN ,(IntPtr)HTCAPTION, lParam);
}
}
/// <summary>
/// Static Method: Returns a Long from a lowWord and HiWord Integer
/// </summary>
/// <param name="LoWord"></param>
/// <param name="HiWord"></param>
/// <returns></returns>
public static int GetLong(int loWord, int hiWord)
{
return (hiWord << 16) | (loWord & 0xffff);
}
/// <summary>
/// Return a LParam structure pointer from a LoWord and HiWord
/// </summary>
/// <param name="LoWord"></param>
/// <param name="HiWord"></param>
/// <returns></returns>
public static IntPtr GetLParam(int loWord, int hiWord)
{
return (IntPtr) ((hiWord << 16) | (loWord & 0xffff));
}
/// <summary>
/// Return a LParam structure pointer from Point
/// </summary>
/// <param name="LoWord"></param>
/// <param name="HiWord"></param>
/// <returns></returns>
public static IntPtr GetLParam(Point point)
{
return (IntPtr) ((point.X << 16) | (point.Y & 0xffff));
} /// <summary>
/// Method: Lets the HiWord from an integer value, good for interpreting
/// LParam, WParam etc
/// </summary>
/// <param name="aValue"></param>
/// <returns></returns>
public static int GetHiWord(int aValue)
{
return (short)(((uint)aValue & 0xFFFF0000U) >> 16);
}
/// <summary>
/// Method: Lets the LoWord from an integer value, good for interpreting
/// LParam, WParam etc
/// </summary>
/// <param name="aValue"></param>
/// <returns></returns>
public static int GetLoWord(int aValue)
{
//return aValue & 0xffff;
return (short)((uint)aValue & 0x0000FFFFU);
}
}
}
 
R

Ryan Joseph So

Thank you very much for the quick post. I'll try your codes immediately.
 

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