Form上にpanelを配置し、panel上には、いくつかのtextboxやlabelが配置してあります。
この状態で、マウスのドラッグでtextBoxの範囲選択をしたいため、
panel上にDrawRectangleにて範囲選択の枠線を表示しながら、
Rectangle値の中に、TextBoxが含まれているかどうかで範囲選択を判定しています。
この時、TextBox上に描画の線が表示されるような状態になったときに、
描画の線がTextBoxの背面に隠れてしまいます。
Panel上に描画しているから当然だと思うのですが、どうにか描画の線を
Panel上のどこに描いても見えるようにしたいです。
いい方法ありますでしょうか?よろしくお願いします。
開発環境書き忘れました。
VB.NET 2003
OS:XP
です。
背景を透過するというのが案外難しいんですよね。。。
Form 上に描かれた線を TextBox の上まで持ってくるのは…
手段のきっかけすら思い付きません。。。( _)_
TextBox の描画を超えられないので、ちまちま座標計算して
TextBox にも線を描いてやるくらいしか。。。
…それを各コントロールでやると。。。ちょっと面倒そう(?)
いや、全て Control で Top Left の位置が分かるだろうから
作ってみると案外ループでまわせたりするのかもしれないけど。。。
やっぱり別のコントロールを TextBox の上に表示するのが
いいでしょうね。。。最前面に持ってこれるコントロールを
四角く変形させてドラッグの範囲に移動させる方が簡単だと
予想します。
DrawReversibleFrameがうってつけのようです。
VB2005ですが。
ヘルプで調べたら載っていたサンプルを、少し改良したものです。
Public Class Form1
' The following three methods will draw a rectangle and allow
' the user to use the mouse to resize the rectangle. If the
' rectangle intersects a control's client rectangle, the
' control's color will change.
Dim isDrag As Boolean = False
Dim theRectangle As New Rectangle(New Point(0, 0), New Size(0, 0))
Dim startPoint As Point
Private Sub Panel1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseDown
' Set the isDrag variable to true and get the starting point
' by using the PointToScreen method to convert form coordinates to
' screen coordinates.
If (e.Button = MouseButtons.Left) Then
isDrag = True
End If
Dim control As Control = CType(sender, Control)
' Calculate the startPoint by using the PointToScreen
' method.
startPoint = control.PointToScreen(New Point(e.X, e.Y))
End Sub
Private Sub Panel1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseMove
' If the mouse is being dragged, undraw and redraw the rectangle
' as the mouse moves.
If (isDrag) Then
' Hide the previous rectangle by calling the ReversibleFrame
' method with the same parameters.
ControlPaint.DrawReversibleFrame(theRectangle, Panel1.BackColor, FrameStyle.Dashed)
' Calculate the endpoint and dimensions for the new rectangle,
' again using the PointToScreen method.
Dim endPoint As Point = Panel1.PointToScreen(New Point(e.X, e.Y))
Dim width As Integer = endPoint.X - startPoint.X
Dim height As Integer = endPoint.Y - startPoint.Y
theRectangle = New Rectangle(startPoint.X, startPoint.Y, width, height)
' Draw the new rectangle by calling DrawReversibleFrame again.
ControlPaint.DrawReversibleFrame(theRectangle, Panel1.BackColor, FrameStyle.Dashed)
End If
End Sub
Private Sub Panel1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseUp
' If the MouseUp event occurs, the user is not dragging.
isDrag = False
' Draw the rectangle to be evaluated. Set a dashed frame style
' using the FrameStyle enumeration.
ControlPaint.DrawReversibleFrame(theRectangle, Panel1.BackColor, FrameStyle.Dashed)
' Find out which controls intersect the rectangle and change their color.
' The method uses the RectangleToScreen method to convert the
' Control's client coordinates to screen coordinates.
Dim i As Integer
Dim controlRectangle As Rectangle
For i = 0 To Panel1.Controls.Count - 1
controlRectangle = Panel1.Controls(i).RectangleToScreen(Panel1.Controls(i).ClientRectangle)
If controlRectangle.IntersectsWith(theRectangle) Then
Panel1.Controls(i).BackColor = Color.BurlyWood
End If
Next
' Reset the rectangle.
theRectangle = New Rectangle(0, 0, 0, 0)
End Sub
End Class
特攻隊長まるるうさん、弦さん、お返事ありがとうございます。
>背景を透過するというのが案外難しいんですよね。。。
本当そうです。最初、なんとか背景を透明にする方向で考えてたのですが、
本当に難しいみたいなので、自分で描画という観点で調べておりました。
>DrawReversibleFrameがうってつけのようです。
2003にも上記メソッドがありました!
さっそくサンプルコードを参考にコーディングしてみたところ、
思い描くような動きができました。
Panel上のLabelやTextBox上にもきちんと線が描画されています。
感激です!!本当にありがとうございました!
ツイート | ![]() |