• 締切済み

マウスで画像の移動(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

みんなの回答

  • cradoll
  • ベストアンサー率50% (7/14)
回答No.1

試していませんが DoubleBufferedプロパティにTrueを設定してみたらどうでしょうか

参考URL:
http://msdn.microsoft.com/ja-jp/library/system.windows.forms.control.doublebuffered(v=vs.80).aspx

関連するQ&A

  • VB205のPicturebox上でのMousewheelイベント

    VB2005で、Form1にPanel1をはりつけ、その中にPicturebox1をはりつけ、Pictureboxの範囲の中で、マウスをホィールしても、イベントが発生しません。ためしに、Picturebox1とPanel1について書いてみましたが、だめでした!(Form1では、発生します) 何がダメなんでしょうか?どなたか詳しい方がいらっしゃいましたら教えて頂けないでしょうか?宜しくお願い致します。 Private Sub PictureBox1_MouseWheel(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseWheel MsgBox("TEST_Picturebox") End Sub Private Sub Panel1_MouseWheel(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseWheel MsgBox("TESUT_Panel") End Sub

  • 描画した後での塗りるぶし VB

    丸を描画後にColorDialogで指定された色で丸の範囲だけ塗りつぶすというプログラムを作っているのですが、なかなかうまくいきません。 丸は、このように描画するようにしています。 Private Sub PictureBox1_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove If (e.Button = MouseButtons.Left) Then Dim g As Graphics = PictureBox1.CreateGraphics() Dim ePos As MouseEventArgs PictureBox1.Refresh() g.DrawEllipse(New Pen(Color.Black, 2), Spos.X, Spos.Y, e.X - Spos.X, e.Y - Spos.Y) ePos = e g.Dispose() End If End Sub Private Sub PictureBox1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown If (e.Button = System.Windows.Forms.MouseButtons.Left) Then Spos = e End If End Sub VB2010を使っています。 どなたか教えていただけるとありがたいです。よろしくお願いします。

  • 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(黒)を最前に持っていきたいと考えています。 もしお時間等ありましたら、お力添えをいただけると嬉しく思います。 どうかよろしくお願いします。

  • VB2008でPictureBoxをキーボードを使って動かしたい

    VBを始めたばかりの初心者です。 VB2008を使ってPictureBoxに取り込んだイラストをキーボードを使って自由に動かしたいと思っています。 今作ったものの問題点 1.現状、全く同時に→と↑のキーを押せば右上に動きます。  また、→キーを押してイラストが右に動いているときに、↑キーを押すと上に動きます。  これを右上に動くようにする方法が知りたいのです。 2.キーを押し続けてイラストを動かし続けようとすると、一回動いて止まってから動き続けるという風になってしまいます。  これを止まらずに動くようにしたいのです。 ちなみにこれ↓が今のソースコードです。 Public Class Form1 Private Sub form1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown If e.KeyCode = Keys.Up Then PictureBox1.Top = PictureBox1.Top - 10 End If End Sub Private Sub form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown If e.KeyCode = Keys.Down Then PictureBox1.Top = PictureBox1.Top + 10 End If End Sub Private Sub form1_KeyLeft(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown If e.KeyCode = Keys.Left Then PictureBox1.Left = PictureBox1.Left - 10 End If End Sub Private Sub form1_KeyRight(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown If e.KeyCode = Keys.Right Then PictureBox1.Left = PictureBox1.Left + 10 End If End Sub End Class これ↑を実行する場合は、デザインにPictureBoxを表示してください。PictureBoxに入れるイラストは適当なものを入れてください。 よろしくお願いします。

  • どのpicturboxをクリックしたか知りたい

    formにpicuturbox複数配置しておいてどのpictureboxがクリックされたかしりたいのですがどなたか教えてくださいおねがいします。”Private Sub PictureBox_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1,PictureBox2"ででサブルーチンには入れますがhandlesdeでまとめた複数のpicuturboxのどれがclikされたのかわからないのですがそもそもやり方が違うのでしょうか?

  • 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を自由に移動させて任意の図形を作成することができます。 このコードを用いて画像のような図形を作った際に、その図形を他のフォームで読み込ませたいです。 わかる方がいらっしゃいましたら、お力添えしていただけると幸いです。 よろしくお願いします。

  • VB2005のピクチャーボックス内の図形の移動

    VB2005で、formにPictureBox一つと、Button三つをおいて、Button1で、PictureBoxに丸を書いて、Button2とButton3で、PictureBox内で、丸を右左に移動させようと考えています。で丸を書くことと、同じプロシジャー内では、移動させることはできました。が、別のプロシジャーから移動させるってことは出来るのでしょうか?VB2005をやり始めたばかりなのでてんでわかりません。どなたか詳しい方いらっしゃいましたら教えてください。よろしくお願いします。 Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim g As Graphics = PictureBox1.CreateGraphics() g.Clear(PictureBox1.BackColor) Dim w As Integer = PictureBox1.ClientSize.Width / 3 Dim h As Integer = PictureBox1.ClientSize.Height / 3 g.ResetTransform() g.DrawEllipse(Pens.Black, 0, 0, w, h) g.TranslateTransform(80, 50) g.DrawEllipse(Pens.Black, 0, 0, w, h) g.ResetTransform() g.Dispose() End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Dim g As Graphics = PictureBox1.CreateGraphics() g.TranslateTransform(80, 50) End Sub Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click End Sub End Class

  • 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 これが現在書いているコードです。

  • VB2005 TextBoxの入力制限について

    はじめて質問させていただきます。 現在、VB2005を使ってプログラムを組んでいるのですが、半角カタカナだけをTextBoxに入力し、表示する方法がわかりません。 Private Sub TextBox6_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox6.KeyDown TextBox6.ImeMode = Windows.Forms.ImeMode.KatakanaHalf End Sub Private Sub TextBox6_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox6.KeyPress If (e.KeyChar < "ア"c Or e.KeyChar > "-"c) And e.KeyChar <> vbBack Then e.Handled = True End If End Sub ↑見にくいかもしれませんがコードです。 この方法ではだめなのでしょうか? 回答をお願いします

  • Visual Basicがわかりません。

    Public Class Form1 Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint Dim GraphicsFun As System.Drawing.Graphics GraphicsFun = Me.CreateGraphics Dim PenColor As New System.Drawing.Pen _ (System.Drawing.Color.Black) GraphicsFun.DrawRectangle(PenColor, 30, 30, 450, 300) End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Timer1.Enabled = True End Sub Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick If PictureBox1.Left < 480 - PictureBox1.Width And PictureBox1.Top + 31 Then PictureBox1.Left = PictureBox1.Left + 1 Else If PictureBox1.Top < 330 - PictureBox1.Height Then PictureBox1.Top = PictureBox1.Top + 1 Else If PictureBox1.Left > 30 Then PictureBox1.Left = PictureBox1.Left - 1 Else If PictureBox1.Top > 30 And PictureBox1.Left + 31 Then PictureBox1.Top = PictureBox1.Top - 1 End If End If End If End If End Sub End Class ピクチャーボックスを四角形の中で右周りに枠の中をボタンを押すと回るようにしたいんですけど、2回目の動作と3回目の動作がかぶってしまい、うまくいきません。4回目の解決策を教えてください。また、ボタン2を押すと反対周りにピクチャーボックスが回る、コマンドを 教えてください。

専門家に質問してみよう