程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C# 實現任意控件的隨意拖動

C# 實現任意控件的隨意拖動

編輯:C#入門知識

目的實現窗體裡面的所有控件可以隨意拖動 C#布局代碼如下:  

<Window x:Class="DragLabel.MainWindow"  
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
        Title="MainWindow" Height="350" Width="525" MouseMove="Window_MouseMove" MouseUp="Window_MouseUp">  
    <Grid>  
        <Canvas Height="227" HorizontalAlignment="Left" Margin="20,31,0,0" Name="canvas1" VerticalAlignment="Top" Width="447">  
            <Label Canvas.Left="84" Canvas.Top="120" Content="Label" Height="28" Name="label1" MouseDown="label1_MouseDown" />  
            <StackPanel Canvas.Left="242" Canvas.Top="96" Height="100" Name="stackPanel1" Width="142" MouseDown="stackPanel1_MouseDown">  
                <Label Content="Label" Height="28" Name="label2" />  
                <Label Content="Label" Height="28" Name="label3" />  
            </StackPanel>  
        </Canvas>  
    </Grid>  
</Window>  

 

  C#實現代碼如下:  
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;  
  
namespace DragLabel  
{  
    /// <summary>  
    /// MainWindow.xaml 的交互邏輯  
    /// </summary>  
    public partial class MainWindow : Window  
    {  
        bool IsMouseDown = false;  
        Point mousePoint;  
        object mouseCtrl = null;  
        public MainWindow()  
        {  
            InitializeComponent();  
        }  
  
        private void Window_MouseMove(object sender, MouseEventArgs e)  
        {  
            if (IsMouseDown)  
            {  
                if (e.LeftButton == MouseButtonState.Pressed)  
                {  
                    Point theMousePoint = e.GetPosition(this.canvas1);  
                    Canvas.SetLeft((UIElement)mouseCtrl, theMousePoint.X - (mousePoint.X - Canvas.GetLeft(((UIElement)mouseCtrl))));  
                    Canvas.SetTop((UIElement)mouseCtrl, theMousePoint.Y - (mousePoint.Y - Canvas.GetTop(((UIElement)mouseCtrl))));  
                    mousePoint = theMousePoint;  
                }  
            }  
        }  
  
        private void Window_MouseUp(object sender, MouseButtonEventArgs e)  
        {  
            if (IsMouseDown)  
            {  
                IsMouseDown = false;  
            }    
        }  
  
        private void label1_MouseDown(object sender, MouseButtonEventArgs e)  
        {  
            if (e.LeftButton == MouseButtonState.Pressed)  
            {  
                IsMouseDown = true;  
                mousePoint = e.GetPosition(this.canvas1);  
                mouseCtrl = sender;  
            }         
        }  
  
        private void stackPanel1_MouseDown(object sender, MouseButtonEventArgs e)  
        {  
            if (e.LeftButton == MouseButtonState.Pressed)  
            {  
                IsMouseDown = true;  
                mousePoint = e.GetPosition(this.canvas1);  
                mouseCtrl = sender;  
            }  
        }  
    }  
}  

 

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved