這個類繼承自Panel,把它加到你的項目里面,先運行一下,然后從工具箱里把它拖到窗體上,然后再向里面添加其它控件就可以了,支持Shift加選,Alt減選
創(chuàng)新互聯專注于無棣網站建設服務及定制,我們擁有豐富的企業(yè)做網站經驗。 熱誠為您提供無棣營銷型網站建設,無棣網站制作、無棣網頁設計、無棣網站官網定制、小程序開發(fā)服務,打造無棣網絡公司原創(chuàng)品牌,更為您提供無棣網站排名全網營銷落地服務。
Imports?System.Linq
Imports?System.Collections
Public?Class?MyPanel
Inherits?Panel
'?選擇模式,相交還是包含
Enum?SelectMode
Intersects
Contains
End?Enum
Dim?down?As?New?Point(-1,?-1)
Dim?rect?As?Rectangle
Dim?selected?As?New?List(Of?Control)
Dim?editting?As?IEnumerable(Of?Control)
Dim?mode?As?SelectMode?=?SelectMode.Contains
Dim?shift,?alt?As?Boolean
Public?Sub?New()
Me.DoubleBuffered?=?True
End?Sub
Protected?Overrides?Sub?OnMouseDown(e?As?MouseEventArgs)
MyBase.OnMouseDown(e)
down?=?e.Location
editting?=?selected.ToArray().ToList()
OnMouseMove(e)
End?Sub
Protected?Overrides?Sub?OnMouseMove(e?As?MouseEventArgs)
MyBase.OnMouseMove(e)
If?e.Button?=?Windows.Forms.MouseButtons.Left?Then
Dim?loc?As?New?Point(Math.Min(down.X,?e.X),?Math.Min(down.Y,?e.Y))
Dim?size?As?New?Size(Math.Abs(down.X?-?e.X),?Math.Abs(down.Y?-?e.Y))
rect?=?New?Rectangle(loc,?size)
Dim?cs?As?New?List(Of?Control)
For?Each?c?In?Controls
cs.Add(c)
Next
Dim?a?=?cs.Where(Function(n?As?Control)?(mode?=?SelectMode.Contains?And?rect.Contains(n.Bounds))?Or?(mode?=?SelectMode.Intersects?And?rect.IntersectsWith(n.Bounds)))
If?shift?Then?editting?=?a.Union(selected)?Else?If?alt?Then?editting?=?selected.Except(a)?Else?editting?=?a
Invalidate()
End?If
End?Sub
Protected?Overrides?Sub?OnMouseUp(e?As?MouseEventArgs)
MyBase.OnMouseUp(e)
down?=?New?Point(-1,?-1)
selected?=?editting.ToList()
editting?=?Nothing
Invalidate()
End?Sub
Protected?Overrides?Function?ProcessKeyPreview(ByRef?m?As?Message)?As?Boolean
Dim?KeyCode?As?Keys?=?CInt(m.WParam)?And?CInt(Keys.KeyCode)
Dim?d?As?Boolean
If?m.Msg?=?H100?Or?m.Msg?=?H104?Then?d?=?True?Else?If?m.Msg?=?H101?Or?m.Msg?=?H105?Then?d?=?False?Else?Return?MyBase.ProcessKeyPreview(m)
If?KeyCode?=?Keys.ShiftKey?Then
shift?=?d
ElseIf?KeyCode?=?Keys.Menu?Then
alt?=?d
End?If
Return?MyBase.ProcessKeyPreview(m)
End?Function
Protected?Overrides?Sub?OnPaint(e?As?PaintEventArgs)
MyBase.OnPaint(e)
For?Each?c?As?Control?In?IIf(editting?Is?Nothing,?selected,?editting)
e.Graphics.DrawRectangle(New?Pen(Color.Gray)?With?{.DashStyle?=?Drawing2D.DashStyle.DashDot},?c.Left?-?1,?c.Top?-?1,?c.Width?+?1,?c.Height?+?1)
Next
If?(down.X??0)?Then?e.Graphics.DrawRectangle(New?Pen(Color.Gray)?With?{.DashStyle?=?Drawing2D.DashStyle.DashDot},?rect)
End?Sub
End?Class
記得VB6當中有Shape控件,但是VB.net里這個控件不存在了。
提個思路:使用Picture控件或Label控件,通過代碼在控件里繪圖想要的圖形,可以試試。
花了二十分鐘給你寫了代碼,已測試。建議學習并使用System.Drawing繪制。
主要是掌握Graphics.FillRectangle和DrawString的使用。
Imports?System.Drawing
Public?Class?進度條UI
Public?上面筆刷?As?SolidBrush?=?New?SolidBrush(Color.FromArgb(192,?175,?238,?238))
Public?下面筆刷?As?SolidBrush?=?New?SolidBrush(Color.FromArgb(192,?30,?144,?255))
Public?文字筆?As?SolidBrush?=?New?SolidBrush(Color.FromArgb(255,?255,?255,?255))
Public?字體?As?Font?=?New?Font("微軟雅黑",?14.0)
Public?文字格式?As?StringFormat?=?New?StringFormat()?With
{.Alignment?=?StringAlignment.Center,?.LineAlignment?=?StringAlignment.Center}
'''?summary
'''?繪制指定進度的圖像。
'''?當進度變化時調用一次本方法,建議將創(chuàng)建的Graphics對象保存到變量而不要重復創(chuàng)建。。
'''?/summary
'''?param?name="控件"繪制到此控件的工作區(qū)/param
'''?param?name="g"繪制到控件的Graphics對象,例如?Button1.CreateGraphics()/param
'''?param?name="進度"進度百分比實數,57%?=?0.57/param
Public?Sub?繪制(ByRef?控件?As?Control,?ByRef?g?As?Graphics,?ByVal?進度?As?Double)
Dim?矩形?=?控件.ClientRectangle?'獲取控件的工作區(qū)矩形
Dim?下面高度?=?CInt(矩形.Height?*?進度)?'獲取下面顏色塊的高度
Dim?中間位置?=?矩形.Top?+?矩形.Height?-?下面高度?'獲取中間分界線的Y坐標
Dim?上矩形?=?New?Rectangle(矩形.X,?矩形.Y,?矩形.Width,?矩形.Height?-?下面高度)
Dim?下矩形?=?New?Rectangle(矩形.X,?中間位置,?矩形.Width,?下面高度)
g.FillRectangle(上面筆刷,?上矩形)
g.FillRectangle(下面筆刷,?下矩形)
'繪制文字
Dim?文字?As?String?=?String.Format("{0:0.00}%",?進度?*?100)
g.DrawString(文字,?字體,?文字筆,?矩形,?文字格式)
End?Sub
End?Class
下面是Form1窗體的代碼:添加一個Button1和Timer1控件,將Button1尺寸拖大點
Public?Class?Form1
Public?g?As?Graphics
Public?進度條UI?As?New?進度條UI
Public?進度?As?Double
Private?Sub?Button1_Click(ByVal?sender?As?System.Object,?ByVal?e?As?System.EventArgs)?Handles?Button1.Click
g?=?Button1.CreateGraphics()
Timer1.Enabled?=?Not?Timer1.Enabled
End?Sub
Private?Sub?Timer1_Tick(ByVal?sender?As?System.Object,?ByVal?e?As?System.EventArgs)?Handles?Timer1.Tick
進度?+=?0.01
進度條UI.繪制(Button1,?g,?進度)
End?Sub
End?Class
g?=?Me.picDisplay.CreateGraphics
Dim?mybrush?As?Brush?=?New?SolidBrush(Map_Empty_Color)
Dim?rect2?As?System.Drawing.Rectangle?=?New?System.Drawing.Rectangle(0,?0,?Map_Width,?Map_Height)
g.FillRectangle(mybrush,?rect2)