Form sayfasında belirlenen bir nesneyi hareket ettirmek içim Mouse olayları tanımlanmıştır. Bu olaylar yardımıyla nesneyi hareket ettirebilirsiniz. Bu uygulamamızda yüklenen bir resmi fareyle hareket ettireceğiz.
Form sayfasına bir adet buttun, bir adet panel, bir adet picturebox ekleyiniz. Aşağıdaki kodları yazınız.
using System;
using System.Drawing;
using System.Windows.Forms;
namespace Picturebox_Move_Image_With_Mouse
{
public partial class Form1 : Form
{
bool Dragging;
int xPos;
int yPos;
public Form1()
{
InitializeComponent();
}
private void selectIm_Click(object sender, EventArgs e)
{
OpenFileDialog opf = new OpenFileDialog();
if (opf.ShowDialog() == DialogResult.OK)
{
pictureBox1.Image = Image.FromFile(opf.FileName);
pictureBox1.Cursor = Cursors.Hand;
}
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e) { Dragging = false; }
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
Dragging = true;
xPos = e.X;
yPos = e.Y;
}
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
Control c = sender as Control;
if (Dragging && c != null)
{
c.Top = e.Y + c.Top - yPos;
c.Left = e.X + c.Left - xPos;
}
}
}
}
|