1.实现怎样通过拖放打开一张影像?
PictureBox 也有DragEnter事件 DragDrop事件 但是怎么样都不触发.没有AllowDrag属性。
Form有Allow属性。
只要知道鼠标现在落在PictureBox上,事件触发就行了。
private void Form1_DragDrop(object sender, DragEventArgs e)
{
int x = this.PointToClient(new Point(e.X, e.Y)).X;
int y = this.PointToClient(new Point(e.X, e.Y)).Y;
if (x >= pictureBox1.Location.X && x <= pictureBox1.Location.X + pictureBox1.Width && y >= pictureBox1.Location.Y && y <= pictureBox1.Location.Y + pictureBox1.Height)
{
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
pictureBox1.Image = Image.FromFile(files[0]);
}
}
private void Form1_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
}
从ListView中拖拽到PictureBox
int x = this.PointToClient(new Point(e.X, e.Y)).X;//获取当前鼠标的坐标X
int y = this.PointToClient(new Point(e.X, e.Y)).Y;//获取鼠标当前的坐标Y
if (x >= pictureBox1.Location.X && x <= pictureBox1.Location.X + pictureBox1.Width && y >= pictureBox1.Location.Y && y <= pictureBox1.Location.Y + pictureBox1.Height)
{
ListViewItem LVI = (ListViewItem)e.Data.GetData(typeof(ListViewItem));
string StrPath = LVI.Text;
pictureBox1.Image = Image.FromFile(StrPath);
}
//拖动时显示的效果
private void listView1_ItemDrag(object sender, ItemDragEventArgs e)
{
DoDragDrop(e.Item, DragDropEffects.Move);//开始拖放操作
}