c#中拖動圖片的例子

來源:互聯網
上載者:User

這個問題來自論壇提問,並沒有什麼難度,也不需要重畫內容。當然還有一種方法是通過api發送WM_SysCommand 和SC_MOVE,也就是拖動無標題表單的方法 ,但是效果沒有這個好。

using System;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsApplication2
...{
  public partial class Form1 : Form
  ...{
    static string strDown = @"AAACAAEAICAAAAsACQAwAQAAFgAAACgAAAAgAAAAQAAAAAEAAQAAAAAAAAIAAAAA
            AAAAAAAAAAAAAAAAAAAAAAAA////AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
            AAAAAAAAAAf4AAAD8AAAA/AAAAPwAAAH+AAAD/gAAB/8AAA//AAAN/wAACf+AAAH
            9gAADbQAAA2wAAAJsAAAAIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
            AAAAAAAA////////////////////////////////////////////8AP///gH///4
            B///+Af///AD///gA///wAH//4AB//+AAf//gAD//4AA///AAP//4AH//+AH///g
            D////j////////////////////////////////////////////8=";
    static string strUp = @"AAACAAEAICAAAAoACAAwAQAAFgAAACgAAAAgAAAAQAAAAAEAAQAAAAAAAAIAAA
            AAAAAAAAAAAAAAAAAAAAAAAAAA////AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
            AAAAAAAAAAAAAf4AAAD8AAAA/AAAAPwAAAH+AAAD/gAAB/8AAA//AAAd/wAAGf+
            AAAH9gAADbYAAA2yAAAZsAAAGbAAAAGAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
            AAAAAAAAAAAAA////////////////////////////////////////////8AP///
            gH///4B///+Af///AD///gA///wAH//4AB//8AAf//AAD//4AA///gAP//4AD//
            8AF///AB///5A////5///////////////////////////////////////8=";

    Cursor curUp = new Cursor(new System.IO.MemoryStream(Convert.FromBase64String(strUp)));
    Cursor curDown = new Cursor(new System.IO.MemoryStream(Convert.FromBase64String(strDown)));
    public Form1()
    ...{
      InitializeComponent();
      this.pictureBox1.Cursor = curUp;
    }

    bool bDragging = false;
    Point pClicked;

    private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
    ...{

      bDragging = true;
      pClicked = new Point(e.X, e.Y);
      this.pictureBox1.Cursor = curDown;
    }

    private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
    ...{
      if (bDragging)
      ...{
        Point oMoveToPoint;
        oMoveToPoint = this.PointToClient(pictureBox1.PointToScreen(new Point(e.X, e.Y)));
        oMoveToPoint.Offset(pClicked.X * -1, pClicked.Y * -1);
        pictureBox1.Location = oMoveToPoint;
      }
    }
    private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
    ...{
      bDragging = false;
      this.pictureBox1.Cursor = curUp;
    }
  }
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.