<legend id='hlzG4'><style id='hlzG4'><dir id='hlzG4'><q id='hlzG4'></q></dir></style></legend>

        <bdo id='hlzG4'></bdo><ul id='hlzG4'></ul>

      <small id='hlzG4'></small><noframes id='hlzG4'>

    1. <tfoot id='hlzG4'></tfoot>

        <i id='hlzG4'><tr id='hlzG4'><dt id='hlzG4'><q id='hlzG4'><span id='hlzG4'><b id='hlzG4'><form id='hlzG4'><ins id='hlzG4'></ins><ul id='hlzG4'></ul><sub id='hlzG4'></sub></form><legend id='hlzG4'></legend><bdo id='hlzG4'><pre id='hlzG4'><center id='hlzG4'></center></pre></bdo></b><th id='hlzG4'></th></span></q></dt></tr></i><div id='hlzG4'><tfoot id='hlzG4'></tfoot><dl id='hlzG4'><fieldset id='hlzG4'></fieldset></dl></div>

        运行时在窗体上拖动控件

        时间:2023-08-25
      1. <tfoot id='NXpND'></tfoot>
      2. <i id='NXpND'><tr id='NXpND'><dt id='NXpND'><q id='NXpND'><span id='NXpND'><b id='NXpND'><form id='NXpND'><ins id='NXpND'></ins><ul id='NXpND'></ul><sub id='NXpND'></sub></form><legend id='NXpND'></legend><bdo id='NXpND'><pre id='NXpND'><center id='NXpND'></center></pre></bdo></b><th id='NXpND'></th></span></q></dt></tr></i><div id='NXpND'><tfoot id='NXpND'></tfoot><dl id='NXpND'><fieldset id='NXpND'></fieldset></dl></div>
          <tbody id='NXpND'></tbody>
          <bdo id='NXpND'></bdo><ul id='NXpND'></ul>

          • <legend id='NXpND'><style id='NXpND'><dir id='NXpND'><q id='NXpND'></q></dir></style></legend>

            <small id='NXpND'></small><noframes id='NXpND'>

                  本文介绍了运行时在窗体上拖动控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我刚刚开始使用 WPF.但我正在尝试添加我的代码(来自 Winforms),使用户能够在运行时将任何控件拖动到他们希望的任何位置.但我似乎无法获得鼠标的当前位置......嗯?鼠标没有位置?:(

                  I've just started using WPF. But I'm trying to add my code that (from Winforms) enables the user to drag any control whereever they wish at runtime. But I can't seem to get the current Location of the mouse... Eh? There is no Location for Mouse? :(

                  推荐答案

                  在鼠标事件中可以使用 e.GetPosition 获取当前鼠标位置.此函数可以提供相对于特定元素的鼠标位置,或者您可以只传递 null.

                  In the Mouse event you can use e.GetPosition to get the current mouse position. This function can provide the mouse position relative to a specific element or you can just pass null.

                  这是一个非常简单的例子,没有命中测试或任何东西,只是一个可以拖动的按钮.我使用了一个画布来保持它的简短,但您可能会更好地使用转换并将控件转换到所需的位置.

                  Here is a very simple example, no hit testing or anything, just a button that you can drag around. And I used a canvas to keep it short, but you would probably do better to use a transform and translate the control to the desired position.

                  <Window x:Class="WpfApplication1.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">
                      <Canvas PreviewMouseLeftButtonDown="Canvas_PreviewMouseLeftButtonDown" 
                              PreviewMouseMove="Canvas_PreviewMouseMove" 
                              PreviewMouseLeftButtonUp="Canvas_PreviewMouseLeftButtonUp">
                          <Button Name="dragButton" Width="80" Height="21" Canvas.Left="50" Canvas.Top="10">Drag Me!</Button>
                      </Canvas>
                  </Window>
                  


                  using System;
                  using System.Windows;
                  using System.Windows.Controls;
                  using System.Windows.Input;
                  
                  namespace WpfApplication1
                  {
                    public partial class MainWindow : Window
                    {
                      public MainWindow()
                      {
                        InitializeComponent();      
                      }    
                  
                      private Point _startPoint;
                      private bool _dragging = false;
                  
                      private void Canvas_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
                      {         
                        if (dragButton.CaptureMouse())
                        {        
                          _startPoint = e.GetPosition(null);
                          _dragging = true;        
                        }
                      }
                  
                      private void Canvas_PreviewMouseMove(object sender, MouseEventArgs e)
                      {
                        if (_dragging)
                        {        
                          Point newPoint = e.GetPosition(null);
                          double dx = newPoint.X - _startPoint.X;
                          double dy = newPoint.Y - _startPoint.Y;
                  
                          Canvas.SetLeft(dragButton, Canvas.GetLeft(dragButton) + dx);
                          Canvas.SetTop(dragButton, Canvas.GetTop(dragButton) + dy);
                          _startPoint = newPoint;
                        }
                      }
                  
                      private void Canvas_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
                      {
                        if (_dragging)
                        {        
                          _dragging = false;
                          dragButton.ReleaseMouseCapture();
                        }
                      }    
                    }
                  }
                  

                  这篇关于运行时在窗体上拖动控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:C# 在 FlowLayoutPanels 中拖放标签 下一篇:来自另一个线程的 DoDragDrop()

                  相关文章

                  最新文章

                  • <bdo id='C4VTJ'></bdo><ul id='C4VTJ'></ul>
                  <i id='C4VTJ'><tr id='C4VTJ'><dt id='C4VTJ'><q id='C4VTJ'><span id='C4VTJ'><b id='C4VTJ'><form id='C4VTJ'><ins id='C4VTJ'></ins><ul id='C4VTJ'></ul><sub id='C4VTJ'></sub></form><legend id='C4VTJ'></legend><bdo id='C4VTJ'><pre id='C4VTJ'><center id='C4VTJ'></center></pre></bdo></b><th id='C4VTJ'></th></span></q></dt></tr></i><div id='C4VTJ'><tfoot id='C4VTJ'></tfoot><dl id='C4VTJ'><fieldset id='C4VTJ'></fieldset></dl></div>

                  <small id='C4VTJ'></small><noframes id='C4VTJ'>

                  1. <tfoot id='C4VTJ'></tfoot>

                    <legend id='C4VTJ'><style id='C4VTJ'><dir id='C4VTJ'><q id='C4VTJ'></q></dir></style></legend>