熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

C#實現窗體和任意控件的拖動

2022-06-13   來源: .NET編程 

  程序內有大部分注釋了歡迎大家指正了!

  Code

  [copy to clipboard]

  CODE:

   using System;

   using SystemDrawing;

   using SystemWindowsForms;

  

   namespace YokerFormUtils

  {

    /**//// <summary>

    /// <para>說明窗體拖動類通過這個類提供的方法實現窗體上任意控件可輔助拖動窗體</para>

    /// <para>作者YokerWu</para>

    /// <para>原創地址[url][/url]</para>

    /// </summary>

    public class dragFormClass

    {

      private static bool isMouseDown = false;

      private static Point mouseOffset;

      private static Form _form;

      public dragFormClass() { }

  

      /**//// <summary>

      /// 在窗體上增加拖拽事件

      /// </summary>

      /// <param name=control>控件對象</param>

      public static void bindControl(Control control)

      {

        //如果控件為空

        if (control == null)

        {

          return;

        }

        _form = controlFindForm();

        //增加鼠標拖動窗體移動事件

        controlMouseMove += new MouseEventHandler(control_MouseMove);

        controlMouseDown += new MouseEventHandler(control_MouseDown);

        controlMouseUp += new MouseEventHandler(control_MouseUp);

      }

      /**//// <summary>

      /// 鼠標按下之時保存鼠標相對於窗體的位置

      /// </summary>

      /// <param name=sender></param>

      /// <param name=e></param>

      private static void control_MouseDown(object sender MouseEventArgs e)

      {

        if (eButton == MouseButtonsLeft)

        {

          Control control = sender as Control;

          int offsetX = eX;

          int offsetY = eY;

          //判斷是窗體還是控件從而改進鼠標相對於窗體的位置

          if (!(control is SystemWindowsFormsForm))

          {

            offsetX = offsetX controlLeft;

            offsetY = offsetY controlTop;

          }

          //判斷窗體有沒有標題欄從而改進鼠標相對於窗體的位置

          if (_formFormBorderStyle != FormBorderStyleNone)

          {

            offsetX = offsetX SystemInformationFrameBorderSizeWidth;

            offsetY = offsetY SystemInformationFrameBorderSizeHeight SystemInformationCaptionHeight;

          }

          mouseOffset = new Point(offsetX offsetY);

          isMouseDown = true;

        }

      }

      /**//// <summary>

      /// 移動鼠標的時候改變窗體位置

      /// </summary>

      /// <param name=sender></param>

      /// <param name=e></param>

      private static void control_MouseMove(object sender MouseEventArgs e)

      {

        if (isMouseDown)

        {

          Point mouse = ControlMousePosition;

          mouseOffset(mouseOffsetX mouseOffsetY);

          _formLocation = mouse;

        }

      }

      /**//// <summary>

      /// 松開鼠標的時候重設事件

      /// </summary>

      /// <param name=sender></param>

      /// <param name=e></param>

      private static void control_MouseUp(object sender MouseEventArgs e)

      {

        if (eButton == MouseButtonsLeft)

        {

          isMouseDown = false;

        }

      }

    }

  }

  


From:http://tw.wingwit.com/Article/program/net/201311/13302.html
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.