自訂ChildWindow,完美實現同時彈出多個自訂的視窗

來源:互聯網
上載者:User
針對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>

對應的後台檔案:

 

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;
                }

                
            }
        }
    }
}

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.