wpf(C#)中如何实现Image的移动操作?

beipiao1008 2008-10-08 04:35:18
在一WPF项目中想要对几个图像进行移动操作,于是将其放在Image里面
可是只能对一个进行移动操作,请问如何对另一个也进行移动操作呀[如下面中的myImage1]?
现在中是放了二个,如果放多个时,如果实现对他们的拖动操作?[选中其中一个进行拖动操作时,其它的不动]

望高手指点,在线等~~解决发上散分.

相关代码如下:
XAML代码:
[code=XAML]
<Window x:Class="WpfApplication2.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="348" Width="430">
<Canvas MouseMove="Canvas_MouseMove"
MouseDown="Canvas_MouseDown"
MouseUp="Canvas_MouseUp"
Height="320" Width="350"
Background="Black"
ForceCursor="False" Name="myCanvas">
<Image Name="myImage" Width="61" Height="86"
Canvas.Left="50" Canvas.Top="50"
Source="images/2.gif" Cursor="Hand">
</Image>
<Image Name="myImage1" Width="62" Height="90"
Canvas.Left="150" Canvas.Top="150"
Cursor="Hand" Source="images/1.gif" >
</Image>
</Canvas>
</Window>
[/code]
对myImage的移动代码[其它的不会写了^_^]:

bool isBeginMove = false;
private void Canvas_MouseMove(object sender, MouseEventArgs e)
{
if (isBeginMove)
{
Point position = e.GetPosition(this);
double pX = position.X;
double pY = position.Y;


myImage.SetValue(Canvas.LeftProperty, pX - myImage.Width / 2);
myImage.SetValue(Canvas.TopProperty, pY - myImage.Height / 2);
}
}

private void Canvas_MouseDown(object sender, MouseButtonEventArgs e)
{
if (e.ButtonState == e.LeftButton)
{
isBeginMove = true;

}
}

private void Canvas_MouseUp(object sender, MouseButtonEventArgs e)
{
isBeginMove = false;
}
...全文
1471 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
more0308 2009-03-02
  • 打赏
  • 举报
回复
成了··刚才TRY了一下··最后看了·是权限问题··解决了··麻烦了·
more0308 2009-03-02
  • 打赏
  • 举报
回复
我到有个问题很想问···
这个是WPF的··能成··
如果我是PAGE的工程···
DragDrop.DoDragDrop(ImageCanvas, _selectImage, DragDropEffects.Move);
就会出现异常··
如何能在PAGE运行正常呢·?
beipiao1008 2008-10-11
  • 打赏
  • 举报
回复
非常感谢zhouyongh
当然也感谢大家的关注 thanks
zhouyongh 2008-10-11
  • 打赏
  • 举报
回复
给你写一个

[code=XAML]
<Window x:Class="WpfApplication8.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
Title="Window1" Height="300" Width="300">
<Window.Resources>
<Style TargetType="Image">
<EventSetter Event="PreviewMouseLeftButtonDown" Handler="Image_PreviewMouseLeftButtonDown"/>
</Style>
</Window.Resources>
<Grid>
<Canvas Name="ImageCanvas" DragOver="ImageCanvas_DragOver" AllowDrop="True"
PreviewMouseLeftButtonUp="ImageCanvas_PreviewMouseLeftButtonUp"
MouseMove="ImageCanvas_MouseMove">
<Image Canvas.Left="120" Height="81" Width="60" Source="yohan.jpg"/>
<Image Height="81" Width="60" Source="yohan.jpg"/>
<Image Canvas.Left="180" Height="81" Width="60" Source="yohan.jpg"/>
</Canvas>
</Grid>
</Window>
[/code]


public partial class Window1 : Window
{
private bool _isDragging = false;
private Point _startPoint;
private Point _relativePoint;
private Image _selectImage;

public Window1()
{
InitializeComponent();
}

private void Image_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
_selectImage = sender as Image;
_startPoint = e.GetPosition(ImageCanvas);
_relativePoint = e.GetPosition(_selectImage);
_isDragging = true;
foreach (Image image in ImageCanvas.Children)
{
image.SetValue(Canvas.ZIndexProperty, 0);
}
_selectImage.SetValue(Canvas.ZIndexProperty, 1);
}

private void ImageCanvas_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
_isDragging = false;
}

private void ImageCanvas_DragOver(object sender, DragEventArgs e)
{
if (e.Data != null && e.Data.GetDataPresent(typeof(Image)))
{
Image image = e.Data.GetData(typeof(Image)) as Image;
Point p = e.GetPosition(ImageCanvas);

image.SetValue(Canvas.LeftProperty, p.X - _relativePoint.X);
image.SetValue(Canvas.TopProperty, p.Y - _relativePoint.Y);
}
}

private void ImageCanvas_MouseMove(object sender, MouseEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed && _isDragging )
{
Point newPos = e.GetPosition(ImageCanvas);
if ((Math.Abs(newPos.X - _startPoint.X) > SystemParameters.MinimumHorizontalDragDistance) ||
(Math.Abs(newPos.Y - _startPoint.Y) > SystemParameters.MinimumVerticalDragDistance))
{
ImageCanvas.GiveFeedback += new GiveFeedbackEventHandler(ImageCanvas_GiveFeedback);
DragDrop.DoDragDrop(ImageCanvas, _selectImage, DragDropEffects.Move);
_isDragging = false;
ImageCanvas.GiveFeedback -= new GiveFeedbackEventHandler(ImageCanvas_GiveFeedback);
}
}
}

void ImageCanvas_GiveFeedback(object sender, GiveFeedbackEventArgs e)
{
Mouse.SetCursor(Cursors.Arrow); //Set Cursor
e.Handled = true;
}
}


Hope this helps
JeffChung 2008-10-10
  • 打赏
  • 举报
回复
还没学wpf,只好帮顶
alexjordon 2008-10-10
  • 打赏
  • 举报
回复
UP
我也来
longjun1627 2008-10-10
  • 打赏
  • 举报
回复
呵呵,顶也有分呀,UP~~UP~~
gogogo 2008-10-10
  • 打赏
  • 举报
回复
没用过,只能帮顶了
beipiao1008 2008-10-10
  • 打赏
  • 举报
回复
up~~
beipiao1008 2008-10-09
  • 打赏
  • 举报
回复
各位进来看看呀,不会的帮顶也给分,明天结帖~~
zlb789 2008-10-09
  • 打赏
  • 举报
回复
学习 帮顶
twtetg 2008-10-09
  • 打赏
  • 举报
回复
哇,Windows Presentation Foundation?微软的最新技术都搬出来了啊,等高人来解答
beipiao1008 2008-10-09
  • 打赏
  • 举报
回复
汗,怎么WPF的帖子都没人理的呀?
longjun1627 2008-10-08
  • 打赏
  • 举报
回复
友情关注,学习~~ UP~
具体要求:***********尽量做的简单化 别用CSS技术以及自定义控件 别用脚本语言 我们看不懂 达到我们学生初级初级水平****** 在线售票系统(毕业设计) 系统设置:密码修改 增加用户(权限) [打印机设置 票样打印设置 这2块用不着实现系统界面上 放着就好了] 基础设置:基础参数设置(买票设置/订票设置/退票设置)(比如多少时间之前不能买票订票) 车票设置(标准票/儿童票/。。。增删改) 车辆设置(增删该) 车次设置(增删该) 运营计划设置(调度设置) 前台营业:销售车票 预定车票 退回车票 信息查询:售票信息查询 订票信息查询 运营计划查询 当班信息查询(类似当班收入什么的) 营业统计:日售票报表统计 月售票报表统计 季度售票报表统计 常用工具:记事本、计算器 备注信息:只要程序 不要论文 5/1号要 你看能不 能按照这个界面这样做 这样应该做界面的人有个参照就简单点吧 最好按照这个系统的流程做 有些具体的我订单上没有写说明的就省了吧 但是你要把刚刚我们2个的都做简单再简单。。。。行不 别用java脚本 或者CSS 不然我们不懂的 界面漂亮点 代码菜鸟点 使用帮助: 1. 把DB文件夹的主数据库webSealTicket_Data.MDF 还原到你的sql 2000数据库系统 名称不要变为webSealTicket。 2. App_Code文件夹下是sql静态链接类System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection("server=(local);database=webSealTicket;uid=sa;pwd=;"); 如直接还原这里不用变化"server=(local)是你的本机的ip地址,database=webSealTicket 是你的数据库名称不要变化,uid=sa 是sql2000的用户名,pwd位密码。 3. login.aspx文件设置成主页,为登录界面。登录成共进入Index.aspx页面。 4. image 文件夹为系统用到的图片。 5. 如出现连接数据库问题请查看Web.Config文件用记事本打开配置节 为程序用到的数据库链接。name="webSealTicketConnectionString"为连接字符串。connectionString="Data Source=.;Initial Catalog=webSealTicket; 为数据源。 Security=True"启用安全。 @更多@ http://cleopard.download.csdn.net/ 福利 http://xuemeilaile.com @更多@ http://download.csdn.net/user/cleopard/album 17份软件测试文档 http://download.csdn.net/album/detail/1425 13份WPF经典开发教程 http://download.csdn.net/album/detail/1115 C#资料合辑二[C#桌面编程入门篇] http://download.csdn.net/album/detail/957 C#资料合辑一[C#入门篇] http://download.csdn.net/album/detail/669 [Csharp高级编程(第6版)](共8压缩卷) http://download.csdn.net/album/detail/667 10个[精品资源]Java学习资料合辑[一] http://download.csdn.net/album/detail/663 10个C#Socket编程代码示例 http://download.csdn.net/album/detail/631 6份GDI+程序设计资源整

110,552

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧