這篇文章將為大家詳細講解有關(guān)怎么在C#中利用WinForm實現(xiàn)窗體上控件自由拖動功能,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。
永仁網(wǎng)站建設(shè)公司成都創(chuàng)新互聯(lián)公司,永仁網(wǎng)站設(shè)計制作,有大型網(wǎng)站制作公司豐富經(jīng)驗。已為永仁上千提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\成都外貿(mào)網(wǎng)站建設(shè)要多少錢,請找那個售后服務(wù)好的永仁做網(wǎng)站的公司定做!具體如下:
首先在窗體上放一個PictrueBox控件,命名為pb1,拖動完整代碼如下:
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控件的相對坐標(biāo)) Point mouseDownPoint = Point.Empty; //顯示拖動效果的矩形 Rectangle rect = Rectangle.Empty; //是否正在拖拽 bool isDrag = false; public Form1() { InitializeComponent(); } private void Form1_Paint(object sender, PaintEventArgs e) { if (rect != Rectangle.Empty) { if (isDrag) {//畫一個和Control一樣大小的黑框 e.Graphics.DrawRectangle(Pens.Black, rect); } else { e.Graphics.DrawRectangle(new Pen(this.BackColor), rect); } } } /// <summary> /// 按下鼠標(biāo)時 /// </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> /// 移過時 /// </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)移動 rect.Location = getPointToForm(new Point(e.Location.X - mouseDownPoint.X, e.Location.Y - mouseDownPoint.Y)); this.Refresh(); } } /// <summary> /// 釋放鼠標(biāo)按鈕時 /// </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; //移動control到放開鼠標(biāo)的地方 pb1.Location = rect.Location; this.Refresh(); } reset(); } } //重置變量 private void reset() { mouseDownPoint = Point.Empty; rect = Rectangle.Empty; isDrag = false; } //把相對與control控件的坐標(biāo),轉(zhuǎn)換成相對于窗體的坐標(biāo)。 private Point getPointToForm(Point p) { return this.PointToClient(pb1.PointToScreen(p)); } } }
關(guān)于怎么在C#中利用WinForm實現(xiàn)窗體上控件自由拖動功能就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
名稱欄目:怎么在C#中利用WinForm實現(xiàn)窗體上控件自由拖動功能-創(chuàng)新互聯(lián)
鏈接URL:http://bm7419.com/article28/dschjp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供域名注冊、網(wǎng)站建設(shè)、建站公司、手機網(wǎng)站建設(shè)、營銷型網(wǎng)站建設(shè)、網(wǎng)站設(shè)計公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容