VB.net フォームをなめらかに移動する方法

このQ&Aのポイント
  • フォームをなめらかに移動する方法を教えてください
  • Locationを使用して移動すると、カクカクと動いてしまいます
  • どのようにすればスムーズな移動ができるのでしょうか
回答を見る
  • ベストアンサー

VB.net フォームをなめらかに移動する方法

フォームをなめらかに移動する方法はないでしょうか? 以下のコードのように、Locationで位置を変更すると、カクカク移動してしまいます。 ご存知の方おしえてください。どうぞよろしくお願いいたします。 Private Sub Form1_Mousemove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove Dim WorkingArea As Rectangle = Screen.PrimaryScreen.WorkingArea Dim MousePos As Point = Me.PointToClient(Windows.Forms.Cursor.Position) '上に移動 If Me.Size.Height * 0.8 < MousePos.Y And Me.Location.Y > 0 Then Me.Location = New Point(Me.Location.X, Me.Location.Y - 20) End If '下に移動 If Me.Size.Height * 0.2 > MousePos.Y And (Me.Location.Y + Me.Size.Height) < WorkingArea.Height Then Me.Location = New Point(Me.Location.X, Me.Location.Y + 20) End If '左に移動 If Me.Size.Width * 0.8 < MousePos.X And Me.Location.X > 0 Then Me.Location = New Point(Me.Location.X - 20, Me.Location.Y) End If '右に移動 If Me.Size.Width * 0.2 > MousePos.X And (Me.Location.X + Me.Size.Width) < WorkingArea.Width Then Me.Location = New Point(Me.Location.X + 20, Me.Location.Y) End If End Sub

  • ddtqp
  • お礼率68% (139/203)

質問者が選んだベストアンサー

  • ベストアンサー
  • miyuyu
  • ベストアンサー率61% (30/49)
回答No.1

こんにちは 用途はわかりませんが逃げるフォーム 面白いプログラムですね~ 既に解決していらっしゃるかもしれませんが 20を1にしてみてはいかがでしょうか 1.8GHz 2GB でもスムーズに動きましたよ

ddtqp
質問者

お礼

回答ありがとうございます。 20を1にすると、移動速度が遅いんです。。。

ddtqp
質問者

補足

作りたいイメージのソフトを見つけました。 mag4pという拡大鏡フリーソフトです。 こちらはスムーズに動きました。 スペックの問題ではないようです。 APIをつかうか、フォームをペイントで描いてみます。

その他の回答 (1)

  • MARU4812
  • ベストアンサー率43% (196/452)
回答No.2

>> 1.8GHz 2GB でもスムーズに動きましたよ > 20を1にすると、移動速度が遅いんです。。。 スムーズには動いたのですか? スペックの悪いPCでどうにかしようという話なのか、 1回の移動距離を20にしたい話なのかが分かりません でした。 ただ、後者でも、1回のトリガーで1から20までの座標に 順番に移動させればいいだけに思えますけどね。 プログラム関係なく、小学生がなぞなぞ解くレベルの 発想にも思いますが。

ddtqp
質問者

お礼

回答ありがとうございます。 初めは移動距離を+1にしていたのですが、 ゆっくりいどうするので+20にしました。 そうするとカクカクしながら移動していくので、 なめらかに移動できないものか考えています。

関連するQ&A

  • VB2008でブロック崩しを作っているのですが、玉は動くようになり次に

    VB2008でブロック崩しを作っているのですが、玉は動くようになり次に自機を動かそうと思いやってみたのですが動きません。 もう一つプロジェクトを作って自機を動かす文だけでやってみるときちんと動きます。 Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown Dim w As Graphics = Pic1.CreateGraphics() w.DrawImage(picback, zikiX, zikiY, rect, GraphicsUnit.Pixel) Select Case e.KeyCode Case Keys.Left : zikiX -= 3 Case Keys.Right : zikiX += 3 End Select w.DrawImage(ziki, zikiX, zikiY) w.Dispose() End Sub Private Sub ballmove() Dim g As Graphics = Pic1.CreateGraphics() tamasita = y + 28 tamawidth = x + 28 zikiwidth = zikiX + 15 zikisita = zikiY + 73 rec.X = x : rec.Y = y : rec.Width = 28 : rec.Height = 28 g.DrawImage(picback, x, y, rec, GraphicsUnit.Pixel) g.DrawImage(ziki, zikiX, zikiY) If x + 28 >= Pic1.Width Then flag_x = False ElseIf x < 1 And flag_x = False Then flag_x = True End If If y + 28 >= Pic1.Height Then flag_y = False ElseIf y < 1 And flag_y = False Then flag_y = True End If If flag_x = True Then x = x + 3 Else x = x - 3 End If If flag_y = True Then y = y + 3 Else y = y - 3 End If g.DrawImage(tama, x, y) g.Dispose() End Sub

  • VB2010平面移動

    VisualBasic2010においてMouseDown、DragDropを用いてPanelを移動させるコードを書きました。 Panel4へPanel1,Panel2,Panel3を移動させるというものです。  例) Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Shown Panel4.AllowDrop = True End Sub Private Sub Panel1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseDown Panel1.DoDragDrop(Panel1, DragDropEffects.Move) End Sub Private Sub Panel2_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel2.MouseDown Panel2.DoDragDrop(Panel2, DragDropEffects.Move) End Sub Private Sub Panel3_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel3.MouseDown Panel3.DoDragDrop(Panel3, DragDropEffects.Move) End Sub Private Sub Panel4_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Panel4.DragDrop Dim srsPnl As Panel = e.Data.GetData(GetType(Panel)) Dim dstPnl As New Panel dstPnl.Size = srsPnl.Size dstPnl.Location = Panel4.PointToClient(CursorPosition) 'New Point(e.X, e.Y) dstPnl.BackColor = srsPnl.BackColor AddHandler dstPnl.MouseDown, AddressOf dstPnl_MouseDown AddHandler dstPnl.MouseMove, AddressOf dstPnl_MouseMove Panel4.Controls.Add(dstPnl) End Sub Private Sub Panel4_DragEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Panel4.DragEnter If e.Data.GetDataPresent(GetType(Panel)) Then e.Effect = DragDropEffects.Move End If End Sub Private previousPos As Point Private Sub dstPnl_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseDown previousPos = CursorPosition() End Sub Private Sub dstPnl_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) If e.Button = Windows.Forms.MouseButtons.Left Then Dim nowPos As Point = CursorPosition() DirectCast(sender, Panel).Left += nowPos.X - previousPos.X DirectCast(sender, Panel).Top += nowPos.Y - previousPos.Y Console.WriteLine(nowPos.X & "-" & previousPos.X) previousPos = nowPos End If End Sub Function CursorPosition() As Point Return New Point(CInt(Cursor.Position.X / 10) * 10, CInt(Cursor.Position.Y / 10) * 10) End Function このようにした場合、初めに移動させたものが最前にきてしまいます。(画像参照) 私はPanel1(黒)を最前に持っていきたいと考えています。 もしお時間等ありましたら、お力添えをいただけると嬉しく思います。 どうかよろしくお願いします。

  • VB2010 面積算出

    panelを用いた図形の作成を行っております。 コード Public Class Form1 Dim p1 As Integer 'Panel1のカウンター Dim p2 As Integer 'Panel2のカウンター Dim p3 As Integer 'Panel3のカウンター Dim p1size As Single 'Panel1の面積 Dim p2size As Single 'Panel2の面積 Dim p3size As Single 'Panel3の面積 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Shown Dim p1clnt As Drawing.Size = Panel1.ClientSize Dim p2clnt As Drawing.Size = Panel2.ClientSize Dim p3clnt As Drawing.Size = Panel3.ClientSize p1size = p1clnt.Width * p1clnt.Height p2size = p2clnt.Width * p2clnt.Height p3size = p3clnt.Width * p3clnt.Height Panel4.AllowDrop = True End Sub Private Sub Panel1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseDown, Panel2.MouseDown, Panel3.MouseDown sender.DoDragDrop(sender, DragDropEffects.Move) End Sub Private Sub Panel4_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Panel4.DragDrop Dim srsPnl As Panel = e.Data.GetData(GetType(Panel)) Dim dstPnl As New Panel dstPnl.Size = srsPnl.Size Dim dropp1 As Integer = srsPnl.Size.Width * srsPnl.Size.Height If dropp1 = p1size Then p1 += 1 ElseIf dropp1 = p2size Then p2 += 1 ElseIf dropp1 = p3size Then p3 += 1 End If dstPnl.Location = Panel4.PointToClient(CursorPosition) 'New Point(e.X, e.Y) dstPnl.BackColor = srsPnl.BackColor AddHandler dstPnl.MouseDown, AddressOf dstPnl_MouseDown AddHandler dstPnl.MouseMove, AddressOf dstPnl_MouseMove Panel4.Controls.Add(dstPnl) End Sub Private Sub Panel4_DragEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Panel4.DragEnter If e.Data.GetDataPresent(GetType(Panel)) Then e.Effect = DragDropEffects.Move End If End Sub Private previousPos As Point Private Sub dstPnl_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseDown previousPos = CursorPosition() End Sub Private Sub dstPnl_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) If e.Button = Windows.Forms.MouseButtons.Left Then Dim nowPos As Point = CursorPosition() DirectCast(sender, Panel).Left += nowPos.X - previousPos.X DirectCast(sender, Panel).Top += nowPos.Y - previousPos.Y Console.WriteLine(nowPos.X & "-" & previousPos.X) previousPos = nowPos End If End Sub Function CursorPosition() As Point Return New Point(CInt(Cursor.Position.X / 10) * 10, CInt(Cursor.Position.Y / 10) * 10) End Function Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim ans As Single ans = (p1 * p1size + p2 * p2size + p3 * p3size) * 0.0001 TextBox1.Text = ans End Sub End Class 上記のコードは、GroupBox内のpanelをpanel4へ移動させて図形を作成するものです。 移動させたpanelの数を数えて、全体の面積を算出させているのですが、 作成させた図形の右側1/4の面積だけ(左側1/4,下側1/4といった風に)の面積を算出させることは可能でしょうか? 画像 ※図形は任意  重複なし(panel同士) 1m2   (青) 3枚 0.25m2 (赤) 5枚 0.09m2 (黄) 5枚 計 4.7m2 となっています。 この作成した図形の緑色で着色した部分の面積を出したいと考えております。 もしお時間等ありましたら、お力添えをいただけると嬉しく思います。 どうかよろしくお願いします。

  • マウスで画像の移動(VB2010)

    FormにPanelを配置してそのなかにPicturBoxを配置しています。 エクスプローラから画像をドラッグアンドドロップして、マウスで画像を移動させようと考えています。 (Panelのサイズを250,250にして、1024*768ピクセルの画像の一部を窓から見ているような感じ) 下記のコードを書いたのですが、マウスを左クリックした状態のままマウスを移動させると、画像がちらつきます。 PictureBox1.Refresh()を入れて多少現象しましたが、根本的な問題の解決には至っておりません。 どなたか?詳しい方いらっしゃいましたら教えて頂けないでしょうか?宜しくお願いいたいます。 Private drawFlag As Boolean Private ptStart As Point Private ptEnd As Point Private Sub PictureBox1_DragEnter(ByVal sender As Object, _ ByVal e As System.Windows.Forms.DragEventArgs) _ Handles PictureBox1.DragEnter If e.Data.GetDataPresent(DataFormats.FileDrop) Then e.Effect = DragDropEffects.Copy Else e.Effect = DragDropEffects.None End If End Sub Private Sub PictureBox1_DragDrop(ByVal sender As Object, _ ByVal e As System.Windows.Forms.DragEventArgs) _ Handles PictureBox1.DragDrop Dim strFileName As String() = CType(e.Data.GetData(DataFormats.FileDrop), String()) Dim fi As New System.IO.FileInfo(strFileName(0)) Dim MyBmp As Bitmap = System.Drawing.Image.FromFile(strFileName(0)) PictureBox1.Image = MyBmp End Sub Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles MyBase.Load PictureBox1.SizeMode = PictureBoxSizeMode.AutoSize PictureBox1.AllowDrop = True drawFlag = False End Sub Private Sub PictureBox1_MouseMove(ByVal sender As Object, _ ByVal e As System.Windows.Forms.MouseEventArgs) _ Handles PictureBox1.MouseMove If drawFlag = False Then Exit Sub End If ptEnd = e.Location Dim ptMove As Point ptMove = ptEnd - ptStart Dim MyLocation As Point MyLocation = PictureBox1.Location + ptMove PictureBox1.Location = MyLocation PictureBox1.Refresh() ptStart = ptEnd End Sub Private Sub PictureBox1_MouseDown(ByVal sender As Object, _ ByVal e As System.Windows.Forms.MouseEventArgs) _ Handles PictureBox1.MouseDown If e.Button = Windows.Forms.MouseButtons.Left Then drawFlag = True ptStart.X = e.X ptStart.Y = e.Y End If End Sub Private Sub PictureBox1_MouseUp(ByVal sender As Object, _ ByVal e As System.Windows.Forms.MouseEventArgs) _ Handles PictureBox1.MouseUp drawFlag = False End Sub

  • 【VB2005】コントロールを配列に。

    下記のコードを変更して、コントロールの値を配列に代入して、 その値をもとに、ボタンを複製して追加したいと思っています。 Public Class frmStart Dim btnNewButton1 As New Button Private Sub frmStart_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load NewButton1.Text = "処理1" btnNewButton1.ForeColor = Color.DimGray btnNewButton1.Location = New Point(10, 100) btnNewButton1.Size = New Size(180, 30) '// ボタンを追加します Me.Controls.Add(btnNewButton1) end sub End Class やりたいことは、Dim btnNewButton() As New Button のようにデータを配列にして、 btnNewButton(0).ForeColor = Color.DimGray btnNewButton(0).Location = New Point(10, 100) btnNewButton(0).Size = New Size(180, 30) btnNewButton(1).ForeColor = Color.DimGray btnNewButton(1).Location = New Point(380, 100) btnNewButton(1).Size = New Size(180, 30) '// ボタンを追加します Me.Controls.Add(btnNewButton(0))     Me.Controls.Add(btnNewButton(1)) という事をしたいのですが、方法が解らなくて困ってます。 お知恵を貸してください。

  • VB2008 Formドラッグ時の描画を早くしたいです..

     こんばんは,質問させていただきます. どうぞよろしくお願いたします.  Formが4つあるアプリを作成しております.いずれをドラッグされた際も これらが一緒に動くように,下のように方法でコーディングいたしました. が,コントロールの数が多いせいか,カクカク動いてしまいます.  (↓Form3をドラッグした際のコードでございます)   Private mousePoint As Point   Private Sub Form3_MouseDown(ByVal sender As Object, _     ByVal e As System.Windows.Forms.MouseEventArgs) _     Handles MyBase.MouseDown     If (e.Button And MouseButtons.Left) = MouseButtons.Left Then       mousePoint = New Point(e.X, e.Y) '位置を記憶     End If   End Sub   Private Sub Form3_MouseMove(ByVal sender As Object, _     ByVal e As System.Windows.Forms.MouseEventArgs) _     Handles MyBase.MouseMove 'マウスが動いたとき     If (e.Button And MouseButtons.Left) = MouseButtons.Left Then       Me.Left += e.X - mousePoint.X       Me.Top += e.Y - mousePoint.Y       Form1.Left += e.X - mousePoint.X       Form1.Top += e.Y - mousePoint.Y       Form2.Left += e.X - mousePoint.X       Form2.Top += e.Y - mousePoint.Y       Form4.Left += e.X - mousePoint.X       Form4.Top += e.Y - mousePoint.Y     End If   End Sub  VBでFormの動きを早くする方法というのは,単純にコントロール数を 減らすか,またはPCのスペックを上げるしか無いのでしょうか・・・?  もしお詳しい方がいらっしゃいましたら,是非とも何かアドバイスを いただきたくお願い申し上げます. どうぞよろしくお願いいたします.

  • VB2010 bitmapとして取り込む

    現在、panelを移動させて図形を作成するプログラムを書いています。 このpanelを移動させて作成した図形を画像?(bitmap等)として取り込み、他のフォームで読み込ませたいと考えております。 panelを移動させるコード Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Shown Panel4.AllowDrop = True End Sub Private Sub Panel1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseDown, Panel2.MouseDown, Panel3.MouseDown sender.DoDragDrop(sender, DragDropEffects.Move) End Sub Private Sub Panel4_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Panel4.DragDrop Dim srsPnl As Panel = e.Data.GetData(GetType(Panel)) Dim dstPnl As New Panel dstPnl.Size = srsPnl.Size dstPnl.Location = Panel4.PointToClient(CursorPosition) 'New Point(e.X, e.Y) dstPnl.BackColor = srsPnl.BackColor AddHandler dstPnl.MouseDown, AddressOf dstPnl_MouseDown AddHandler dstPnl.MouseMove, AddressOf dstPnl_MouseMove Panel4.Controls.Add(dstPnl) End Sub Private Sub Panel4_DragEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Panel4.DragEnter If e.Data.GetDataPresent(GetType(Panel)) Then e.Effect = DragDropEffects.Move End If End Sub Private previousPos As Point Private Sub dstPnl_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseDown previousPos = CursorPosition() End Sub Private Sub dstPnl_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) If e.Button = Windows.Forms.MouseButtons.Left Then Dim nowPos As Point = CursorPosition() DirectCast(sender, Panel).Left += nowPos.X - previousPos.X DirectCast(sender, Panel).Top += nowPos.Y - previousPos.Y Console.WriteLine(nowPos.X & "-" & previousPos.X) previousPos = nowPos End If End Sub Function CursorPosition() As Point Return New Point(CInt(Cursor.Position.X / 10) * 10, CInt(Cursor.Position.Y / 10) * 10) End Function 上記のコードでpanel4へpanel1,panel2,panel3を自由に移動させて任意の図形を作成することができます。 このコードを用いて画像のような図形を作った際に、その図形を他のフォームで読み込ませたいです。 わかる方がいらっしゃいましたら、お力添えしていただけると幸いです。 よろしくお願いします。

  • もぐらたたきのプログラミング

    大至急返答お願いします。 学校の自由実験で、VB2008でもぐらたたきを作っているのですが、以下のソースでデバッグし、ゲームを開始するボタンを押すとすると変なバグが表示されます。 Private Sub PictureBox_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Static Hit As Integer 'もぐらを叩いた時 Me.Controls.Remove(sender) 'もぐらが消える Hit += 1 'Hitが1加算される Point = 0 + Hit 'Pointは 0+ Hit で表わされる Label5.Text = Format(Point, "000") End Sub Private Sub Timer4_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer4.Tick Dim X, Y As Integer '座標指定 Dim mole1 As System.Windows.Forms.PictureBox '表示されるモグラ For Each mole1 In Me.Controls mole1.Tag -= 1 'もぐらの出現している時間のカウント If mole1.Tag < 0 Then Me.Controls.Remove(mole1) 'カウントが0になったらもぐらが消える End If Next If Rnd() * 4 < 1 Then mole1 = New System.Windows.Forms.PictureBox '1/4の確率でもぐらを表示 mole1.Size = New Size(100, 100) 'もぐらのサイズは100*100 X = Rnd() * (Me.ClientSize.Width - 100) Y = Rnd() * (Me.ClientSize.Height - 100) mole1.Location = New Point(X, Y) 'もぐらはX,Yの場所に表示される mole1.Image = My.Resources.Mole mole1.Tag = Int(Rnd() * 9) + 7 'もぐらの停滞している時間のカウント AddHandler mole1.Click, AddressOf PictureBox_Click Me.Controls.Add(mole1) End If End Sub もぐらを表示させるためのTimerのソースはこれなのですが、デバッグしてゲームスタートボタンを押すと InvalidCastExceptionはハンドルされませんでした。 型 'System.Windows.Forms.Label' のオブジェクトを型 'System.Windows.Forms.PictureBox' にキャストできません。 という意味深な文章が表示されてしまいます。 まだ初心者なので、よく分からないことが結構あります。 自己解決しようと努力してきましたが結局できず、終了期間間際になってしまいました。 是非御返答の程よろしくお願いします。

  • VB IF なぜ構文エラーにならないのですか?

    Dim x, y, z As Integer x = 1 y = 1 z = 1 If x = y And y = z Then MessageBox.Show("OK") End If 3つの値が同じならアラートを出したいのです。このソースでできるかとおもいます。。。 しかし・・・・ If x = y = z Then MessageBox.Show("OK") End If とするとアラートが出ません。関数的に使い方が間違っているのでしょうか? もしそうならなぜ構文エラーが出ないのでしょうか? よろしくお願いします。 言語Microsoft Visual Basic 2008 Express Edition

  • VB2010において同様のイベントを設定する。

    現在、VisualBasic2010を用いてPanel1~Panel4をPanel5へ移動させて図形を作成させるコードを書いております。 今後Panel1~Panel4をPanel5だけでなく、Panel6,Panel7にも同様に移動させたいと考えておりますが、やりかたがわかりません。 画像はPanel5へ移動させたものです。同様に下に配置したPanel6へも移動させたいです。 わかる方がおられましたら、お力添えいただけると助かります。 Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Panel5.AllowDrop = True End Sub Private Sub Panel1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseDown, Panel2.MouseDown, Panel3.MouseDown, Panel4.MouseDown sender.DoDragDrop(sender, DragDropEffects.Move) End Sub Private Sub Panel5_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Panel5.DragDrop Dim srsPnl As Panel = e.Data.GetData(GetType(Panel)) Dim dstPnl As New Panel dstPnl.Size = srsPnl.Size dstPnl.Location = Panel5.PointToClient(CursorPosition) 'New Point(e.X, e.Y) dstPnl.BackColor = srsPnl.BackColor AddHandler dstPnl.MouseDown, AddressOf dstPnl_MouseDown AddHandler dstPnl.MouseMove, AddressOf dstPnl_MouseMove Panel5.Controls.Add(dstPnl) End Sub Private Sub Panel5_DragEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Panel5.DragEnter If e.Data.GetDataPresent(GetType(Panel)) Then e.Effect = DragDropEffects.Move End If End Sub Private previousPos As Point Private Sub dstPnl_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseDown previousPos = CursorPosition() End Sub Private Sub dstPnl_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) If e.Button = Windows.Forms.MouseButtons.Left Then Dim nowPos As Point = CursorPosition() DirectCast(sender, Panel).Left += nowPos.X - previousPos.X DirectCast(sender, Panel).Top += nowPos.Y - previousPos.Y Console.WriteLine(nowPos.X & "-" & previousPos.X) previousPos = nowPos End If End Sub Function CursorPosition() As Point Return New Point(CInt(Cursor.Position.X / 10) * 10, CInt(Cursor.Position.Y / 10) * 10) End Function End Class これが現在書いているコードです。

専門家に質問してみよう