VB.NET WPF(我用的是2012)一种用户控件该怎么写 40
【请注意是WPF用户控件】我要编写一个类似于按钮的用户控件,在窗口中插入这个用户控件时,会出现三个属性:DefaultImage、OnImage、DownImage,De...
【请注意是WPF用户控件】
我要编写一个类似于按钮的用户控件,在窗口中插入这个用户控件时,会出现三个属性:DefaultImage、OnImage、DownImage,DefaultImage指鼠标没有来时的图,OnImage指鼠标移到它上面时的图,DownImage指鼠标【左键】按下时的图。这个控件还要有一个事件:当鼠标【左键】按下又弹起(也就是点一下按钮时)发生的事件。
求助……
这个控件的大小可以调整,指的是在设计界面中调整,尺寸多大,这个图就多大。这个控件可以在设计器中设置
【我要的是UserControl】
控件做好后应该是这样的
在设计器右边的属性栏中有三个选项,原图像,鼠标移动上去时的图像,鼠标左键按下时的图像
在这个WPF窗体的代码中,可以这样写:Private Sub (名称)...Handles (控件名称).MousePress,就是鼠标触发这个按钮时的事件 展开
我要编写一个类似于按钮的用户控件,在窗口中插入这个用户控件时,会出现三个属性:DefaultImage、OnImage、DownImage,DefaultImage指鼠标没有来时的图,OnImage指鼠标移到它上面时的图,DownImage指鼠标【左键】按下时的图。这个控件还要有一个事件:当鼠标【左键】按下又弹起(也就是点一下按钮时)发生的事件。
求助……
这个控件的大小可以调整,指的是在设计界面中调整,尺寸多大,这个图就多大。这个控件可以在设计器中设置
【我要的是UserControl】
控件做好后应该是这样的
在设计器右边的属性栏中有三个选项,原图像,鼠标移动上去时的图像,鼠标左键按下时的图像
在这个WPF窗体的代码中,可以这样写:Private Sub (名称)...Handles (控件名称).MousePress,就是鼠标触发这个按钮时的事件 展开
1个回答
展开全部
跟你说一下大题思路啊
1、根据你的意思,如何控件多的话可以写成一个类,该类继承于Button
2、 声明一个Public WithEvents BaseControl As System.Windows.Forms.Control 的事件
3、编写类的New对BaseControl进行初始化
4、编写事件BaseControl的事件
鼠标按下事件
Private Sub MouseDownEvent(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles BaseControl.MouseDown
Me.BaseControl.BackgroundImage = System.Drawing.Image.FromFile("鼠标按下时图片")
End Sub
依次往下写出鼠标的弹起事件,离开事件,进入事件
5、控件的大小改变,分为判断鼠标位置和鼠标按下后移动
解释起来繁琐,把代码粘给你,
Private parParent As Control '容器控件
''' <summary>
''' 鼠标指针的拉伸形状
''' </summary>
''' <remarks></remarks>
Enum EnumMousePointPosition
MouseSizeNone = 0 '无拉伸时
MouseSizeRight = 1 '拉伸右边框
MouseSizeLeft = 2 '拉伸左边框
MouseSizeBottom = 3 '拉伸下边框
MouseSizeTop = 4 '拉伸上边框
MouseSizeTopLeft = 5 '拉伸左上角
MouseSizeTopRight = 6 '拉伸右上角
MouseSizeBottomLeft = 7 '拉伸左下角
MouseSizeBottomRight = 8 '拉伸右下角
MouseDrag = 9 '鼠标拖动
End Enum
''' <summary>
''' 鼠标移动时对控件移动或拉伸
''' </summary>
''' <remarks></remarks>
Private Sub MouseMoveEvent(ByVal sender As System.Object, ByVal e As MouseEventArgs) Handles BaseControl.MouseMove
Dim c As Control = CType(sender, Control)
m_MousePointPosition = MousePointPosition(c.Size, e)
'鼠标进入区域后,所显示的样子
Select Case m_MousePointPosition
Case EnumMousePointPosition.MouseSizeNone
parParent.Cursor = Cursors.Arrow '箭头
Case EnumMousePointPosition.MouseDrag
parParent.Cursor = Cursors.SizeAll '四角指针
Case EnumMousePointPosition.MouseSizeBottom
parParent.Cursor = Cursors.SizeNS '上下指针
Case EnumMousePointPosition.MouseSizeTop
parParent.Cursor = Cursors.SizeNS '上下指针
Case EnumMousePointPosition.MouseSizeLeft
parParent.Cursor = Cursors.SizeWE '左右指针
Case EnumMousePointPosition.MouseSizeRight
parParent.Cursor = Cursors.SizeWE '左右指针
Case EnumMousePointPosition.MouseSizeBottomLeft
parParent.Cursor = Cursors.SizeNESW '右上左下指针
Case EnumMousePointPosition.MouseSizeBottomRight
parParent.Cursor = Cursors.SizeNWSE '左上右下指针
Case EnumMousePointPosition.MouseSizeTopLeft
parParent.Cursor = Cursors.SizeNWSE '左上右下指针
Case EnumMousePointPosition.MouseSizeTopRight
parParent.Cursor = Cursors.SizeNESW '右上左下指针
End Select
'拉伸和移动在鼠标按下时生效
If M_Move Then
If e.Button = MouseButtons.Left Then
Select Case MouseNowType
Case 1 '拖动操作
'设置位置
c.Location = New Point(c.Left + e.X - M_Point.X, c.Top + e.Y - M_Point.Y)
Case 2 '拖拽底部
c.Size = New Size(c.Width, c.Height + e.Y - M_CurrentPoint.Y)
Case 7 '拖拽右底部
c.Size = New Size(c.Width + e.X - M_CurrentPoint.X, c.Height + e.Y - M_CurrentPoint.Y)
Case 5 '拖拽右边
c.Size = New Size(c.Width + e.X - M_CurrentPoint.X, c.Height)
Case 3 '拖拽顶部
c.Location = New Point(c.Left, c.Top + (e.Y - M_Point.Y))
c.Size = New Size(c.Width, c.Height - (e.Y - M_Point.Y))
Case 4 '拖拽左侧
c.Location = New Point(c.Left + e.X - M_Point.X, c.Top)
c.Size = New Size(c.Width - (e.X - M_Point.X), c.Height)
Case 6 '拖拽左下侧
c.Location = New Point(c.Left + e.X - M_Point.X, c.Top)
c.Size = New Size(c.Width - (e.X - M_Point.X), c.Height + e.Y - M_CurrentPoint.Y)
Case 9 '拖拽右上部
c.Location = New Point(c.Left, c.Top + (e.Y - M_Point.Y))
c.Size = New Size(c.Width + (e.X - M_CurrentPoint.X), c.Height - (e.Y - M_Point.Y))
Case 8 '拖拽左上部
c.Location = New Point(c.Left + e.X - M_Point.X, c.Top + (e.Y - M_Point.Y))
c.Size = New Size(c.Width - (e.X - M_Point.X), c.Height - (e.Y - M_Point.Y))
End Select
M_CurrentPoint = New Point(e.X, e.Y) '记录光标拖动的当前点
End If
End If
GetDataInfo()
End Sub
''' <summary>
''' 判断鼠标位置
''' </summary>
''' <param name="Size"></param>
''' <param name="e"></param>
''' <returns></returns>
''' <remarks></remarks>
Private Function MousePointPosition(ByVal Size As Size, ByVal e As System.Windows.Forms.MouseEventArgs) As EnumMousePointPosition
Const Band As Integer = 10 '变化的有效范围
'在一个有效的范围内
If e.X >= -1 * Band And e.X <= Size.Width And e.Y >= -1 * Band And e.Y <= Size.Height Then
If e.X < Band Then
If e.Y < Band Then '在( <10, <10)范围内认为是左上部
Return EnumMousePointPosition.MouseSizeTopLeft
ElseIf e.Y > -1 * Band + Size.Height Then '左下部
Return EnumMousePointPosition.MouseSizeBottomLeft
Else
Return EnumMousePointPosition.MouseSizeLeft '左部
End If
ElseIf e.X > -1 * Band + Size.Width Then
If e.Y < Band Then
Return EnumMousePointPosition.MouseSizeTopRight '右上
ElseIf e.Y > -1 * Band + Size.Height Then
Return EnumMousePointPosition.MouseSizeBottomRight '右下
Else
Return EnumMousePointPosition.MouseSizeRight '右
End If
Else
If e.Y < Band Then
Return EnumMousePointPosition.MouseSizeTop '上部
ElseIf e.Y > -1 * Band + Size.Height Then
Return EnumMousePointPosition.MouseSizeBottom '下部
Else
Return EnumMousePointPosition.MouseDrag '移动
End If
End If
Else
Return EnumMousePointPosition.MouseSizeNone '没有操作
End If
End Function
生成类后,用New初始化一个控件,就可以用了
1、根据你的意思,如何控件多的话可以写成一个类,该类继承于Button
2、 声明一个Public WithEvents BaseControl As System.Windows.Forms.Control 的事件
3、编写类的New对BaseControl进行初始化
4、编写事件BaseControl的事件
鼠标按下事件
Private Sub MouseDownEvent(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles BaseControl.MouseDown
Me.BaseControl.BackgroundImage = System.Drawing.Image.FromFile("鼠标按下时图片")
End Sub
依次往下写出鼠标的弹起事件,离开事件,进入事件
5、控件的大小改变,分为判断鼠标位置和鼠标按下后移动
解释起来繁琐,把代码粘给你,
Private parParent As Control '容器控件
''' <summary>
''' 鼠标指针的拉伸形状
''' </summary>
''' <remarks></remarks>
Enum EnumMousePointPosition
MouseSizeNone = 0 '无拉伸时
MouseSizeRight = 1 '拉伸右边框
MouseSizeLeft = 2 '拉伸左边框
MouseSizeBottom = 3 '拉伸下边框
MouseSizeTop = 4 '拉伸上边框
MouseSizeTopLeft = 5 '拉伸左上角
MouseSizeTopRight = 6 '拉伸右上角
MouseSizeBottomLeft = 7 '拉伸左下角
MouseSizeBottomRight = 8 '拉伸右下角
MouseDrag = 9 '鼠标拖动
End Enum
''' <summary>
''' 鼠标移动时对控件移动或拉伸
''' </summary>
''' <remarks></remarks>
Private Sub MouseMoveEvent(ByVal sender As System.Object, ByVal e As MouseEventArgs) Handles BaseControl.MouseMove
Dim c As Control = CType(sender, Control)
m_MousePointPosition = MousePointPosition(c.Size, e)
'鼠标进入区域后,所显示的样子
Select Case m_MousePointPosition
Case EnumMousePointPosition.MouseSizeNone
parParent.Cursor = Cursors.Arrow '箭头
Case EnumMousePointPosition.MouseDrag
parParent.Cursor = Cursors.SizeAll '四角指针
Case EnumMousePointPosition.MouseSizeBottom
parParent.Cursor = Cursors.SizeNS '上下指针
Case EnumMousePointPosition.MouseSizeTop
parParent.Cursor = Cursors.SizeNS '上下指针
Case EnumMousePointPosition.MouseSizeLeft
parParent.Cursor = Cursors.SizeWE '左右指针
Case EnumMousePointPosition.MouseSizeRight
parParent.Cursor = Cursors.SizeWE '左右指针
Case EnumMousePointPosition.MouseSizeBottomLeft
parParent.Cursor = Cursors.SizeNESW '右上左下指针
Case EnumMousePointPosition.MouseSizeBottomRight
parParent.Cursor = Cursors.SizeNWSE '左上右下指针
Case EnumMousePointPosition.MouseSizeTopLeft
parParent.Cursor = Cursors.SizeNWSE '左上右下指针
Case EnumMousePointPosition.MouseSizeTopRight
parParent.Cursor = Cursors.SizeNESW '右上左下指针
End Select
'拉伸和移动在鼠标按下时生效
If M_Move Then
If e.Button = MouseButtons.Left Then
Select Case MouseNowType
Case 1 '拖动操作
'设置位置
c.Location = New Point(c.Left + e.X - M_Point.X, c.Top + e.Y - M_Point.Y)
Case 2 '拖拽底部
c.Size = New Size(c.Width, c.Height + e.Y - M_CurrentPoint.Y)
Case 7 '拖拽右底部
c.Size = New Size(c.Width + e.X - M_CurrentPoint.X, c.Height + e.Y - M_CurrentPoint.Y)
Case 5 '拖拽右边
c.Size = New Size(c.Width + e.X - M_CurrentPoint.X, c.Height)
Case 3 '拖拽顶部
c.Location = New Point(c.Left, c.Top + (e.Y - M_Point.Y))
c.Size = New Size(c.Width, c.Height - (e.Y - M_Point.Y))
Case 4 '拖拽左侧
c.Location = New Point(c.Left + e.X - M_Point.X, c.Top)
c.Size = New Size(c.Width - (e.X - M_Point.X), c.Height)
Case 6 '拖拽左下侧
c.Location = New Point(c.Left + e.X - M_Point.X, c.Top)
c.Size = New Size(c.Width - (e.X - M_Point.X), c.Height + e.Y - M_CurrentPoint.Y)
Case 9 '拖拽右上部
c.Location = New Point(c.Left, c.Top + (e.Y - M_Point.Y))
c.Size = New Size(c.Width + (e.X - M_CurrentPoint.X), c.Height - (e.Y - M_Point.Y))
Case 8 '拖拽左上部
c.Location = New Point(c.Left + e.X - M_Point.X, c.Top + (e.Y - M_Point.Y))
c.Size = New Size(c.Width - (e.X - M_Point.X), c.Height - (e.Y - M_Point.Y))
End Select
M_CurrentPoint = New Point(e.X, e.Y) '记录光标拖动的当前点
End If
End If
GetDataInfo()
End Sub
''' <summary>
''' 判断鼠标位置
''' </summary>
''' <param name="Size"></param>
''' <param name="e"></param>
''' <returns></returns>
''' <remarks></remarks>
Private Function MousePointPosition(ByVal Size As Size, ByVal e As System.Windows.Forms.MouseEventArgs) As EnumMousePointPosition
Const Band As Integer = 10 '变化的有效范围
'在一个有效的范围内
If e.X >= -1 * Band And e.X <= Size.Width And e.Y >= -1 * Band And e.Y <= Size.Height Then
If e.X < Band Then
If e.Y < Band Then '在( <10, <10)范围内认为是左上部
Return EnumMousePointPosition.MouseSizeTopLeft
ElseIf e.Y > -1 * Band + Size.Height Then '左下部
Return EnumMousePointPosition.MouseSizeBottomLeft
Else
Return EnumMousePointPosition.MouseSizeLeft '左部
End If
ElseIf e.X > -1 * Band + Size.Width Then
If e.Y < Band Then
Return EnumMousePointPosition.MouseSizeTopRight '右上
ElseIf e.Y > -1 * Band + Size.Height Then
Return EnumMousePointPosition.MouseSizeBottomRight '右下
Else
Return EnumMousePointPosition.MouseSizeRight '右
End If
Else
If e.Y < Band Then
Return EnumMousePointPosition.MouseSizeTop '上部
ElseIf e.Y > -1 * Band + Size.Height Then
Return EnumMousePointPosition.MouseSizeBottom '下部
Else
Return EnumMousePointPosition.MouseDrag '移动
End If
End If
Else
Return EnumMousePointPosition.MouseSizeNone '没有操作
End If
End Function
生成类后,用New初始化一个控件,就可以用了
追问
我在问题补充中说得比较含糊,你可以去掉一堆代码……Sorry......
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询