请教如何画出一个矩形选择框

andymdj 2012-01-04 01:35:13
请教如何画出一个矩形选择框:
1.像WINXP 桌面空白区域那种。
2.如何让矩形区域覆盖半透明颜色。


下面是拷贝原来有人提问的类似问题,给出的代码,但是我拷贝在CLASS里无效果。


Private rectList As New List(Of Rectangle)
Private pt As Point
Private bmpOld As Bitmap

Private Sub Form1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
pt.X = e.X
pt.Y = e.Y
End Sub

Private Sub Form1_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove
If e.Button = Windows.Forms.MouseButtons.Left Then
Dim g As Graphics = Graphics.FromImage(Me.BackgroundImage)

'擦除之前绘制的内容
Dim brush As New SolidBrush(Me.BackColor)
g.FillRectangle(brush, Me.ClientRectangle)
brush.Dispose()

'绘制之前的,包括当前的内容
Dim x, y, w, h As Integer

x = Math.Min(pt.X, e.X)
y = Math.Min(pt.Y, e.Y)
w = Math.Abs(pt.X - e.X)
h = Math.Abs(pt.Y - e.Y)

For i As Integer = 0 To rectList.Count - 1
g.DrawRectangle(Pens.Blue, rectList(i))
Next

g.DrawRectangle(Pens.Blue, x, y, w, h)

g.Dispose()

g = Me.CreateGraphics()
g.DrawImage(Me.BackgroundImage, 0, 0)
g.Dispose()
End If
End Sub

Private Sub Form1_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseUp
Dim x, y, w, h As Integer

x = Math.Min(pt.X, e.X)
y = Math.Min(pt.Y, e.Y)
w = Math.Abs(pt.X - e.X)
h = Math.Abs(pt.Y - e.Y)

rectList.Add(New Rectangle(x, y, w, h))
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.BackgroundImage = New Bitmap(Width, Height)
End Sub

Private Sub Form1_ResizeEnd(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.ResizeEnd
If bmpOld Is Nothing Then Return
Me.BackgroundImage = New Bitmap(Width, Height)
Dim g As Graphics = Graphics.FromImage(Me.BackgroundImage)
g.DrawImage(bmpOld, 0, 0)
g.Dispose()
bmpOld.Dispose()
bmpOld = Nothing
End Sub

Private Sub Form1_ResizeBegin(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.ResizeBegin
If BackgroundImage Is Nothing Then Return
bmpOld = Me.BackgroundImage
End Sub

Private Sub Form1_Resize(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Resize
Me.BackgroundImage = Nothing
End Sub

Private Sub Form1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
If bmpOld Is Nothing Then Return
e.Graphics.DrawImage(bmpOld, 0, 0)
End Sub

第一次接触VB,请指教!
...全文
152 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
shashengduguzhe 2012-01-04
  • 打赏
  • 举报
回复
有那么麻烦吗? 直接获取鼠标左键按下时的鼠标位置及鼠标移动过程中鼠标的位置就可以画出矩形。具体如何处理,要看你的需求。想怎么做就怎么做。
libralibra 2012-01-04
  • 打赏
  • 举报
回复
用label模拟吧

在Winforms的所有控件中,只有Form和ToolStripDropDown支持Opacity属性。当Opacity属性的值为0时,为完全透明;当值为1时,则为完全不透明;当值在0和1之间时,则显示出半透明的效果。

那么为什么其他控件不能实现半透明的效果呢?当我们深入到Winform的内部代码去分析Opacity的内部实现的时候,我们发现半透明效果需要控件支持WS_EX_LAYERED。从MSDN http://msdn.microsoft.com/en-us/library/ms632680(VS.85).aspx上我们发现WS_EX_LAYERED不能用于子窗口(Child Window)上。Form不能成为其他窗口的子窗口,而ToolStripDropDown只有在TopLevel为true的时候Opacity才有效,此时窗口的类型是pop-up而不是child类型。因此这两个类型的空间支持Opacity属性。而其他所有空间在创建的时候都用了WS_CHILD类型,都属于Child窗口,所以都不可能支持Opacity属性。

如果希望其他空间支持半透明的背景颜色,我们除了BackColor的颜色含有alpha信息之外,还需要把ControlStyles.SupportsTransparentBackColor和ControlStyles.UserPaint设为true.例如我们希望得到背景颜色为透明的Label,我们可以自己从Label继承出一个类,并在该类的构造函数里调用Control.SetStyle函数把前面两个Style设为true.此时如果背景颜色的alpha值为0,则该Label的背景变成透明的了
chinaboyzyq 2012-01-04
  • 打赏
  • 举报
回复
转.net版问吧。
andymdj 2012-01-04
  • 打赏
  • 举报
回复
初学小白请高手这点啊,还有学VB那本书含内容比较全面?

16,722

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧