Reputation: 199
So I need to move my form no matter what element is clicked (I need to drag form by pressing and holding button, form is 100% transparent) , I tried to do this:
private void MessageForm_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
this.DragMove();
}
but I was quite surprised that there in no DragMove()
method, it was renamed or what I am missing?
And if this is not possible, is there any other way to do that?
Upvotes: 2
Views: 3014
Reputation: 1356
Tested and working: this.DragMove(), alternative
private void Form1_Load(object sender, EventArgs e)
{
FormCommonSetting(this);
}
public void FromCommonSetting(Form _Form)
{
_Form.StartPosition = FormStartPosition.CenterScreen;
_Form.FormBorderStyle = FormBorderStyle.None;
_Form.MaximizeBox = false;
_Form.ShowInTaskbar = true;
_Form.AutoSize = false;
}
protected override void WndProc(ref Message _Message)
{
switch (_Message.Msg)
{
case 0x84:
base.WndProc(ref _Message);
if ((int)_Message.Result == 0x1)
_Message.Result = (IntPtr)0x2;
return;
}
base.WndProc(ref _Message);
}
Upvotes: 0
Reputation: 9789
You will need something like this:
public const int WM_NCLBUTTONDOWN = 0xA1;
public const int HT_CAPTION = 0x2;
[DllImportAttribute("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
[DllImportAttribute("user32.dll")]
public static extern bool ReleaseCapture();
private void MessageForm_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
ReleaseCapture();
SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
}
}
private void button1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
ReleaseCapture();
SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
}
}
Basically, it acts like dragging the title bar/window caption when you drag anywhere in the window. This is great for borderless windows.
EDIT: If you use a button as the control for moving the form, you will need to be careful when attaching your click event handler as you are overriding the Windows Forms event loop for that control.
By moving/adding the ReleaseCapture and SendMessage calls to the MouseDown event of a control, you can use it to drag the window. Any control can be used to drag the window as long as you update the MouseDown event to be similar to the code above.
Upvotes: 6