PDA

View Full Version : dragکردن فرم



feree123
جمعه 15 مرداد 1389, 19:04 عصر
سلام
من میخوام یه فرم رو که form border آن برابر none است رو جا به جا کنم
لطفا کمکم کنین!:گریه::گریه:

MSN_Issue
جمعه 15 مرداد 1389, 21:26 عصر
جابجا کردن یک فرم با کلیک روی هر جای آن :

private bool dragging;
private Point pointClicked;
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
// Turn drag mode on and store the point clicked.
dragging = true;
pointClicked = new Point(e.X, e.Y);
}
else
{
dragging = false;
}
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (dragging)
{
Point pointMoveTo;
// Find the current mouse position in screen coordinates.
pointMoveTo = this.PointToScreen(new Point(e.X, e.Y));
// Compensate for the position the control was clicked.
pointMoveTo.Offset(-pointClicked.X, -pointClicked.Y);
// Move the form.
this.Location = pointMoveTo;
}
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
dragging = false;
}

feree123
شنبه 16 مرداد 1389, 02:24 صبح
ممنون ولی من یه راه ساده تر رو یاد گرفتم:




publicpartialclassForm1 : Form
{
bool mouseisdown;
int x, y, dx, dy;
public Form1()
{
InitializeComponent();
}
privatevoid Form1_MouseMove(object sender, MouseEventArgs e)
{
if (mouseisdown)
{
dx = e.X - x;
dy = e.Y - y;
this.Location = newPoint(this.Location.X + dx, this.Location.Y + dy);
}
}
privatevoid Form1_MouseDown(object sender, MouseEventArgs e)
{
this.mouseisdown = true;
x = e.X;
y = e.Y;
}
privatevoid Form1_MouseUp(object sender, MouseEventArgs e)
{
this.mouseisdown = false;
}
}

فکر کنم این خیلی بهتر باشه