Blog Content

    티스토리 뷰

    49.SilverLight3 - DragAndDrop

    반응형


    MainPage.xaml

    [그림 49-1]


    <Grid x:Name="LayoutRoot" Background="White">

            <Canvas Width="400" Height="300" Background="Silver">

                <Image x:Name="imgs" Source="Images/j3.jpg"></Image>

            </Canvas>

    </Grid>



    MainPage.xaml.cs

    public partial class MainPage : UserControl

    {

        private bool isMouseDown = false;           //

        private Point lastPoint = new Point();      //

        private Point offset = new Point();         //

     

        public MainPage()

        {

            InitializeComponent();

            //[1]

            this.imgs.MouseLeftButtonDown += new

                          MouseButtonEventHandler(imgs_MouseLeftButtonDown);

               

            //[2] ()

            this.imgs.MouseMove += new MouseEventHandler(imgs_MouseMove);

               

            //[3]

            this.imgs.MouseLeftButtonUp += new

                          MouseButtonEventHandler(imgs_MouseLeftButtonUp);

        }

     

        void imgs_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)

        {

            isMouseDown = false; //

            this.imgs.ReleaseMouseCapture(); //

        }


        void imgs_MouseMove(object sender, MouseEventArgs e)

        {

            if (isMouseDown) // ???

            {

                lastPoint = e.GetPosition(null); //

               

                //TextBlock Left Top

                //-TextBlock

                imgs.SetValue(Canvas.LeftProperty, lastPoint.X - offset.X);

                    imgs.SetValue(Canvas.TopProperty, lastPoint.Y - offset.Y);

            }

        }

     

        void imgs_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)

        {

            isMouseDown = true; //

            this.imgs.CaptureMouse(); //

            lastPoint = e.GetPosition(null); // Point

               

            //MessageBox.Show(lastPoint.X.ToString());

            offset.X = lastPoint.X - Convert.ToDouble(imgs.GetValue(Canvas.LeftProperty));

            offset.Y = lastPoint.Y - Convert.ToDouble(imgs.GetValue(Canvas.TopProperty));

        }

    }


    결과화면

    [그림 49-2]


    사진속 주인공 : 배우 조선옥


    반응형

    'Silverlight' 카테고리의 다른 글

    51.SilverLight3 - RoutedEventArgs  (0) 2009.12.01
    50.SilverLight3 - InkPresenter  (0) 2009.12.01
    48.SilverLight3 - TextBlock  (0) 2009.12.01
    47.SilverLight3 - PasswordBox  (0) 2009.12.01
    46.SilverLight3 - TextBox  (0) 2009.12.01

    Comments