自定义ChildWindow,完美实现同时弹出多个自定义的窗口
针对Sl中自带的ChildWindow不能同时弹出两个甚至更多,我们开发了自己的ChildWindow,解决类似的障碍。
效果:
xaml:
<UserControl x:Class="SlChindWindow.scSelfControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" Width="592" Height="511" MouseLeftButtonDown="UserControl_MouseLeftButtonDown" >
<Canvas Name="Carrier">
<Canvas Height="43" Name="Head" Width="592" MouseLeftButtonDown="Head_MouseLeftButtonDown" MouseMove="Head_MouseMove" MouseLeftButtonUp="Head_MouseLeftButtonUp">
<Image Canvas.Left="554" Canvas.Top="2" Height="16" Name="btnClose" Width="35" Source="/SlChindWindow;component/Images/close.jpg" MouseLeftButtonDown="btnClose_MouseLeftButtonDown"/>
</Canvas>
<Canvas Height="468" Name="Body" Width="592">
<Image Height="12" Name="Resizer" Width="10" Source="/SlChindWindow;component/Images/resizer.jpg" Canvas.Left="582" Canvas.Top="499" MouseLeftButtonDown="Resizer_MouseLeftButtonDown" MouseLeftButtonUp="Resizer_MouseLeftButtonUp" MouseMove="Resizer_MouseMove" />
</Canvas>
</Canvas>
</UserControl>
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" Width="592" Height="511" MouseLeftButtonDown="UserControl_MouseLeftButtonDown" >
<Canvas Name="Carrier">
<Canvas Height="43" Name="Head" Width="592" MouseLeftButtonDown="Head_MouseLeftButtonDown" MouseMove="Head_MouseMove" MouseLeftButtonUp="Head_MouseLeftButtonUp">
<Image Canvas.Left="554" Canvas.Top="2" Height="16" Name="btnClose" Width="35" Source="/SlChindWindow;component/Images/close.jpg" MouseLeftButtonDown="btnClose_MouseLeftButtonDown"/>
</Canvas>
<Canvas Height="468" Name="Body" Width="592">
<Image Height="12" Name="Resizer" Width="10" Source="/SlChindWindow;component/Images/resizer.jpg" Canvas.Left="582" Canvas.Top="499" MouseLeftButtonDown="Resizer_MouseLeftButtonDown" MouseLeftButtonUp="Resizer_MouseLeftButtonUp" MouseMove="Resizer_MouseMove" />
</Canvas>
</Canvas>
</UserControl>
对应的后台文件:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Imaging;
namespace SlChindWindow
{
public partial class scSelfControl : UserControl
{
bool isMouseCaptured = false;
Point clickPoint = new Point();
public scSelfControl()
{
InitializeComponent();
InitControl();
}
private void InitControl()
{
Image HeadBg = new Image();
HeadBg.Source = new BitmapImage(new Uri(@"/Images/head.jpg", UriKind.Relative));
HeadBg.Width = 592;
HeadBg.Height = 43;
Canvas.SetLeft(HeadBg, 0);
Canvas.SetTop(HeadBg, 0);
Canvas.SetZIndex(HeadBg, -1);
Head.Children.Add(HeadBg);
Image BodyBg = new Image();
BodyBg.Source = new BitmapImage(new Uri(@"/Images/body.jpg", UriKind.Relative));
BodyBg.Width = 592;
BodyBg.Height = 468;
Canvas.SetLeft(BodyBg, 0);
Canvas.SetTop(BodyBg, 43);
Canvas.SetZIndex(BodyBg, -1);
Body.Children.Add(BodyBg);
}
private void Head_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
Head.CaptureMouse();
isMouseCaptured = true;
clickPoint = e.GetPosition(sender as UIElement);
}
private void Head_MouseMove(object sender, MouseEventArgs e)
{
if (isMouseCaptured)
{
if (Application.Current != null && Application.Current.RootVisual != null &&
(e.GetPosition(Application.Current.RootVisual).X < 0 || e.GetPosition(Application.Current.RootVisual).Y < 0))
{
return;//如果超出Silverlight的内容区,则直接返回
}
TransformGroup transformGroup = this.RenderTransform as TransformGroup;
if (transformGroup == null)
{
transformGroup = new TransformGroup();
}
TranslateTransform t = new TranslateTransform()
{
X = e.GetPosition(this).X - this.clickPoint.X,
Y = e.GetPosition(this).Y - this.clickPoint.Y
};
if (transformGroup != null)
{
transformGroup.Children.Add(t);
this.RenderTransform = transformGroup;
}
}
}
private void Head_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
Head.ReleaseMouseCapture();
isMouseCaptured = false;
}
static int z;
private void UserControl_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
z++;
Canvas.SetZIndex(this, z);
}
/// <summary>
/// 关闭
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnClose_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
Canvas Container = this.Parent as Canvas;
Container.Children.Remove(this);
}
private void Resizer_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
Resizer.CaptureMouse();
isMouseCaptured = true;
clickPoint = e.GetPosition(sender as UIElement);
}
private void Resizer_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
Resizer.ReleaseMouseCapture();
isMouseCaptured = false;
}
private void Resizer_MouseMove(object sender, MouseEventArgs e)
{
if (isMouseCaptured)
{
if (Application.Current != null && Application.Current.RootVisual != null &&
(e.GetPosition(Application.Current.RootVisual).X < 0 || e.GetPosition(Application.Current.RootVisual).Y < 0))
{
return;//如果超出Silverlight的内容区,则直接返回
}
TransformGroup transformGroup = this.RenderTransform as TransformGroup;
if (transformGroup == null)
{
transformGroup = new TransformGroup();
}
ScaleTransform t = new ScaleTransform()
{
ScaleX = (e.GetPosition(this).X - this.clickPoint.X)/this.Width,//获得放大或缩小的倍率
ScaleY = (e.GetPosition(this).Y - this.clickPoint.Y) / this.Height
};
if (transformGroup != null)
{
transformGroup.Children.Add(t);
this.RenderTransform = transformGroup;
}
}
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Imaging;
namespace SlChindWindow
{
public partial class scSelfControl : UserControl
{
bool isMouseCaptured = false;
Point clickPoint = new Point();
public scSelfControl()
{
InitializeComponent();
InitControl();
}
private void InitControl()
{
Image HeadBg = new Image();
HeadBg.Source = new BitmapImage(new Uri(@"/Images/head.jpg", UriKind.Relative));
HeadBg.Width = 592;
HeadBg.Height = 43;
Canvas.SetLeft(HeadBg, 0);
Canvas.SetTop(HeadBg, 0);
Canvas.SetZIndex(HeadBg, -1);
Head.Children.Add(HeadBg);
Image BodyBg = new Image();
BodyBg.Source = new BitmapImage(new Uri(@"/Images/body.jpg", UriKind.Relative));
BodyBg.Width = 592;
BodyBg.Height = 468;
Canvas.SetLeft(BodyBg, 0);
Canvas.SetTop(BodyBg, 43);
Canvas.SetZIndex(BodyBg, -1);
Body.Children.Add(BodyBg);
}
private void Head_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
Head.CaptureMouse();
isMouseCaptured = true;
clickPoint = e.GetPosition(sender as UIElement);
}
private void Head_MouseMove(object sender, MouseEventArgs e)
{
if (isMouseCaptured)
{
if (Application.Current != null && Application.Current.RootVisual != null &&
(e.GetPosition(Application.Current.RootVisual).X < 0 || e.GetPosition(Application.Current.RootVisual).Y < 0))
{
return;//如果超出Silverlight的内容区,则直接返回
}
TransformGroup transformGroup = this.RenderTransform as TransformGroup;
if (transformGroup == null)
{
transformGroup = new TransformGroup();
}
TranslateTransform t = new TranslateTransform()
{
X = e.GetPosition(this).X - this.clickPoint.X,
Y = e.GetPosition(this).Y - this.clickPoint.Y
};
if (transformGroup != null)
{
transformGroup.Children.Add(t);
this.RenderTransform = transformGroup;
}
}
}
private void Head_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
Head.ReleaseMouseCapture();
isMouseCaptured = false;
}
static int z;
private void UserControl_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
z++;
Canvas.SetZIndex(this, z);
}
/// <summary>
/// 关闭
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnClose_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
Canvas Container = this.Parent as Canvas;
Container.Children.Remove(this);
}
private void Resizer_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
Resizer.CaptureMouse();
isMouseCaptured = true;
clickPoint = e.GetPosition(sender as UIElement);
}
private void Resizer_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
Resizer.ReleaseMouseCapture();
isMouseCaptured = false;
}
private void Resizer_MouseMove(object sender, MouseEventArgs e)
{
if (isMouseCaptured)
{
if (Application.Current != null && Application.Current.RootVisual != null &&
(e.GetPosition(Application.Current.RootVisual).X < 0 || e.GetPosition(Application.Current.RootVisual).Y < 0))
{
return;//如果超出Silverlight的内容区,则直接返回
}
TransformGroup transformGroup = this.RenderTransform as TransformGroup;
if (transformGroup == null)
{
transformGroup = new TransformGroup();
}
ScaleTransform t = new ScaleTransform()
{
ScaleX = (e.GetPosition(this).X - this.clickPoint.X)/this.Width,//获得放大或缩小的倍率
ScaleY = (e.GetPosition(this).Y - this.clickPoint.Y) / this.Height
};
if (transformGroup != null)
{
transformGroup.Children.Add(t);
this.RenderTransform = transformGroup;
}
}
}
}
}