2012-04-12 70 views
1

我用多個標籤創建了簡單的UserControl。我如何實現簡單的機制,允許像普通窗口一樣移動整個控件(當我將它添加到winForms - 如果有區別)WPF中的可移動用戶控件

+0

返回位置,更新用戶控件位置,鼠標的位置? – Amicable 2012-04-12 08:49:38

回答

2

你可以用我的採集等級:

public class ClsCapture 
{ 
    bool bCaptureMe; 
    Point pLocation = new Point(); 

    Control dd; 
    //Handles dad.MouseDown, dd.MouseDown 
    private void Form1_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e) 
    { 
     try { 
      bCaptureMe = true; 
      pLocation = e.GetPosition(sender); 
     } catch { 
     } 
    } 

    //Handles dad.MouseMove, dd.MouseMove 
    private void Form1_MouseMove(object sender, System.Windows.Input.MouseEventArgs e) 
    { 
     try { 

      if (bCaptureMe) { 
       dd.Margin = new Thickness(dd.Margin.Left - pLocation.X + e.GetPosition(sender).X, dd.Margin.Top - pLocation.Y + e.GetPosition(sender).Y, dd.Margin.Right, dd.Margin.Bottom); 

      } 
     } catch { 
     } 
    } 

    //Handles dad.MouseUp, dd.MouseUp 
    private void Form1_MouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e) 
    { 
     try { 
      bCaptureMe = false; 
     } catch { 
     } 
    } 

    public ClsCapture(Control pnl) 
    { 
     dd = pnl; 
     dd.PreviewMouseLeftButtonDown += Form1_MouseDown; 
     dd.PreviewMouseLeftButtonUp += Form1_MouseUp; 
     dd.PreviewMouseMove += Form1_MouseMove; 
    } 

    public static void CaptureMe(Control pnl) 
    { 
     ClsCapture cc = new ClsCapture(pnl); 
    } 

} 

使用方法:鼠標

ClsCapture.CaptureMe(AnyControlYouWant); 
+0

我無法獲取System.Windows.Input.MouseButtonEventArgs – Raghurocks 2013-10-30 05:01:50