Today, i will tell you a Program to move a Form ( preferably Windows Form Application, although it can be made in WPF too) by dragging the mouse.
Here's how to do it:
1. STEP 1 - Create New WFA
Here's how to do it:
1. STEP 1 - Create New WFA
If you know how to create a WFA, then Go to Step 2
- Open Visual Studio, and Click "New Project"
- Go to Templates > Visual C# > Windows.
- Select Windows Form Application from the List
bool Mouse_Cord_Stored = false;
int X_Coordinate;
int Y_Coordinate;
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
switch (e.Button)
{
//To Let user move the Form1 when Left mouse button is pressed
case (MouseButtons.Left):
{
switch (Mouse_Cord_Stored)
{
case false:
{
//If Mouse Coordinates are not stored, then to store them.
X_Coordinate = e.X;
Y_Coordinate = e.Y;
Mouse_Cord_Stored = true;
break;
}
case true:
{
//Move the Form using ActiveForm.Location if mouse coordinates are stored
Form1.ActiveForm.Location =
new Point(Form1.ActiveForm.Location.X + e.X - X_Coordinate,
Form1.ActiveForm.Location.Y + e.Y - Y_Coordinate);
break;
}
}
break;
}
default:
{
switch (Mouse_Cord_Stored)
{
case true:
{
Mouse_Cord_Stored = false;
break;
}
}
break;
}
}
}
Output:
Output of this Program will be a Windows Form, which can be moved by dragging MainForm
No comments:
Post a Comment