怎么在C#中利用WinForm實(shí)現(xiàn)窗體上控件自由拖動(dòng)功能-創(chuàng)新互聯(lián)

這篇文章將為大家詳細(xì)講解有關(guān)怎么在C#中利用WinForm實(shí)現(xiàn)窗體上控件自由拖動(dòng)功能,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。

永仁網(wǎng)站建設(shè)公司成都創(chuàng)新互聯(lián)公司,永仁網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為永仁上千提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\成都外貿(mào)網(wǎng)站建設(shè)要多少錢,請(qǐng)找那個(gè)售后服務(wù)好的永仁做網(wǎng)站的公司定做!

具體如下:

首先在窗體上放一個(gè)PictrueBox控件,命名為pb1,拖動(dòng)完整代碼如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WinFormDrag
{
  public partial class Form1 : Form
  {
    //鼠標(biāo)按下坐標(biāo)(control控件的相對(duì)坐標(biāo))
    Point mouseDownPoint = Point.Empty;
    //顯示拖動(dòng)效果的矩形
    Rectangle rect = Rectangle.Empty;
    //是否正在拖拽
    bool isDrag = false;
    public Form1()
    {
      InitializeComponent();
    }
    private void Form1_Paint(object sender, PaintEventArgs e)
    {
      if (rect != Rectangle.Empty)
      {
        if (isDrag)
        {//畫一個(gè)和Control一樣大小的黑框
          e.Graphics.DrawRectangle(Pens.Black, rect);
        }
        else
        {
          e.Graphics.DrawRectangle(new Pen(this.BackColor), rect);
        }
      }
    }
    /// <summary>
    /// 按下鼠標(biāo)時(shí)
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void pb1_MouseDown(object sender, MouseEventArgs e)
    {
      if (e.Button == MouseButtons.Left)
      {
        mouseDownPoint = e.Location;
        //記錄控件的大小
        rect = pb1.Bounds;
      }
    }
    /// <summary>
    /// 移過時(shí)
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void pb1_MouseMove(object sender, MouseEventArgs e)
    {
      if (e.Button == MouseButtons.Left)
      {
        isDrag = true;
        //重新設(shè)置rect的位置,跟隨鼠標(biāo)移動(dòng)
        rect.Location = getPointToForm(new Point(e.Location.X - mouseDownPoint.X, e.Location.Y - mouseDownPoint.Y));
        this.Refresh();
      }
    }
    /// <summary>
    /// 釋放鼠標(biāo)按鈕時(shí)
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void pb1_MouseUp(object sender, MouseEventArgs e)
    {
      if (e.Button == MouseButtons.Left)
      {
        if (isDrag)
        {
          isDrag = false;
          //移動(dòng)control到放開鼠標(biāo)的地方
          pb1.Location = rect.Location;
          this.Refresh();
        }
        reset();
      }
    }
    //重置變量
    private void reset()
    {
      mouseDownPoint = Point.Empty;
      rect = Rectangle.Empty;
      isDrag = false;
    }
    //把相對(duì)與control控件的坐標(biāo),轉(zhuǎn)換成相對(duì)于窗體的坐標(biāo)。
    private Point getPointToForm(Point p)
    {
      return this.PointToClient(pb1.PointToScreen(p));
    }
  }
}

關(guān)于怎么在C#中利用WinForm實(shí)現(xiàn)窗體上控件自由拖動(dòng)功能就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。

名稱欄目:怎么在C#中利用WinForm實(shí)現(xiàn)窗體上控件自由拖動(dòng)功能-創(chuàng)新互聯(lián)
鏈接URL:http://muchs.cn/article28/dschjp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供域名注冊(cè)、網(wǎng)站建設(shè)建站公司、手機(jī)網(wǎng)站建設(shè)、營銷型網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)公司

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站建設(shè)