如何向形状添加处理程序 [英] How to addhandler to a shape

查看:84
本文介绍了如何向形状添加处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一个椭圆

i尝试调用该事件.click

但它失败了

因为我试图做一个回合圈子。

谁能教我如何添加形状?



我尝试过的事情: < br $>


myPen =新笔(Drawing.Color.Red,5)



Dim myGraphics As Graphics = Me .CreateGraphics



将myRectangle调暗为新矩形



myRectangle.X = 10



myRectangle.Y = 10



myRectangle.Width = 200



myRectangle.Height = 200



myGraphics.DrawEllipse(myPen,myRectangle)

i have make an ellipse
i try to call the event .click
but it fail
cause i am try to make a round circle.
can anyone teach me how to addhandle to a shape?

What I have tried:

myPen = New Pen(Drawing.Color.Red, 5)

Dim myGraphics As Graphics = Me.CreateGraphics

Dim myRectangle As New Rectangle

myRectangle.X = 10

myRectangle.Y = 10

myRectangle.Width = 200

myRectangle.Height = 200

myGraphics.DrawEllipse(myPen, myRectangle)

推荐答案

有这里有几个问题 - 第一个是您正在创建自己的Graphics对象,但是您没有发布它。这很糟糕:图形句柄是一种稀缺资源,如果你经常这样做,你的应用程序就会崩溃。我建议使用使用块:

There are a couple of problems here - the first is that you are creating your own Graphics object, but you aren't releasing it. That's bad: Graphics handles are a scarce resource, and if you do that too often, your app will crash. I'd recommend a Using block:
Using myGraphics As Graphics = Me.CreateGraphics
   Dim myRectangle As New Rectangle
   myRectangle.X = 10
   myRectangle.Y = 10
   myRectangle.Width = 200
   myRectangle.Height = 200
   myGraphics.DrawEllipse(myPen, myRectangle)
End Using

因为它会全部为您处理。

其次,这将绘制一个椭圆 - 一次。但它不会持久 - 如果你将另一个应用程序移到顶部,当再次显示应用程序时,椭圆将消失。要防止这种情况,请将绘图代码移动到Paint事件处理程序中,并使用event参数提供的Graphics上下文。这意味着椭圆是持久的,你不需要创建(或销毁)你自己的图形上下文。



点击椭圆是另一个问题:只有控件可以响应鼠标事件 - 因此,如果要检测何时单击椭圆,则需要处理将椭圆绘制到的控件的Click事件,并检查点击是否在实际椭圆内部或外部。

这并不像看起来那么简单,因为虽然您知道定义椭圆的边界矩形的尺寸,但并非该矩形内的所有区域都在椭圆内,并且您不知道哪些部分是!

要做到这一点,你需要创建一个椭圆路径并测试:

As it will handle it all for you.
Secondly, that will draw an ellipse - once. But it won't be "persistent" - if you move another app over the top, the ellipse will disappear when you app is displayed again. To prevent that, move the drawing code into the Paint event handler, and use the Graphics context provided by the event argument. That means the ellipse is persisted, and you don't need to create (or destroy) your own graphics context.

Clicking the ellipse is another problem: only controls can respond to mouse events - so if you want to detect when the ellipse was clicked, you need to handle the Click event for the control you drew the ellipse onto, and check if the click is inside or outside the actual ellipse.
That's not as simple as it might seem, because although you know the dimensions of the bounding rectangle that defines the ellipse, not all of the area inside that rectangle is inside the ellipse, and you don't know which parts are!
To do that, you need to create an elliptical path and test that:

Private Sub myDrawing_MouseClick(sender As Object, e As MouseEventArgs)
	Dim path As New GraphicsPath()
	path.AddEllipse(0, 0, 200, 100)
	If path.IsVisible(e.Location) Then
		Console.WriteLine("YES")
	Else
		Console.WriteLine("NO")
	End If
End Sub


这篇关于如何向形状添加处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆