我写了一个wpf程序,主要实现 把用户选择的图片加载到窗体,并能随意拖动图片,加载的代码写在了菜单的click事件里,这样就带来一个问题,当用户选择多个大点的图片时,程序的 UI就会出现无响应的.希望有人能帮我想想可行的解决办法,谢谢!以下是实现代码
windows1.xaml<Window x:Class="GetPicWave.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="图片摆放" Width="300" Height=" 300"
MouseLeftButtonUp="Window_MouseLeftButtonUp"
MouseMove="Window_MouseMove"
Icon="app.ico"
Background="AliceBlue"
>
<Window.ContextMenu>
<ContextMenu>
<MenuItem Header="open" Click="MenuItem_Click"/>
<Separator />
<MenuItem Header="Clear" Click="MenuItem_Click_1" />
<Separator />
<MenuItem Header="Close" Click="MenuItem_Click_2" />
</ContextMenu>
</Window.ContextMenu>
<Canvas Name="canvas">
</Canvas>
</Window>windows1.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Forms;
using System.Threading;
using System.ComponentModel;namespace GetPicWave
{
/// <summary>
/// Window1.xaml 的交互逻辑
/// </summary>
public partial class Window1 : Window
{
UIElement uiOnDrag = null ;
bool bIsDraging = false;
double ix, iy; public Window1()
{
InitializeComponent(); }
private void MenuItem_Click(object sender, RoutedEventArgs e)
{ //打开文件
OpenFileDialog openFileDialog1 = new OpenFileDialog(); openFileDialog1.InitialDirectory = "d:\\";
openFileDialog1.Filter = "图片 files (*.jpg)|*.jpg|All files (*.*)|*.*";
openFileDialog1.FilterIndex = 1;
openFileDialog1.RestoreDirectory = true;
openFileDialog1.Multiselect = true; if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
try
{
Random rnd = new Random(100);
Int32 ic = 0;
foreach (string fullname in openFileDialog1.FileNames)
{
Uri url = new Uri(fullname);
BitmapImage bmp = new BitmapImage(url);
Image img = new Image();
img.Source = bmp; img.Width = 400;
img.Height = 300; canvas.Children.Add(img);
ic = rnd.Next(800);
Canvas.SetLeft(img, ic);
ic = rnd.Next(600);
Canvas.SetTop(img, ic);
img.MouseLeftButtonDown += Window_MouseLeftButtonDown;
} }
catch (Exception ex)
{
System.Windows.MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
}
}
}
private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
uiOnDrag = e.Source as UIElement;
// Image tempImg = e.Source as Image;
Int32 i = 1;
foreach (UIElement uiETemp in canvas.Children)
{
if (i < Canvas.GetZIndex(uiETemp)) i = Canvas.GetZIndex(uiETemp);
}
i++;
Canvas.SetZIndex(uiOnDrag, i);
bIsDraging = true;
ix = e.GetPosition(canvas ).X;
iy = e.GetPosition(canvas).Y;
} private void Window_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
bIsDraging = false;
} private void Window_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
{
if (!bIsDraging) return;
e.GetPosition(canvas);
object xx = uiOnDrag.GetValue(Canvas.LeftProperty);
object yy = uiOnDrag.GetValue(Canvas.TopProperty);
double x = (double)xx;
double y = (double)yy;
uiOnDrag.SetValue(Canvas.LeftProperty, x + (e.GetPosition(canvas).X - ix));
uiOnDrag.SetValue(Canvas.TopProperty, y + (e.GetPosition(canvas).Y - iy)); ix = e.GetPosition(canvas).X;
iy = e.GetPosition(canvas).Y;
} private void MenuItem_Click_1(object sender, RoutedEventArgs e)
{
canvas.Children.Clear();
} private void TextBlock_MouseDown(object sender, MouseButtonEventArgs e)
{
DragMove();
} private void MenuItem_Click_2(object sender, RoutedEventArgs e)
{
Close();
}
}
}
windows1.xaml<Window x:Class="GetPicWave.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="图片摆放" Width="300" Height=" 300"
MouseLeftButtonUp="Window_MouseLeftButtonUp"
MouseMove="Window_MouseMove"
Icon="app.ico"
Background="AliceBlue"
>
<Window.ContextMenu>
<ContextMenu>
<MenuItem Header="open" Click="MenuItem_Click"/>
<Separator />
<MenuItem Header="Clear" Click="MenuItem_Click_1" />
<Separator />
<MenuItem Header="Close" Click="MenuItem_Click_2" />
</ContextMenu>
</Window.ContextMenu>
<Canvas Name="canvas">
</Canvas>
</Window>windows1.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Forms;
using System.Threading;
using System.ComponentModel;namespace GetPicWave
{
/// <summary>
/// Window1.xaml 的交互逻辑
/// </summary>
public partial class Window1 : Window
{
UIElement uiOnDrag = null ;
bool bIsDraging = false;
double ix, iy; public Window1()
{
InitializeComponent(); }
private void MenuItem_Click(object sender, RoutedEventArgs e)
{ //打开文件
OpenFileDialog openFileDialog1 = new OpenFileDialog(); openFileDialog1.InitialDirectory = "d:\\";
openFileDialog1.Filter = "图片 files (*.jpg)|*.jpg|All files (*.*)|*.*";
openFileDialog1.FilterIndex = 1;
openFileDialog1.RestoreDirectory = true;
openFileDialog1.Multiselect = true; if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
try
{
Random rnd = new Random(100);
Int32 ic = 0;
foreach (string fullname in openFileDialog1.FileNames)
{
Uri url = new Uri(fullname);
BitmapImage bmp = new BitmapImage(url);
Image img = new Image();
img.Source = bmp; img.Width = 400;
img.Height = 300; canvas.Children.Add(img);
ic = rnd.Next(800);
Canvas.SetLeft(img, ic);
ic = rnd.Next(600);
Canvas.SetTop(img, ic);
img.MouseLeftButtonDown += Window_MouseLeftButtonDown;
} }
catch (Exception ex)
{
System.Windows.MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
}
}
}
private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
uiOnDrag = e.Source as UIElement;
// Image tempImg = e.Source as Image;
Int32 i = 1;
foreach (UIElement uiETemp in canvas.Children)
{
if (i < Canvas.GetZIndex(uiETemp)) i = Canvas.GetZIndex(uiETemp);
}
i++;
Canvas.SetZIndex(uiOnDrag, i);
bIsDraging = true;
ix = e.GetPosition(canvas ).X;
iy = e.GetPosition(canvas).Y;
} private void Window_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
bIsDraging = false;
} private void Window_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
{
if (!bIsDraging) return;
e.GetPosition(canvas);
object xx = uiOnDrag.GetValue(Canvas.LeftProperty);
object yy = uiOnDrag.GetValue(Canvas.TopProperty);
double x = (double)xx;
double y = (double)yy;
uiOnDrag.SetValue(Canvas.LeftProperty, x + (e.GetPosition(canvas).X - ix));
uiOnDrag.SetValue(Canvas.TopProperty, y + (e.GetPosition(canvas).Y - iy)); ix = e.GetPosition(canvas).X;
iy = e.GetPosition(canvas).Y;
} private void MenuItem_Click_1(object sender, RoutedEventArgs e)
{
canvas.Children.Clear();
} private void TextBlock_MouseDown(object sender, MouseButtonEventArgs e)
{
DragMove();
} private void MenuItem_Click_2(object sender, RoutedEventArgs e)
{
Close();
}
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货