• 締切済み

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

  • TMYMS
  • お礼率11% (4/36)

みんなの回答

  • redfox63
  • ベストアンサー率71% (1325/1856)
回答No.3

ごめんなさい DragDropの引数 e.X e.Yがスクリーン座標で渡されてきているのを変換するのを忘れていました   ' 親パネルの DragDrop   Sub PanelDragDrop(ByVal s As Object, ByVal e As DragEventArgs)     Dim srsPanel As Panel = e.Data.GetData(GetType(Panel))     Dim dstPanel As New Panel     dstPanel.BackColor = srsPanel.BackColor     dstPanel.Size = srsPanel.Size     Dim pt As New Point(e.X, e.Y)     ’ 親パネルの参照を追加     Dim parent As Panel = s     ’ スクリーン座標を 親パネルのクライアント座標に変換     pt = parent.PointToClient(pt)     ’ ドラッグ位置のオフセットを計算     pt.Offset(-srcPt.X, -srcPt.Y)     dstPanel.Location = pt     AddHandler dstPanel.MouseDown, AddressOf Panel1_MouseDown     DirectCast(s, Panel).Controls.Add(dstPanel)   End Sub といった具合です

TMYMS
質問者

補足

回答ありがとうございます。 複数のPanelに移動させることができるようになりました。 丸投げで申し訳ないのですが、私が書いていたコードでは、DragDropさせたPanelの座標を dstPanel.Location = Panel4.PointToClient(CursorPosition) のコードで取得していたのですが、作って頂いたコードでは pt = parent.PointToClient(pt) dstPanel.Location = pt となっています。 DragDropさせたPanelの動きを Function CursorPosition() As Point Return New Point(CInt(Cursor.Position.X / 10) * 10, CInt(Cursor.Position.Y / 10) * 10) End Function によって制限させていたのですが、どのように変更させればよいでしょうか? よろしくお願いします。

  • redfox63
  • ベストアンサー率71% (1325/1856)
回答No.2

' 親パネルの DragDrop Sub PanelDragDrop(ByVal s as Object, ByVal e as DragEventArggs)   dim srsPanel as Panel = e.Data.GetData(GetType(Panel))   dim dstPanel as new Panel   dstPanel.BackColor = srsPanel.BackColor   dstPanel.Size = srsPanel.Size   dim pt as New Point(e.X, e.Y)   pt.Offset( -srcPt.X, -srcPt.Y )   dstPanel.Location = pt   AddHandler dstPanel.MouseDown, AddressOf Panel1_MouseDown   ’ 引数が sなのに senderとしてしまいました …   DirectCast(s, Panel).Controls.Add( dstPanel ) End Sub

TMYMS
質問者

補足

お返事ありがとうございます。 書いていただいたコードを用いて、やってみましたがPanelが動きません。 抜けている箇所、足りない箇所等ありましたら教えてください。 ' ドラッグ時の座標保持用 Dim srcPt As Point ' ドラッグ先での移動用パネル Dim srcPanel As Panel ' 親パネルの DragDrop Sub PanelDragDrop(ByVal s As Object, ByVal e As DragEventArgs) Dim srsPanel As Panel = e.Data.GetData(GetType(Panel)) Dim dstPanel As New Panel dstPanel.BackColor = srsPanel.BackColor dstPanel.Size = srsPanel.Size Dim pt As New Point(e.X, e.Y) pt.Offset(-srcPt.X, -srcPt.Y) dstPanel.Location = pt AddHandler dstPanel.MouseDown, AddressOf Panel1_MouseDown DirectCast(s, Panel).Controls.Add(dstPanel) End Sub ' 親パネルの DragEnter Sub PanelDragEnter(ByVal s As Object, ByVal e As DragEventArgs) If e.Data.GetDataPresent(GetType(Panel)) Then e.Effect = DragDropEffects.Move End If End Sub ' 親パネルのMouseMove Sub PanelMouseMove(ByVal s As Object, ByVal e As MouseEventArgs) If srcPanel IsNot Nothing And e.Button = MouseButtons.Left Then Dim p1 As New Point(e.X, e.Y) p1.Offset(-srcPt.X, -srcPt.Y) srcPanel.Location = p1 End If End Sub ' 親パネルでのMouseUp Sub PanelMouseUp(ByVal s As Object, ByVal e As MouseEventArgs) If srcPanel IsNot Nothing And e.Button = MouseButtons.Left Then srcPanel.Parent.Capture = False srcPanel.BringToFront() End If End Sub ' 子パネルでの MouseMove Sub Panel1_MouseDown(ByVal s As Object, ByVal e As MouseEventArgs) _ Handles Panel1.MouseDown, Panel2.MouseDown, Panel3.MouseDown, Panel4.MouseDown Dim pnl As Panel = s srcPanel = Nothing If TypeOf pnl.Parent Is Panel Then ' ドロップ先での移動用 srcPt = New Point(e.X, e.y) srcPanel = s pnl.Parent.Capture = True srcPanel.BringToFront() Else ' GroupBoxからのD&D pnl.DoDragDrop(pnl, DragDropEffects.Move) End If End Sub ' 親パネルのハンドラ登録メソッド Sub myAddHandler(ByVal ParamArray prm() As Panel) For Each pnl As Panel In prm pnl.AllowDrop = True AddHandler pnl.MouseMove, AddressOf PanelMouseMove AddHandler pnl.MouseUp, AddressOf PanelMouseUp AddHandler pnl.DragEnter, AddressOf PanelDragEnter AddHandler pnl.DragDrop, AddressOf PanelDragDrop Next End Sub Private previousPos As Point Sub dstPnl_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseDown previousPos = CursorPosition() End Sub 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 Sub Form1_Load(ByVal s As Object, ByVal e As EventArgs) Handles MyBase.Load myAddHandler(Panel5, Panel6) End Sub

  • redfox63
  • ベストアンサー率71% (1325/1856)
回答No.1

DragEnterとDragDropイベントを少々手直しして D&Dした先での移動に関しては Captureを親のパネルに設定して 親のパネルのMouseMoveイベントで行えばいいように思います ' ドラッグ時の座標保持用 dim srcPt as Point ’ ドラッグ先での移動用パネル dim srcPanel as Panel ' 親パネルの DragDrop Sub PanelDragDrop(ByVal s as Object, ByVal e as DragEventArggs)   dim srsPanel as Panel = e.Data.GetData(GetType(Panel))   dim dstPanel as new Panel   dstPanel.BackColor = srsPanel.BackColor   dstPanel.Size = srsPanel.Size   dim pt as New Point(e.X, e.Y)   pt.Offset( -srcPt.X, -srcPt.Y )   dstPanel.Location = pt   AddHandler dstPanel.MouseDown, AddressOf Panel1_MouseDown   DirectCast(sender, Panel).Controls.Add( dstPanel ) End Sub ’ 親パネルの DragEnter Sub PanelDragEnter(ByVal s as Object, ByVal e as DragEventArggs)   if e.Data.GetDataPresent(GetType(Panel)) then     e.Effect = DragDropEffects.Move   end if End Sub ’ 親パネルのMouseMove Sub PanelMouseMove( ByVal s as Object,ByVal e as MouseEventArgs)   if srcPanel isnot Nothing and e.Button = MouseBUtton.Left then     dim p1 as new Point( e.X, e.Y)     p1.Offset( -src.X, -srcPt.Y )     srcPanel.Location = p1   end if End Sub ’ 親パネルでのMouseUp Sub PanelMouseUp( s as Object, e as mouseEventArgs)   if srcPanel isnot nothing and e.Button = mouseButton.Left then     srcPanel.Parent.Capture = false     srcPanel.BringToFront()   end if End Sub ' 子パネルでの MouseMove Sub Panel1_MouseDown( s as Object, e as mouseEventArgs) _ handles Panel1.MouseDown,Panel2.MouseDown,Panel3.MouseDown,Panel4.MouseDown   dim pnl as Panel = s   srcPanel = nothing   if TypeOf pnl.Parent is Panel then     ’ ドロップ先での移動用     srcPt = new Point(e.X, e.y)     srcPanel = s     pnl.Parent.Capture = true     srcPanel.BringToFront()   else     ’ GroupBoxからのD&D     pnl.DoDragDrop(pnl,DragDropEffects.Move)   endif End Sub ’ 親パネルのハンドラ登録メソッド Sub myAddHandler(ByVal ParamArray prm() as Panel)   for each pnl as Panel in prm     pnl.AllowDrop = true     AddHandler pnl.MouseMove, AddressOf PanelMouseMove     AddHandler pnl.MouseUp, AddressOf PanelMouseUp     AddHandler pnl.DragEnter, AddressOf PanelDragEnter     AddHandler pnl.DragDrop, AddressOf PanelDragDrop   next end Sub Sub Form1_Load(ByVal s as Object, e as EventArgs) handles Form1.Load   MyAddHandler(Panel5, panel6, panel7, panel8) End Sub といった具合で いいかも …

TMYMS
質問者

補足

回答ありがとうございます。 ' 親パネルの DragDrop 部分での DirectCast(sender, Panel).Controls.Add(dstPanel) において「sender」部分にエラーがでてしまいます。 解決策がわかりません。丸投げで申し訳ないのですが、教えていただけると幸いです。 お時間がありましたら、お返事おねがいします。

関連するQ&A

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

  • VisualBasic2010,Undoについて

    VisualBasic2010においてTextBoxであれば、ボタンのクリックイベント等でTextBox1.Undo()で操作を戻すことが可能ですが 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_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Panel2.DragDrop Dim srsPnl As Panel = e.Data.GetData(GetType(Panel)) Dim dstPnl As New Panel dstPnl.Size = srsPnl.Size dstPnl.Location = Panel2.PointToClient(CursorPosition) 'New Point(e.X, e.Y) dstPnl.BackColor = srsPnl.BackColor AddHandler dstPnl.MouseDown, AddressOf dstPnl_MouseDown AddHandler dstPnl.MouseMove, AddressOf dstPnl_MouseMove Panel2.Controls.Add(dstPnl) End Sub Private Sub Panel2_DragEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Panel2.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をPanel2へ移動させるというコードを書いております。 Panel2へ移動させたPanel1を移動させる前の状態に戻したいです。 http://ameblo.jp/kazukiokumura/theme4-10011664374.html#main TextBoxでのUndoのやり方は目にすることがあるのですが、PanelでのUndoは可能なのでしょうか? お時間等ありましたらコードを作っていただけると助かります。 よろしくお願いいたします。

  • 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

  • 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 となっています。 この作成した図形の緑色で着色した部分の面積を出したいと考えております。 もしお時間等ありましたら、お力添えをいただけると嬉しく思います。 どうかよろしくお願いします。

  • 描画した後での塗りるぶし 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)

    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

  • VB初心者です。コードの書き方が分かりません。

    VB初心者です。 VBで(zのn乗)-(xのn乗+yのn乗)の計算が出来るようにしたいのですが、答えが必ず-1になってしまいます。 Option Explicit On Public Class Form1 Dim x As Long Dim y As Long Dim z As Long Dim n As Long Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Me.Close() End Sub Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged End Sub Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged End Sub Private Sub TextBox3_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox3.TextChanged End Sub Private Sub TextBox4_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox4.TextChanged End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click TextBox5.Text = (z ^ n) - (x ^ n + y ^ n) End Sub End Class 正しいコードの書き方を教えて下さい。 また特定の答えのときにメッセージを表示したいのですが、どうすればいいですか?

  • 【.NET】ボタンからマウスが離れた際に発生するイベント?

     こんばんは.いつも勉強させていただいております. 質問させていただきます.どうぞよろしくお願いたします.  Form内のボタンが小さい為,目的のボタンかどうかを視覚的に 確認しながらクリックできるように,Button1をMouseDownしたときに, BackGroundImageを変えるようにしてみました. (Button1の初期画像は「画像1」でございます)   Private Sub Button1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) _     Handles Button1.MouseDown     Button1.BackgroundImage = My.Resources.画像2   End Sub   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _     Handles Button1.Click     Button1.BackgroundImage = My.Resources.画像3         :      (色んな動作)         :     Button1.BackgroundImage = My.Resources.画像1   End Sub が,この方法だとButton1を押した状態のままカーソルをButton1の外へ 移動すると, 画像2のままになってしまいます...  マウスのフォーカス(?)が外れたことを知るようなイベントというものは ないものでございましょうか??  色々と検索したつもりでございますが,解決いたしません...  お詳しい方がいらっしゃいましたら,是非ともアドバイスいただきたく お願い申し上げます.  どうぞよろしくお願いいたします.

  • VBのこのコードの意味を教えてください。(至急)

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click このコードの意味を教えてください。 よろしくお願いします。