有人可以告诉我如何在设计模式下以编程方式选择控件。
我试图在VB.net中创建一个包含两个面板的自定义用户控件。我希望整个用户控件都可以在设计模式下轻松选择。我已包含以下示例并尝试了。我能够识别所选控件,但无法选择它。
我正在使用Visual Studio 2015 / Windows 10 Pro。
1.制作项目
新建项目-> Windows窗体应用程序->名称:示例-> [确定]
2.添加用户控件
添加->新建项目->用户控件->名称:usrPanel.vb-> [添加]
usrPanel.BorderStyle:固定的
3,在usrPanel上添加2个面板控件
在usrPanel上添加面板控件
名称:pnlTitle
pnlTitle.Dock:顶部
pnlTitle.BackColor:栗色
再添加一个面板
名称:pnlMain
pnlMain.Dock:完整
4.像这样修改usrPanel.vb
Imports System.ComponentModel
Imports System.Windows.Forms.Design
Imports System.Windows.Forms.Design.Behavior
<Designer(GetType(usrPanelDesigner))>
Public Class usrPanel
End Class
Public Class usrPanelDesigner
Inherits ParentControlDesigner
Private myAdorner As Adorner
Public Overrides Sub Initialize(component As IComponent)
MyBase.Initialize(component)
If (TypeOf MyBase.Control Is usrPanel) Then
MyBase.EnableDesignMode(DirectCast(MyBase.Control, usrPanel).pnlTitle, "Title")
MyBase.EnableDesignMode(DirectCast(MyBase.Control, usrPanel).pnlMain, "Main")
End If
myAdorner = New Adorner()
BehaviorService.Adorners.Add(myAdorner)
myAdorner.Glyphs.Add(New MyGlyph(BehaviorService, Control))
End Sub
End Class
5.在下面粘贴整个示例。
Class MyGlyph
Inherits Glyph
Private control As Control
Private behaviorSvc As _
System.Windows.Forms.Design.Behavior.BehaviorService
Public Sub New(ByVal behaviorSvc As _
System.Windows.Forms.Design.Behavior.BehaviorService, _
ByVal control As Control)
MyBase.New(New MyBehavior())
Me.behaviorSvc = behaviorSvc
Me.control = control
End Sub
Public Overrides ReadOnly Property Bounds() As Rectangle
Get
' Create a glyph that is 10x10 and sitting
' in the middle of the control. Glyph coordinates
' are in adorner window coordinates, so we must map
' using the behavior service.
Dim edge As Point = behaviorSvc.ControlToAdornerWindow(control)
Dim size As Size = control.Size
Dim center As New Point(edge.X + size.Width / 2, edge.Y + _
size.Height / 2)
Dim bounds1 As New Rectangle(center.X - 5, center.Y - 5, 10, 10)
Return bounds1
End Get
End Property
Public Overrides Function GetHitTest(ByVal p As Point) As Cursor
' GetHitTest is called to see if the point is
' within this glyph. This gives us a chance to decide
' what cursor to show. Returning null from here means
' the mouse pointer is not currently inside of the glyph.
' Returning a valid cursor here indicates the pointer is
' inside the glyph,and also enables our Behavior property
' as the active behavior.
If Bounds.Contains(p) Then
Return Cursors.Hand
End If
Return Nothing
End Function
Public Overrides Sub Paint(ByVal pe As PaintEventArgs)
' Draw our glyph. It is simply a blue ellipse.
pe.Graphics.FillEllipse(Brushes.Blue, Bounds)
End Sub
' By providing our own behavior we can do something interesting
' when the user clicks or manipulates our glyph.
Class MyBehavior
Inherits System.Windows.Forms.Design.Behavior.Behavior
Public Overrides Function OnMouseUp(ByVal g As Glyph, _
ByVal button As MouseButtons) As Boolean
MessageBox.Show("Hey, you clicked the mouse here")
Return True
' indicating we processed this event.
End Function 'OnMouseUp
End Class
End Class
6.Mouse Clidk之后,我想在设计模式下选择指定的面板
MessageBox.Show("Hey, you clicked the mouse here")
更改
'MessageBox.Show("Hey, you clicked the mouse here")
Dim myg As MyGlyph = CType(g, MyGlyph)
Dim c As Control = myg.control
c.Select()
c.Focus()
7,全部构建
8,在Form1上添加usrPanel控件
usrPanel已显示
单击usrPanel的标题->已选择标题
单击蓝点->没有任何反应
单击usrPanel的右边缘->整个面板被选中
单击蓝点后,如何获取usrPanel?
WinForm设计器代码全部与使用设计器服务有关。在这种情况下,您需要获取选择服务的实例,该实例通过实现ISelectionService接口的对象来体现。
您可以通过实现IServiceProvider接口的对象获取服务实例。该Control.Site物业实现的ISite接口,反过来工具的IServiceProvider。
使用IServiceProvider.GetService(Type)方法获取服务,然后使用ISelectionService.SetSelectedComponents方法选择所需的组件。
Public Overrides Function OnMouseUp(g As Glyph, button As MouseButtons) As Boolean
Dim myg As MyGlyph = CType(g, MyGlyph)
Dim c As Control = myg.control
Dim selService As ISelectionService = CType(c.Site.GetService(GetType(ISelectionService)), ISelectionService)
selService?.SetSelectedComponents({c})
Return True
End Function
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句