掲示板システム
ホーム
アクセス解析
カテゴリ
ログアウト
画像を選択状態にするには (ID:146431)
名前
ホームページ(ブログ、Twitterなど)のURL (省略可)
本文
アドバイスいただきましたので、早速ラバーバンドなるものを 作ってみました。 しかし、マウスを下から上に移動して右方向にムーブすると描画できますが 上から下の方向にムーブしても描画できません。 原因がつかめず困っています。 コードに間違いがありましたらおしえてください。(VB2008) Dim drawfrg As Boolean '描画フラグ Dim startpoint As Point '開始位置 Dim endpoint As Point '終了位置 Private Sub PictureBox2_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox2.MouseDown If drawfrg = False Then '描画フラグがoffのとき(1回目のクリック) startpoint.X = e.X '開始位置の取得 startpoint.Y = e.Y drawfrg = True '描画フラグON endpoint.X = -1 '終了位置の初期化 endpoint.X = -1 Else If endpoint.X <> -1 Then '前のラバーバンドを消す drawraberrectangle(startpoint, endpoint) End If endpoint.X = e.X '終了位置を取得 endpoint.Y = e.Y Dim x As Integer '四角形の左上隅X座標 If startpoint.X < endpoint.X Then x = startpoint.X Else x = endpoint.X Dim y As Integer '四角形の左上隅y座標 If startpoint.Y < endpoint.Y Then y = startpoint.Y Else y = endpoint.Y '四角形の幅と高さ Dim w As Integer = Math.Abs(startpoint.X - endpoint.X) Dim h As Integer = Math.Abs(startpoint.Y - endpoint.Y) '四角形の描画 Dim g As Graphics = PictureBox2.CreateGraphics() g.DrawRectangle(Pens.Black, x, y, w, h) 'コンテナコントロールのグラブハンドルグリフの描画 ControlPaint.DrawContainerGrabHandle(g, New Rectangle(x, y, 15, 15)) ControlPaint.DrawContainerGrabHandle(g, New Rectangle(x + w - 15, y, 15, 15)) ControlPaint.DrawContainerGrabHandle(g, New Rectangle(x, y + h - 15, 15, 15)) ControlPaint.DrawContainerGrabHandle(g, New Rectangle(x + w - 15, y + h - 15, 15, 15)) g.Dispose() '描画フラグoff drawfrg = False End If End If End If End Sub Private Sub PictureBox2_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox2.MouseMove If drawfrg = False Then '描画フラグoffのとき Return End If If endpoint.X <> -1 Then '前のラバーバンドを消す drawraberrectangle(startpoint, endpoint) End If endpoint.X = e.X '終了位置を更新 endpoint.Y = e.Y drawraberrectangle(startpoint, endpoint) '新しいラバーバンドを描画 End Sub Private Sub drawraberrectangle(ByVal p1 As Point, ByVal p2 As Point) p1 = PictureBox2.PointToScreen(p1) 'スクリーン座標に変換 p2 = PictureBox2.PointToScreen(p2) Dim rect As Rectangle = New Rectangle() '四角形 If p1.X < p2.X Then '左上隅x座標 rect.X = p1.X Else rect.X = p2.X If p1.Y < p2.Y Then '左上隅y座標 rect.Y = p1.Y Else rect.Y = p2.Y '幅と高さ rect.Width = Math.Abs(p1.X - p2.X) rect.Height = Math.Abs(p1.Y - p2.Y) 'ラバーバンド(四角形)を描画 ControlPaint.DrawReversibleFrame(rect, Color.White, FrameStyle.Dashed) End If End If End Sub
←解決時は質問者本人がここをチェックしてください。
更新する
戻る
掲示板システム
Copyright 2021 Takeshi Okamoto All Rights Reserved.