How to move form in C#.Net? [duplicate] - c#

This question already has answers here:
Make a borderless form movable?
(20 answers)
How to move a Windows Form when its FormBorderStyle property is set to None? [duplicate]
(5 answers)
Winforms - Click/drag anywhere in the form to move it as if clicked in the form caption [duplicate]
(5 answers)
Closed 3 years ago.
Thanks you for your previous answers for my question.
you can see following link.
How to minimize and maximize in C#.Net?
Now i'm facing another problem.
When i changed my form's borderstyle to none,i can't move form like the real form.
Its stable and can't move anywhere.
In Windows form's normal borderstyle can move anywhere.
But i want to move like that in borderstyle's none property.
How can i do that?
Please let me know if you can.
Thanks you for your time. :)

public class AppFormBase : Form
{
public Point downPoint = Point.Empty;
protected override void OnLoad(EventArgs e)
{
if (FormBorderStyle == System.Windows.Forms.FormBorderStyle.None)
{
MouseDown += new MouseEventHandler(AppFormBase_MouseDown);
MouseMove += new MouseEventHandler(AppFormBase_MouseMove);
MouseUp += new MouseEventHandler(AppFormBase_MouseUp);
}
base.OnLoad(e);
}
private void AppFormBase_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
downPoint = new Point(e.X, e.Y);
}
private void AppFormBase_MouseMove(object sender, MouseEventArgs e)
{
if (downPoint != Point.Empty)
Location = new Point(Left + e.X - downPoint.X, Top + e.Y, - downPoint.Y);
}
private void AppFormBase_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
downPoint = Point.Empty;
}
}

Take a look at this tutorial: http://www.codeproject.com/KB/cs/csharpmovewindow.aspx.
Here's the gist of it:
using System.Runtime.InteropServices;
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 Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
ReleaseCapture();
SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
}
}

public const int WM_NCLBUTTONDOWN = 0xA1;
public const int HT_CAPTION = 0x2;
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
Capture = false;
Message msg = Message.Create(Handle, WM_NCLBUTTONDOWN, (IntPtr)HT_CAPTION, IntPtr.Zero);
base.WndProc(ref msg);
}

Related

reopen minimized window visual studio application developpment

im using visual studio to make a screen recorder using the ScreenRecorderLib library (but it is not related to my question) and c#. It is my first app ever even though i made some games in unity using c#.
I want to, on record button click or keyboard shortcut (for example crtl+a), to minimize the window in taskbar. I managed to do this using
private void RecordButton_Click(object sender, EventArgs e)
{
ActiveControl = null;
try
{
if (_IsRecording)
{
//some actions to stop recording (not interresting for my question)
WindowState = FormWindowState.Normal;
}
UpdateProgress();
if (_rec == null)
{
// some actions to make settings active and launch the recording
WindowState = FormWindowState.Minimized;
}
}}
and my code for the shortcuts
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control && e.KeyCode.ToString() == "A")
{
RecordButton.PerformClick();
}
}
This is not my real code but i tried to select some interresting parts for my question.
I manage to minimize my button in taskbar with shortcut or with button, but not to stop the recording using the shortcut while it is minimized. I would like that while recording and window minimised to be able to click crtl+a to stop the recording
Any ideas???
"How to use shortcut keys when the application is inactive" has been collected in API: MouseKeyHook.
You only need to modify the operations you need in the appropriate places.
globalmousekeyhook
Sample:
nuget install MouseKeyHook
public bool Flag { get; set; }
private void MinOrNarmal() {
//1. Define key combinations
var undo = Combination.TriggeredBy(Keys.A).With(Keys.Control);
//2. Define actions
Action actionUndo = DoSomething;
void DoSomething() {
if (Flag == false) {
this.WindowState = FormWindowState.Minimized;
Flag = true;
return;
}
if (Flag == true) {
this.WindowState = FormWindowState.Normal;
Flag = false;
}
}
//3. Assign actions to key combinations
var assignment = new Dictionary<Combination, Action> { { undo, actionUndo } };
//4. Install listener
Hook.GlobalEvents().OnCombination(assignment);
}
private void Form1_Load(object sender, EventArgs e) {
MinOrNarmal();
}
Press "Control+A":
You can also write global hotkeys yourself.
Only realize the function of shrinking the window:
private void Form1_Load(object sender, EventArgs e) {
this.KeyPreview = true;
}
private void Form1_KeyDown(object sender, KeyEventArgs e) {
if (e.Control && e.KeyCode == Keys.A) {
this.WindowState = FormWindowState.Minimized;
}
}
https://www.fluxbytes.com/csharp/how-to-register-a-global-hotkey-for-your-application-in-c/#comments
This perfectly answers my question. I will put a code down below for the people who want to have more than one shortcut. I'm a beginner, so mabe my explanations are not correct, but i tried to explain them in the esasiest way and in the way i understood them
public partial class Form1 : Form
{
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern bool RegisterHotKey(IntPtr hWnd, int id1, int fsModifiers, int vk);
//for creating another hotKey
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern bool RegisterHotKey1(IntPtr hWnd, int id2, int fsModifiers, int vk);
//here you can create even more shortcuts
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern bool UnregisterHotKey(IntPtr hWnd, int id1);
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern bool UnregisterHotKey1(IntPtr hWnd, int id2);
//remember unreg them
enum KeyModifier
{
None = 0,
Alt = 1,
Control = 2,
Shift = 4,
WinKey = 8
}
public Form1()
{
InitializeComponent();
int id1 = 0; // The id of the hotkey.
RegisterHotKey(this.Handle, id1, (int)KeyModifier.Shift, Keys.F1.GetHashCode());
int id2 = 1; // The 2nd id of the hotkey.
RegisterHotKey(this.Handle, id2, (int)KeyModifier.Shift, Keys.F2.GetHashCode());// Register Shift + A as global hotkey.
}
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (m.Msg == 0x0312)
{
if (m.WParam.ToInt32() == 0)
{
RecordButton.PerformClick();
}
if(m.WParam.ToInt32() == 1)
{
PauseButton.PerformClick();
}
//add the other shortcuts just replace == 1 by the id value
}
}
private void ExampleForm_FormClosing(object sender, FormClosingEventArgs e)
{
UnregisterHotKey(this.Handle, 0); // Unregister hotkey with id 0 before closing the form. You might want to call this more than once with different id values if you are planning to register more than one hotkey.
UnregisterHotKey(this.Handle, 1);
}
//delete them with the id

Sendkeys event is not detected inside the application in windows form application

I am new to windows app development. I am developing a Windows Form Application where the layout is as follows:
There is one textbox and i have created the keyboard inside the application using SendKeys event.
Problem is that all other application on the system are able to detect the keys but the textbox inside the application is not able to detect the keys.
Basically app is having complete keyboard this is just one button press code
What I have tried:
public partial class Form1 : Form
{
Control focusedC;
protected override CreateParams CreateParams
{
get
{
CreateParams param = base.CreateParams;
param.ExStyle |= 0x08000000;
return param;
}
}
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
FormBorderStyle = FormBorderStyle.None;
WindowState = FormWindowState.Maximized;
TopMost = true;
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape) {
FormBorderStyle = FormBorderStyle.Sizable;
WindowState = FormWindowState.Normal;
TopMost = false;
}
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
//checkbox is for CapsLock Key
}
private void button14_Click(object sender, EventArgs e)
{
if (checkBox1.Checked && focusedC != null)
{
focusedC.Focus();
SendKeys.Send("Q");
}
else if(focusedC != null)
{
focusedC.Focus();
SendKeys.Send("q");
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
focusedC = sender as TextBox;
}
}
Of cource it doesn't work on your window. You set the WS_EX_NOACTIVATE style! It works on other windows but not yours, obviously. If you want it to work on your textbox remove or comment this line
param.ExStyle |= 0x08000000;
and it will work fine in your app window not others:
private void button14_Click(object sender, EventArgs e)
{
if (checkBox1.Checked)
{
textBox1.Focus();
SendKeys.Send("Q");
}
else
{
textBox1.Focus();
SendKeys.Send("q");
}
}
For WPF applications, you have to use SendKeys.SendWait() method.
SendKeys.SendWait("Q")
SendKeys.Send() will work for WinForm Applications.
Another option is to use WinAPI instead of SendKeys. More information here
Edit 1
Control focusedC;
//Enter event handler for your TextBox
private void textBox1_TextChanged(object sender, EventArgs e)
{
focusedC = sender as TextBox;
}
//Click event handler
private void button14_Click(object sender, EventArgs e)
{
if (focusedC != null)
{
focusedC.Focus();
SendKeys.Send("Q");
}
}
Edit 2: Using WinAPI
[DllImport("user32.dll")]
static extern void SendInput(byte bVk, byte bScan, uint dwFlags, int dwExtraInfo);
public static void PressKey(byte keyCode)
{
const int KEYEVENTF_EXTENDEDKEY = 0x1;
const int KEYEVENTF_KEYUP = 0x2;
SendInput((byte)keyCode, 0x45, KEYEVENTF_EXTENDEDKEY, 0);
SendInput((byte)keyCode, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
}
Use by calling the PressKey function and Keycodes can be found here

How to make a picturebox dragable?

I am an enthusiastic programmer and new to stack overflow.
I am trying to build a prototype of an OS(Operating System) in C#...Just a test.
Just like we see that we can drag things in the desktop and put it anywhere I am creating a desktop for my OS.
So how should I make an icon(a picturebox) drag-able and how would I save its position so that next time I open my desktop I see it in the same place?
I would love the dragging without any freezes or those sneaky bugs. I would love if, it is as close and smooth as the ones in Windows(dragging items(icons) in desktop)..
Thanks...
Yes, it is.
Assume a Picturebox named "pbxBigCat" (load it with a pPicture ...)
Add this lines to your form:
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();
And then write an eventhandler for the pbxBigCat MouseDown event:
private void pbxBigCat_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
ReleaseCapture();
SendMessage(pbxBigCat.Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
}
}
If you test it you will se it works. For more Information (like saving positions etc.) I refere to Make a borderless form movable?
Another possibility to do this (now we have a Label called label1)
public partial class Form1 : Form
{
private bool mouseDown;
private Point lastLocation;
public Form1()
{
InitializeComponent();
}
private void pbxBigCat_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
mouseDown = true;
lastLocation = e.Location;
}
}
private void pbxBigCat_MouseMove(object sender, MouseEventArgs e)
{
if (mouseDown)
{
pbxBigCat.Location = new Point((pbxBigCat.Location.X - lastLocation.X + e.X), (pbxBigCat.Location.Y - lastLocation.Y) + e.Y);
label1.Text = pbxBigCat.Location.X.ToString() + "/" + pbxBigCat.Location.Y.ToString();
}
}
private void pbxBigCat_MouseUp(object sender, MouseEventArgs e)
{
mouseDown = false;
}
}
Everything drawn from the example on the mentioned SO article.

locking winform location

I have a main form window which will pop up a new form window. I want to lock the location of the popup form so that the window cannot be moved and it will move at the same time as the main form.
(so if a user drags the main form the popup moves with it)
Did a search on the site and some did it like this:
this.FormBorderStyle=System.Windows.Forms.FormBorderStyle.None
and I have the Locked attribute set to True but that doesn't work.
But I want to keep the borders. What's the proper way of locking a form?
public class Form1
{
private Form2 Form2 = new Form2();
private Point form2Location;
private Point form1Location;
private void Button1_Click(System.Object sender, System.EventArgs e)
{
form1Location = this.Location;
Form2.Show();
form2Location = Form2.Location;
}
private void Form1_Move(System.Object sender, System.EventArgs e)
{
Form2.IsMoving = true;
Point form2OffSetLocation = new Point(this.Location.X - form2Location.X, this.Location.Y - form2Location.Y);
Form2.Location = form2OffSetLocation;
Form2.IsMoving = false;
}
}
public class Form2
{
public bool IsMoving;
private void Form2_Move(System.Object sender, System.EventArgs e)
{
if (IsMoving) return;
if (staticLocation.X != 0 & staticLocation.Y != 0) this.Location = staticLocation;
}
private Point staticLocation;
private void Form2_Load(System.Object sender, System.EventArgs e)
{
staticLocation = this.Location;
}
}
I agree with Hans on this one and I think once you see how dodgy it looks you'll probably agree too.
You could do something like this (taken from here):
protected override void WndProc(ref Message message)
{
const int WM_SYSCOMMAND = 0x0112;
const int SC_MOVE = 0xF010;
switch(message.Msg)
{
case WM_SYSCOMMAND:
int command = message.WParam.ToInt32() & 0xfff0;
if (command == SC_MOVE)
return;
break;
}
base.WndProc(ref message);
}

Make a borderless form movable?

Is there a way to make a form that has no border (FormBorderStyle is set to "none") movable when the mouse is clicked down on the form just as if there was a border?
This article on CodeProject details a technique. Is basically boils down to:
public const int WM_NCLBUTTONDOWN = 0xA1;
public const int HT_CAPTION = 0x2;
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern bool ReleaseCapture();
private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
ReleaseCapture();
SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
}
}
This essentially does exactly the same as grabbing the title bar of a window, from the window manager's point of view.
Let's not make things any more difficult than they need to be. I've come across so many snippets of code that allow you to drag a form around (or another Control). And many of them have their own drawbacks/side effects. Especially those ones where they trick Windows into thinking that a Control on a form is the actual form.
That being said, here is my snippet. I use it all the time. I'd also like to note that you should not use this.Invalidate(); as others like to do because it causes the form to flicker in some cases. And in some cases so does this.Refresh. Using this.Update, I have not had any flickering issues:
private bool mouseDown;
private Point lastLocation;
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
mouseDown = true;
lastLocation = e.Location;
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if(mouseDown)
{
this.Location = new Point(
(this.Location.X - lastLocation.X) + e.X, (this.Location.Y - lastLocation.Y) + e.Y);
this.Update();
}
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
mouseDown = false;
}
Another simpler way to do the same thing.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
// set this.FormBorderStyle to None here if needed
// if set to none, make sure you have a way to close the form!
}
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (m.Msg == WM_NCHITTEST)
m.Result = (IntPtr)(HT_CAPTION);
}
private const int WM_NCHITTEST = 0x84;
private const int HT_CLIENT = 0x1;
private const int HT_CAPTION = 0x2;
}
use MouseDown, MouseMove and MouseUp. You can set a variable flag for that. I have a sample, but I think you need to revise.
I am coding the mouse action to a panel. Once you click the panel, your form will move with it.
//Global variables;
private bool _dragging = false;
private Point _offset;
private Point _start_point=new Point(0,0);
private void panel1_MouseDown(object sender, MouseEventArgs e)
{
_dragging = true; // _dragging is your variable flag
_start_point = new Point(e.X, e.Y);
}
private void panel1_MouseUp(object sender, MouseEventArgs e)
{
_dragging = false;
}
private void panel1_MouseMove(object sender, MouseEventArgs e)
{
if(_dragging)
{
Point p = PointToScreen(e.Location);
Location = new Point(p.X - this._start_point.X,p.Y - this._start_point.Y);
}
}
WPF only
don't have the exact code to hand, but in a recent project I think I used MouseDown event and simply put this:
frmBorderless.DragMove();
Window.DragMove Method (MSDN)
It worked for Me.
private Point _mouseLoc;
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
_mouseLoc = e.Location;
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
int dx = e.Location.X - _mouseLoc.X;
int dy = e.Location.Y - _mouseLoc.Y;
this.Location = new Point(this.Location.X + dx, this.Location.Y + dy);
}
}
Ref. video Link
This is tested and easy to understand.
protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
case 0x84:
base.WndProc(ref m);
if((int)m.Result == 0x1)
m.Result = (IntPtr)0x2;
return;
}
base.WndProc(ref m);
}
Since some answers do not allow for child controls to be draggable, I've created a little helper class.
It should be passed the top level form. Can be made more generic if desired.
class MouseDragger
{
private readonly Form _form;
private Point _mouseDown;
protected void OnMouseDown(object sender, MouseEventArgs e)
{
_mouseDown = e.Location;
}
protected void OnMouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
int dx = e.Location.X - _mouseDown.X;
int dy = e.Location.Y - _mouseDown.Y;
_form.Location = new Point(_form.Location.X + dx, _form.Location.Y + dy);
}
}
public MouseDragger(Form form)
{
_form = form;
MakeDraggable(_form);
}
private void MakeDraggable(Control control)
{
var type = control.GetType();
if (typeof(Button).IsAssignableFrom(type))
{
return;
}
control.MouseDown += OnMouseDown;
control.MouseMove += OnMouseMove;
foreach (Control child in control.Controls)
{
MakeDraggable(child);
}
}
}
There's no property you can flip to make this just happen magically. Look at the events for the form and it becomes fairly trivial to implement this by setting this.Top and this.Left. Specifically you'll want to look at MouseDown, MouseUp and MouseMove.
public Point mouseLocation;
private void frmInstallDevice_MouseDown(object sender, MouseEventArgs e)
{
mouseLocation = new Point(-e.X, -e.Y);
}
private void frmInstallDevice_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
Point mousePos = Control.MousePosition;
mousePos.Offset(mouseLocation.X, mouseLocation.Y);
Location = mousePos;
}
}
this can solve ur problem....
https://social.msdn.microsoft.com/Forums/vstudio/en-US/d803d869-68e6-46ff-9ff1-fabf78d6393c/how-to-make-a-borderless-form-in-c?forum=csharpgeneral
This bit of code from the above link did the trick in my case :)
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
if (e.Button == MouseButtons.Left)
{
this.Capture = false;
Message msg = Message.Create(this.Handle, 0XA1, new IntPtr(2), IntPtr.Zero);
this.WndProc(ref msg);
}
}
Best way I've found (modified of course)
// This adds the event handler for the control
private void AddDrag(Control Control) { Control.MouseDown += new System.Windows.Forms.MouseEventHandler(this.DragForm_MouseDown); }
public const int WM_NCLBUTTONDOWN = 0xA1;
public const int HT_CAPTION = 0x2;
[System.Runtime.InteropServices.DllImportAttribute("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
[System.Runtime.InteropServices.DllImportAttribute("user32.dll")]
public static extern bool ReleaseCapture();
private void DragForm_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
ReleaseCapture();
SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
// Checks if Y = 0, if so maximize the form
if (this.Location.Y == 0) { this.WindowState = FormWindowState.Maximized; }
}
}
To apply drag to a control simply insert this after InitializeComponent()
AddDrag(NameOfControl);
For .NET Framework 4,
You can use this.DragMove() for the MouseDown event of the component (mainLayout in this example) you are using to drag.
private void mainLayout_MouseDown(object sender, MouseButtonEventArgs e)
{
this.DragMove();
}
Easiest way is:
First create a label named label1.
Go to label1's events > mouse events > Label1_Mouse Move and write these:
if (e.Button == MouseButtons.Left){
Left += e.X;
Top += e.Y;`
}
I was trying to make a borderless windows form movable which contained a WPF Element Host control and a WPF User control.
I ended up with a stack panel called StackPanel in my WPF user control which seemed the logical thing to try click on to move. Trying junmats's code worked when I moved the mouse slowly, but if I moved the mouse faster, the mouse would move off the form and the form would be stuck somewhere mid move.
This improved on his answer for my situation using CaptureMouse and ReleaseCaptureMouse and now the mouse does not move off the form while moving it even if I move it quickly.
private void StackPanel_MouseDown(object sender, MouseButtonEventArgs e)
{
_start_point = e.GetPosition(this);
StackPanel.CaptureMouse();
}
private void StackPanel_MouseUp(object sender, MouseButtonEventArgs e)
{
StackPanel.ReleaseMouseCapture();
}
private void StackPanel_MouseMove(object sender, MouseEventArgs e)
{
if (StackPanel.IsMouseCaptured)
{
var p = _form.GetMousePositionWindowsForms();
_form.Location = new System.Drawing.Point((int)(p.X - this._start_point.X), (int)(p.Y - this._start_point.Y));
}
}
//Global variables;
private Point _start_point = new Point(0, 0);
I'm expanding the solution from jay_t55 with one more method ToolStrip1_MouseLeave that handles the event of the mouse moving quickly and leaving the region.
private bool mouseDown;
private Point lastLocation;
private void ToolStrip1_MouseDown(object sender, MouseEventArgs e) {
mouseDown = true;
lastLocation = e.Location;
}
private void ToolStrip1_MouseMove(object sender, MouseEventArgs e) {
if (mouseDown) {
this.Location = new Point(
(this.Location.X - lastLocation.X) + e.X, (this.Location.Y - lastLocation.Y) + e.Y);
this.Update();
}
}
private void ToolStrip1_MouseUp(object sender, MouseEventArgs e) {
mouseDown = false;
}
private void ToolStrip1_MouseLeave(object sender, EventArgs e) {
mouseDown = false;
}
Also if you need to DoubleClick and make your Form bigger/smaller , you can use the First answer, create a global int variable, add 1 every time user clicks on the component you use for dragging. If variable == 2 then make your form bigger/smaller. Also use a timer for every half a sec or a second to make your variable = 0;
Adding a MouseLeftButtonDown event handler to the MainWindow worked for me.
In the event function that gets automatically generated, add the below code:
base.OnMouseLeftButtonDown(e);
this.DragMove();
Form1(): new Moveable(control1, control2, control3);
Class:
using System;
using System.Windows.Forms;
class Moveable
{
public const int WM_NCLBUTTONDOWN = 0xA1;
public const int HT_CAPTION = 0x2;
[System.Runtime.InteropServices.DllImportAttribute("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
[System.Runtime.InteropServices.DllImportAttribute("user32.dll")]
public static extern bool ReleaseCapture();
public Moveable(params Control[] controls)
{
foreach (var ctrl in controls)
{
ctrl.MouseDown += (s, e) =>
{
if (e.Button == MouseButtons.Left)
{
ReleaseCapture();
SendMessage(ctrl.FindForm().Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
// Checks if Y = 0, if so maximize the form
if (ctrl.FindForm().Location.Y == 0) { ctrl.FindForm().WindowState = FormWindowState.Maximized; }
}
};
}
}
}
I tried the following and presto changeo, my transparent window was no longer frozen in place but could be moved!! (throw away all those other complex solutions above...)
private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
base.OnMouseLeftButtonDown(e);
// Begin dragging the window
this.DragMove();
}

Categories