最接近鼠标光标? [英] Nearest point to the mouse cursor?

查看:57
本文介绍了最接近鼠标光标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在开发一个小项目,在一个类似于此的形式的面板上绘制一个hexgrid

- 它用作基于电子邮件的客户端

策略游戏:

____ ____

/ \ / \

/(0,0)\ ____ / (2,0)\ ____ /

\ / \ / \

\ ____ /(1,1)\ ____ /(3,1 )\_

/ \ / \

/(0,1)\ ____ /(2,1)\ ____ /

\ / \ / \

\ ____ /(1,2)\ ____ /(3,2)\_

/ \\ \\ / \

/(0,2) \ ____ /(2,2)\ ____ /

\ / \ / \

\ ____ /(1,3)\ ____ /(3,3)\_


格式数量可能因地图而异泽。现在问题我现在的问题是试图获得一个label.text来显示

鼠标当前所在的十六进制坐标。


使用鼠标移动功能,我可以通过使用以下公式将它自动更改为

通过(1,y)列中的行:

(为此目的HexHeight = 44像素,HexWidth = 50像素)


MouseYpos = LocalMousePosition.Y / HexHeight

Label1.text = Coords: &安培; MouseXpos& "," &安培; MouseYpos


但是,因为colums是错开的,任何列都是以偶数

的数字开头,所以新的coords在它的中途发生了变化。


所以我现在倾向于尝试标记每个十六进制的中心点,然后确定
然后确定鼠标指针最接近哪一个。但是我很难确定如何做到这一点。


有人可以帮忙吗?

I have been developing a little project which draw''s a hexgrid on a panel
within a form similar to this - it:s used as a client from a e-mail based
strategy game:
____ ____
/ \ / \
/ (0,0) \____/ (2,0) \____/
\ / \ / \
\____/(1,1) \____/ (3,1) \_
/ \ / \
/ (0,1) \____/ (2,1) \____/
\ / \ / \
\____/(1,2) \____/ (3,2) \_
/ \ / \
/ (0,2) \____/ (2,2) \____/
\ / \ / \
\____/(1,3) \____/ (3,3) \_

The number of hexes can vary depending on the mapsize. now the problem I
currently have is trying to get a label.text to display which hex coord the
mouse is currently in.

Using the mouse move function I can get it to automatically change when it
passes over the lines in the (1,y) columns by using the following formula:

(for these purposes HexHeight = 44 pixels, HexWidth = 50 pixels)

MouseYpos = LocalMousePosition.Y / HexHeight
Label1.text = "Coords: " & MouseXpos & "," & MouseYpos

however, because the colums are staggered any column starting with an even
number get''s the new coords changed halfway through it.

So I''m now leaning towards trying mark the centre point of each hex, and
then establishing which one the mousepointer is nearest to. However I''m
having difficulty in figuring out how to do this.

Can anyone help?

推荐答案

您好,

为每个单元格创建一个区域。使用该区域可以查看光标是否在单元格中。


Dim rgnCircle As Region


Private Sub Form1_Load(ByVal sender As System.Object,ByVal e As

System.EventArgs)Handles MyBase.Load


Dim pth As New System.Drawing。 Drawing2D.GraphicsPath


pth.AddEllipse(100,100,50,50)

rgnCircle =新地区(第p)


End Sub


Private Sub Form1_Paint(ByVal sender As Object,ByVal e As

System.Windows.Forms.PaintEventArgs)处理MyBase.Paint


e.Graphics.FillRegion(Brushes.Blue,rgnCircle)


End Sub


Private Sub Form1_MouseMove(ByVal sender As Object,ByVal e As

System.Windows.Forms.MouseEventArgs)处理MyBase.MouseMove


我。 Text = String.Format(" In Circle {0}",rgnCircle.IsVisible(New Point(eX,

eY)))


End Sub





-------------------------------------


" Niels Jensen" <镍********* @ discussions.microsoft.com>在留言中写道

news:9E ********************************** @ microsof t.com ...

我一直在开发一个小项目,在一个面板上绘制一个hexgrid

在一个类似于这个的形式 - 它:s用作来自基于电子邮件的客户

策略游戏:

____ ____

/ \ / \

/(0,0)\ ____ /(2,0)\ ____ /

\ / \ / \

\ ____ /(1 ,1)\ ____ /(3,1)\_

/ \ / \

/(0,1)\ ____ /(2, 1)\ ____ /

\ / \ / \

\ ____ /(1,2)\ ____ /(3,2)\ _

/ \ / \

/(0,2)\ ____ /(2,2)\ ____ /

\ / \ / \

\ ____ /(1,3) \ ____ /(3,3)\_


十六进制的数量可能因地图大小而异。现在问题我现在的问题是试图获得一个label.text来显示

鼠标当前所在的十六进制坐标。


使用鼠标移动功能,我可以通过使用以下公式将它自动更改为

通过(1,y)列中的行:

(为此目的HexHeight = 44像素,HexWidth = 50像素)


MouseYpos = LocalMousePosition.Y / HexHeight

Label1.text = Coords: &安培; MouseXpos& "," &安培; MouseYpos


但是,因为colums是错开的,任何列都是以偶数

的数字开头,所以新的coords在它的中途发生了变化。


所以我现在倾向于尝试标记每个十六进制的中心点,然后确定
然后确定鼠标指针最接近哪一个。但是我很难确定如何做到这一点。


有人可以帮忙吗?
Hi,
Create a region for each cell. Use the region isvisble to see if
the cursor is in the cell.

Dim rgnCircle As Region

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim pth As New System.Drawing.Drawing2D.GraphicsPath

pth.AddEllipse(100, 100, 50, 50)

rgnCircle = New Region(pth)

End Sub

Private Sub Form1_Paint(ByVal sender As Object, ByVal e As
System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint

e.Graphics.FillRegion(Brushes.Blue, rgnCircle)

End Sub

Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove

Me.Text = String.Format("In Circle {0}", rgnCircle.IsVisible(New Point(e.X,
e.Y)))

End Sub

Ken

-------------------------------------

"Niels Jensen" <Ni*********@discussions.microsoft.com> wrote in message
news:9E**********************************@microsof t.com...
I have been developing a little project which draw''s a hexgrid on a panel
within a form similar to this - it:s used as a client from a e-mail based
strategy game:
____ ____
/ \ / \
/ (0,0) \____/ (2,0) \____/
\ / \ / \
\____/(1,1) \____/ (3,1) \_
/ \ / \
/ (0,1) \____/ (2,1) \____/
\ / \ / \
\____/(1,2) \____/ (3,2) \_
/ \ / \
/ (0,2) \____/ (2,2) \____/
\ / \ / \
\____/(1,3) \____/ (3,3) \_

The number of hexes can vary depending on the mapsize. now the problem I
currently have is trying to get a label.text to display which hex coord the
mouse is currently in.

Using the mouse move function I can get it to automatically change when it
passes over the lines in the (1,y) columns by using the following formula:

(for these purposes HexHeight = 44 pixels, HexWidth = 50 pixels)

MouseYpos = LocalMousePosition.Y / HexHeight
Label1.text = "Coords: " & MouseXpos & "," & MouseYpos

however, because the colums are staggered any column starting with an even
number get''s the new coords changed halfway through it.

So I''m now leaning towards trying mark the centre point of each hex, and
then establishing which one the mousepointer is nearest to. However I''m
having difficulty in figuring out how to do this.

Can anyone help?




感谢您的帮助到目前为止,但我还有一个问题 - 如何为多个地区执行




我已经设法将你给我的例子应用到我的程序中但我现在

有另一个问题...可能有数百个Hexregions on

随时形成 - 问题是它是一个未知数量的地区,因为运行服务器的人可以制作任何大小的地图。


如何进行下一步并获取mousemove事件

覆盖表单上未知数量的区域?


Niels


Ken Tucker [MVP]写道:
Hi,

Thanks for the help so far but I do have one further question - how do
you do it for multiple regions?

I''ve managed to apply the example you gave me to my program but I now
have another problem... there could be hundreds of Hexregions on the
form at any time - the problem is it''s an unknown number of regions as
the person running the server can make a map of any size.

How can I take this to the next step and get the mousemove event to
cover an unknown number of regions on the form?

Niels

Ken Tucker [MVP] wrote:


创建地区对于每个细胞。使用该区域可以看到光标是否在单元格中。

Dim rgnCircle作为区域

私有子Form1_Load(ByVal sender As System.Object,ByVal e As
System.EventArgs)处理MyBase.Load

Dim pth As New System.Drawing.Drawing2D.GraphicsPath

pth.AddEllipse(100,100,50) ,50)

rgnCircle =新区域(pth)

End Sub

私有子Form1_Paint(ByVal发送者为对象,ByVal e As
System.Windows.Forms.PaintEventArgs)处理MyBase.Paint

.Graphics.FillRegion(Brushes.Blue,rgnCircle)

End Sub

Private Sub Form1_MouseMove(ByVal sender As Object,ByVal e As
System.Windows.Forms.MouseEventArgs)处理MyBase.MouseMove

Me.Text = String.Format(&In; Circle) {0}",rgnCircle.IsVisible(New Point(eX,
eY)))

结束子



-------------------------------------

Niels Jensen <镍********* @ discussions.microsoft.com>在消息中写道
新闻:9E ********************************** @ microsof t.com。 ..
我一直在开发一个小项目,在一个面板上绘制'hexgrid
在类似的形式 - 它:用作基于电子邮件的客户端
策略游戏:
____ ____
/ \ / \
/(0,0)\ ____ /(2,0)\ ____ /
\ / \\ \\ / \
\\ ____ /(1,1)\ ____ /(3,1)\_
/ \ / \
/(0,1)\\ \\ ____ /(2,1)\ ____ /
\ / \ / \
\ ____ /(1,2)\ ____ /(3,2)\_
/ \ / \
/(0,2)\ ____ /(2,2)\ ____ /
\ / \ / \
\ ____ /(1,3)\ ____ /(3,3)\_

十六进制数es可以根据地图大小而有所不同。现在我目前遇到的问题是试图获得一个label.text来显示当前鼠标所在的十六进制坐标。

使用鼠标移动功能我可以得到它当它通过以下公式越过(1,y)列中的线时自动更改:

(出于这些目的,HexHeight = 44像素,HexWidth = 50像素)

MouseYpos = LocalMousePosition.Y / HexHeight
Label1.text =" Coords:" &安培; MouseXpos& "," &安培; MouseYpos

然而,因为colums是错开的任何列,以偶数
数字开头,新的coords在它的中途改变了。

所以我'我现在倾向于尝试标记每个十六进制的中心点,然后确定鼠标指针最接近哪一个。但是我很难弄清楚如何做到这一点。

任何人都可以帮忙吗?
Hi,
Create a region for each cell. Use the region isvisble to see if
the cursor is in the cell.

Dim rgnCircle As Region

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim pth As New System.Drawing.Drawing2D.GraphicsPath

pth.AddEllipse(100, 100, 50, 50)

rgnCircle = New Region(pth)

End Sub

Private Sub Form1_Paint(ByVal sender As Object, ByVal e As
System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint

e.Graphics.FillRegion(Brushes.Blue, rgnCircle)

End Sub

Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove

Me.Text = String.Format("In Circle {0}", rgnCircle.IsVisible(New Point(e.X,
e.Y)))

End Sub

Ken

-------------------------------------

"Niels Jensen" <Ni*********@discussions.microsoft.com> wrote in message
news:9E**********************************@microsof t.com...
I have been developing a little project which draw''s a hexgrid on a panel
within a form similar to this - it:s used as a client from a e-mail based
strategy game:
____ ____
/ \ / \
/ (0,0) \____/ (2,0) \____/
\ / \ / \
\____/(1,1) \____/ (3,1) \_
/ \ / \
/ (0,1) \____/ (2,1) \____/
\ / \ / \
\____/(1,2) \____/ (3,2) \_
/ \ / \
/ (0,2) \____/ (2,2) \____/
\ / \ / \
\____/(1,3) \____/ (3,3) \_

The number of hexes can vary depending on the mapsize. now the problem I
currently have is trying to get a label.text to display which hex coord the
mouse is currently in.

Using the mouse move function I can get it to automatically change when it
passes over the lines in the (1,y) columns by using the following formula:

(for these purposes HexHeight = 44 pixels, HexWidth = 50 pixels)

MouseYpos = LocalMousePosition.Y / HexHeight
Label1.text = "Coords: " & MouseXpos & "," & MouseYpos

however, because the colums are staggered any column starting with an even
number get''s the new coords changed halfway through it.

So I''m now leaning towards trying mark the centre point of each hex, and
then establishing which one the mousepointer is nearest to. However I''m
having difficulty in figuring out how to do this.

Can anyone help?




" Niels Jensen" < NI *** @ surf-spot.co.uk>写了

"Niels Jensen" <ni***@surf-spot.co.uk> wrote

感谢您的帮助,但我还有一个问题 - 您是如何为多个地区做的?

我'我设法将你给我的例子应用到我的程序中,但我现在还有另外一个问题......
表格上可能有数百个Hexregions - 问题是它的'运行服务器的人可以制作任何大小的地图。

如何进行下一步并将鼠标移动事件发送到
覆盖表单上未知数量的区域?

Thanks for the help so far but I do have one further question - how do
you do it for multiple regions?

I''ve managed to apply the example you gave me to my program but I now
have another problem... there could be hundreds of Hexregions on the
form at any time - the problem is it''s an unknown number of regions as
the person running the server can make a map of any size.

How can I take this to the next step and get the mousemove event to
cover an unknown number of regions on the form?



由于你必须覆盖一些大量的单元格,你可以通过计算结果来做到最好

。例如,将以下代码粘贴在

a新表单下,在Designer生成的代码(部分)下。


HTH

LFS


公共图像作为位图

私有行(1)作为数组


Const SX作为整数= 30 ''红细胞大小

Const SY As Integer = 50


Const RATIO As Single = SX / SY


Private Sub Form1_Load(ByVal sender As System.Object,ByVal e As System.EventArgs)Handles MyBase.Load

Image = New Bitmap(Me.ClientSize.Width,Me.ClientSize.Height)

''行图表示哪些单元格有角度

行(0)=新整数(){-1,0,0,-2,1,1,-1}

Rows(1)= New Integer(){-2,0,0,-1,1,1,-2}

DrawHex()

End Sub


Private Sub HexCell(ByVal sender As Object,ByVal e As System.Windows.Forms.MouseEventArgs)处理MyBase.MouseMove

Dim r,x,y,hx,hy As Integer

Dim a As Integer()

''获取红色网格单元格x和y

x = eX \ SX

y = eY \ SY


''初级HexX和HexY

hx = x \ 3

hy = y \ 2


''获取行地图值(r)

a = CType(行(y和1),整数())

r = a(x Mod 6)


''调整角度细胞

如果r = -1那么

If(eX - (x * SX)) < ((SY - (eY - (y * SY)))* RATIO)然后

hx - = 1

结束如果

'' Debug.WriteLine((eX - (x * SX))。ToString&" X"&(SY - (eY - (y * SY)))。ToString&" -Y"&( (SY - (eY - (y *

SY)))* RATIO).ToString)

ElseIf r = -2然后

如果(eX - (x * SX))< ((eY - (y * SY))* RATIO)然后

hx - = 1

结束如果

''Debug.WriteLine( (eX - (x * SX))。ToString&" X"&(eY - (y * SY))。ToString&" -Y"&((eY - (y * SY)) )*

比率).ToString)

结束如果


''调整奇数十六进制列中的y偏移量

If(hx和1)= 1 AndAlso(y和1)= 0然后hy - = 1


''显示值

Dim msg As String =" X =" &安培; hx.ToString& " Y =" &安培; hy.ToString

如果Me.Text<> msg然后Me.Text = msg


结束子


Private Sub DrawHex()

''这只是在屏幕上绘制单元格

Dim grx As Graphics = Graphics.FromImage(Image)

Dim r,hx,hy,x,y As Integer

调整为整数()


对于y = 0到12

对于x = 0到17

grx .DrawRectangle(Pens.Red,New Rectangle(x * SX,y * SY,SX,SY))

a = CType(行(y和1),整数())

r = a(x Mod 6)

hx = x * SX

hy = y * SY

如果r = 0则

如果(y和1)= 0那么

grx.DrawLine(Pens.Black,hx,hy,hx + SX,hy)

否则

grx.DrawLine(Pens.Black,hx,hy + SY,hx + SX,hy + SY)

结束如果

ElseIf r = 1然后

如果(y和1)= 1那么

grx.DrawLine(Pens.Black,hx,hy,hx + SX,hy)
Else

grx.DrawLine(Pens.Black,hx,hy + SY,hx + SX,hy + SY)

结束如果

ElseIf r = -1 n

grx.DrawLine(Pens.Black,hx,hy + SY,hx + SX,hy)

ElseIf r = -2然后

grx.DrawLine(Pens.Black,hx,hy,hx + SX,hy + SY)

结束如果

下一页

下一页


End Sub


Private Sub Form1_Paint(ByVal sender As Object,ByVal e As System.Windows.Forms.PaintEventArgs)处理MyBase.Paint

Dim grx As Graphics = Me.CreateGraphics

grx.DrawImage(图片,0,0)

grx.Dispose()

End Sub


Since you have to cover some large number of cells, you might do best
by calculating the results. For an example, paste the following code in
a new form, under the Designer generated code (section).

HTH
LFS

Public Image As Bitmap
Private Rows(1) As Array

Const SX As Integer = 30 '' Red cell sizes
Const SY As Integer = 50

Const RATIO As Single = SX / SY

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Image = New Bitmap(Me.ClientSize.Width, Me.ClientSize.Height)
'' Row map indicates which cells have angles
Rows(0) = New Integer() {-1, 0, 0, -2, 1, 1, -1}
Rows(1) = New Integer() {-2, 0, 0, -1, 1, 1, -2}
DrawHex()
End Sub

Private Sub HexCell(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove
Dim r, x, y, hx, hy As Integer
Dim a As Integer()

'' Get red grid cell x and y
x = e.X \ SX
y = e.Y \ SY

'' Preliminary HexX and HexY
hx = x \ 3
hy = y \ 2

'' Get row map value (r)
a = CType(Rows(y And 1), Integer())
r = a(x Mod 6)

'' Adjust for angled cells
If r = -1 Then
If (e.X - (x * SX)) < ((SY - (e.Y - (y * SY))) * RATIO) Then
hx -= 1
End If
''Debug.WriteLine((e.X - (x * SX)).ToString & " X " & (SY - (e.Y - (y * SY))).ToString & " -Y " & ((SY - (e.Y - (y *
SY))) * RATIO).ToString)
ElseIf r = -2 Then
If (e.X - (x * SX)) < ((e.Y - (y * SY)) * RATIO) Then
hx -= 1
End If
''Debug.WriteLine((e.X - (x * SX)).ToString & " X " & (e.Y - (y * SY)).ToString & " -Y " & ((e.Y - (y * SY)) *
RATIO).ToString)
End If

'' Adjust for y offset in odd hex columns
If (hx And 1) = 1 AndAlso (y And 1) = 0 Then hy -= 1

'' Show values
Dim msg As String = "X=" & hx.ToString & " Y=" & hy.ToString
If Me.Text <> msg Then Me.Text = msg

End Sub

Private Sub DrawHex()
'' This just draws the cells on the screen
Dim grx As Graphics = Graphics.FromImage(Image)
Dim r, hx, hy, x, y As Integer
Dim a As Integer()

For y = 0 To 12
For x = 0 To 17
grx.DrawRectangle(Pens.Red, New Rectangle(x * SX, y * SY, SX, SY))
a = CType(Rows(y And 1), Integer())
r = a(x Mod 6)
hx = x * SX
hy = y * SY
If r = 0 Then
If (y And 1) = 0 Then
grx.DrawLine(Pens.Black, hx, hy, hx + SX, hy)
Else
grx.DrawLine(Pens.Black, hx, hy + SY, hx + SX, hy + SY)
End If
ElseIf r = 1 Then
If (y And 1) = 1 Then
grx.DrawLine(Pens.Black, hx, hy, hx + SX, hy)
Else
grx.DrawLine(Pens.Black, hx, hy + SY, hx + SX, hy + SY)
End If
ElseIf r = -1 Then
grx.DrawLine(Pens.Black, hx, hy + SY, hx + SX, hy)
ElseIf r = -2 Then
grx.DrawLine(Pens.Black, hx, hy, hx + SX, hy + SY)
End If
Next
Next

End Sub

Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
Dim grx As Graphics = Me.CreateGraphics
grx.DrawImage(Image, 0, 0)
grx.Dispose()
End Sub


这篇关于最接近鼠标光标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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