<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # 拖放 > 原文: [http://zetcode.com/gui/vbwinforms/dragdrop/](http://zetcode.com/gui/vbwinforms/dragdrop/) Mono Visual Basic Winforms 教程的這一部分將專門用于拖放操作。 在計算機圖形用戶界面中,拖放是單擊虛擬對象并將其拖動到其他位置或另一個虛擬對象上的動作(或支持以下動作)。 通常,它可用于調用多種動作,或在兩個抽象對象之間創建各種類型的關聯。 (維基百科) 拖放功能是圖形用戶界面最明顯的方面之一。 拖放操作使您可以直觀地完成復雜的事情。 ## 拖動按鈕 在第一個示例中,我們將在按鈕控件上執行拖放操作。 該示例在拖放&放置協議之外執行作業。 ```vb ' ZetCode Mono Visual Basic Winforms tutorial ' ' In this program we drag & drop ' a button control ' ' author jan bodnar ' last modified May 2009 ' website www.zetcode.com Imports System.Windows.Forms Imports System.Drawing Public Class WinVBApp Inherits Form Private isDragging As Boolean = False Private oldX As Integer Private oldY As Integer Private button As Button Public Sub New Me.Text = "Drag & Drop button" Me.Size = New Size(270, 180) Me.InitUI Me.CenterToScreen End Sub Private Sub InitUI button = New Button button.Parent = Me button.Cursor = Cursors.Hand button.Text = "Button" button.Location = New Point(20, 20) AddHandler button.MouseDown, AddressOf Me.OnMouseDown AddHandler button.MouseUp, AddressOf Me.OnMouseUp AddHandler button.MouseMove, AddressOf Me.OnMouseMove End Sub Private Sub OnMouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) isDragging = True oldX = e.X oldY = e.Y End Sub Private Sub OnMouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) If isDragging button.Top = button.Top + (e.Y - oldY) button.Left = button.Left + (e.X - oldX) End If End Sub Private Sub OnMouseUp(ByVal sender As Object, ByVal e As MouseEventArgs) isDragging = False End Sub Public Shared Sub Main Application.Run(New WinVBApp) End Sub End Class ``` 該代碼示例將一個常規按鈕控件放在表單容器上。 通過單擊按鈕表面并同時用鼠標拖動它,我們可以重新放置按鈕。 我們的示例中有一些支持變量。 `isDragging`變量告訴我們是否正在拖動對象。 `oldX`和`oldY`變量在拖動過程開始之前存儲 x,y 坐標。 ```vb AddHandler button.MouseDown, AddressOf Me.OnMouseDown AddHandler button.MouseUp, AddressOf Me.OnMouseUp AddHandler button.MouseMove, AddressOf Me.OnMouseMove ``` 我們為按鈕插入了三種不同的鼠標處理器。 它們實現了拖放過程的三個不同階段。 當我們單擊按鈕時,過程開始。 這由`OnMouseDown`方法處理。 第二部分是機芯。 這是當我們將對象移動到新位置時。 它以`OnMouseMove`方法處理。 最后一部分是過程停止的時間。 當我們釋放鼠標按鈕時會發生這種情況。 適當的任務委托給`OnMouseUp`方法。 ```vb Private Sub OnMouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) isDragging = True oldX = e.X oldY = e.Y End Sub ``` `OnMouseDown`方法實現了過程的第一部分。 它設置了三個必要的變量。 ```vb Private Sub OnMouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) If isDragging button.Top = button.Top + (e.Y - oldY) button.Left = button.Left + (e.X - oldX) End If End Sub ``` 在`OnMouseMove`方法中,我們重新定位按鈕。 我們計算存儲的 x,y 坐標與鼠標指針的新坐標之間的差。 差異將添加到按鈕的`Top`和`Left`屬性中,從而將其移動到新位置。 ![Dragging a button](https://img.kancloud.cn/e4/a8/e4a88e1f844b761b6b2ddd84bdf07a9d_270x181.jpg) 圖:拖動按鈕 ## 拖動文字 在前面的示例中,我們確實拖動了控件上的&拖放。 接下來,我們將對文本數據進行拖放操作。 在這里,我們將使用 Winforms 庫提供的拖放協議。 拖放操作是 Winforms 中的標準通信協議。 我們有兩個基本對象。 `drag source`和`drop target`。 ```vb ' ZetCode Mono Visual Basic Winforms tutorial ' ' In this program we drag & drop ' text ' ' author jan bodnar ' last modified May 2009 ' website www.zetcode.com Imports System.Windows.Forms Imports System.Drawing Public Class WinVBApp Inherits Form Private txtBox As TextBox Private btn As Button Public Sub New Me.Text = "Drag & Drop text" Me.Size = New Size(250, 200) Me.InitUI Me.CenterToScreen End Sub Private Sub InitUI btn = New Button txtBox = New TextBox Me.SuspendLayout btn.AllowDrop = True btn.Location = New Point(150, 50) txtBox.Location = New Point(15, 50) Me.Controls.Add(btn) Me.Controls.Add(txtBox) Me.ResumeLayout AddHandler btn.DragEnter, AddressOf Me.OnDragEnter AddHandler btn.DragDrop, AddressOf Me.OnDragDrop AddHandler txtBox.MouseDown, AddressOf Me.OnMouseDown End Sub Private Sub OnDragEnter(ByVal sender As Object, ByVal e As DragEventArgs) e.Effect = DragDropEffects.Copy End Sub Private Sub OnDragDrop(ByVal sender As Object, ByVal e As DragEventArgs) sender.Text = e.Data.GetData(DataFormats.Text) End Sub Private Sub OnMouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) sender.DoDragDrop(sender.Text, DragDropEffects.Copy) End Sub Public Shared Sub Main Application.Run(New WinVBApp) End Sub End Class ``` 我們在表單上有兩個控件。 一個按鈕和一個文本框。 我們將文本從文本框中拖放到按鈕上。 ```vb btn.AllowDrop = True ``` 我們將`AllowDrop`屬性設置為`true`。 默認情況下不啟用刪除。 ```vb AddHandler btn.DragEnter, AddressOf Me.OnDragEnter AddHandler btn.DragDrop, AddressOf Me.OnDragDrop AddHandler txtBox.MouseDown, AddressOf Me.OnMouseDown ``` 同樣,拖放過程分為三個步驟。 對于每個特定步驟,我們有三種方法。 ```vb Private Sub OnDragEnter(ByVal sender As Object, ByVal e As DragEventArgs) e.Effect = DragDropEffects.Copy End Sub ``` 當鼠標指針進入放置目標控件的區域時,將啟動`DragEnter`事件。 必須設置`Effect`屬性。 拖動源和放置目標的`DragDropEffects`必須相等。 否則,該操作將無法進行。 ```vb Private Sub OnDragDrop(ByVal sender As Object, ByVal e As DragEventArgs) sender.Text = e.Data.GetData(DataFormats.Text) End Sub ``` 最后,我們有`OnDragDrop`方法。 在這里,我們從事件對象獲取數據并將其設置為按鈕`Text`屬性。 ```vb Private Sub OnMouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) sender.DoDragDrop(sender.Text, DragDropEffects.Copy) End Sub ``` 在`OnMouseDown`方法中,我們初始化了拖放過程。 我們使用`DoDragDrop`方法啟動該過程。 `DragDropEffects.Copy`參數指定操作的類型。 實質上,我們可以在拖放操作期間復制文本或移動文本。 ![Drag & drop of text](https://img.kancloud.cn/a1/c5/a1c5d8141e21152bfb8432d9d1fb4da4_250x201.jpg) 圖:文本拖放 ## 拖動圖像 在最后一個示例中,我們將&拖放圖像拖到窗體上。 ```vb ' ZetCode Mono Visual Basic Winforms tutorial ' ' In this program we drag & drop ' an image ' ' author jan bodnar ' last modified May 2009 ' website www.zetcode.com Imports System.Windows.Forms Imports System.Drawing Public Class WinVBApp Inherits Form Private IsDragging As Boolean = False Private oldX As Integer Private oldY As Integer Private dropRect As Rectangle Private picBox As PictureBox Private image As Bitmap Private brsh As Brush Public Sub New Me.Text = "Drag & Drop image" Me.Size = New Size(350, 250) Me.InitUI Me.CenterToScreen End Sub Private Sub InitUI isDragging = False dropRect = New Rectangle(10, 10, 200, 160) brsh = Brushes.Gray picBox = New PictureBox Me.LoadImage picBox.Parent = Me picBox.Location = New Point(100, 50) picBox.Size = New Size(image.Width, image.Height) picBox.Image = image picBox.Cursor = Cursors.Hand AddHandler Me.Paint, AddressOf Me.OnPaint AddHandler picBox.MouseDown, AddressOf Me.OnMouseDown AddHandler picBox.MouseMove, AddressOf Me.OnMouseMove AddHandler picBox.MouseUp, AddressOf Me.OnMouseUp End Sub Private Sub LoadImage Try image = New Bitmap("image.jpg") Catch Console.WriteLine("Error reading image") Environment.Exit(1) End Try End Sub Private Sub OnMouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) isDragging = True oldX = e.X oldY = e.Y End Sub Private Sub OnMouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) If isDragging picBox.Top = picBox.Top + (e.Y - oldY) picBox.Left = picBox.Left + (e.X - oldX) End If End Sub Private Sub OnMouseUp(ByVal sender As Object, ByVal e As MouseEventArgs) isDragging = False If dropRect.Contains(picBox.Bounds) brsh = Brushes.Gold Else brsh = Brushes.Gray End If Me.Refresh End Sub Private Sub OnPaint(ByVal sender As Object, ByVal e As PaintEventArgs) Dim g As Graphics = e.Graphics g.FillRectangle(brsh, dropRect) End Sub Public Shared Sub Main Application.Run(New WinVBApp) End Sub End Class ``` 在我們的示例中,我們有一個`PictureBox`,并繪制了一個灰色矩形。 如果將圖片放在矩形內,則矩形的顏色變為金色。 ```vb brsh = Brushes.Gray ``` `brsh`變量保存矩形的筆刷。 默認情況下為灰色。 ```vb Private Sub LoadImage Try image = New Bitmap("image.jpg") Catch Console.WriteLine("Error reading image") Environment.Exit(1) End Try End Sub ``` `LoadImage`方法為`PictureBox`控件加載位圖。 ```vb If dropRect.Contains(picBox.Bounds) brsh = Brushes.Gold Else brsh = Brushes.Gray End If ``` 在`OnMouseUp`方法中,我們確定矩形的筆刷。 如果圖片框的邊界在矩形內,則畫筆為金色;否則,畫筆為金色。 否則為灰色。 ```vb Me.Refresh ``` 我們必須調用`Refresh`方法來激活新的畫筆顏色。 ![Drag & drop image](https://img.kancloud.cn/77/30/77301aa71b352b8164f7bec2bf7d2dfd_350x251.jpg) 圖:拖放圖像 本章致力于使用帶有 Visual Basic 語言的 Mono Winforms 庫拖放操作。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看